Sunday, March 16, 2014

One Time Pad Encryption Decryption Source code in Java

Written by Pranshu Bajpai |  | LinkedIn

Encryption

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DICAss;
import java.util.Random;
import java.io.*;
/**
 *
 * @author Pranshu
 */
public class DICAss17_OneTimePadEnc {
    public static void main(String[] args) {
        try{
            String Msg ;
            String Key = "";
            String CTxt = "";
            
            //taking message from user
            BufferedReader ObjIn  = new BufferedReader(
                    new InputStreamReader(System.in));
            System.out.print("Enter the Message(Without any space and in Block Letters): \n");
            Msg = ObjIn.readLine();
            
            //to Generate random no. between 0-25 and Generating Key
            Random randomGenerator = new Random();
            for (int idx = 1; idx <= Msg.length(); ++idx){
              int randomInt = randomGenerator.nextInt(26);
              Key = Key + (char)(65+randomInt);
            }
            System.out.println("Key: \n" + Key);
            
            //ENCRYPTION...
            int temp;
            char c;
            int sum;
            for(int i = 0 ; i < Msg.length(); i++){
                sum = Msg.charAt(i) + Key.charAt(i);

                if (sum >= 155 ){
                    temp = sum - 90;
                }
                else {
                    temp = sum - 64;
                }

                c = (char) temp;
                CTxt = CTxt + c;
            }   
            System.out.println("CiP : " + CTxt);
            
            //CREATING FILES FOR THE KEY & CIPHER TEXT... FOR TRANSMISSION!!!
            DICAss0_FileHandling ObjFileHand = new DICAss0_FileHandling();            
            ObjFileHand.FileWriteFunc("OTP_Key.txt", Key);
            ObjFileHand.FileWriteFunc("OTP_CipherText.txt", CTxt);
        }
        catch(Exception err){
            System.err.println("Error: " + err);
        }
    }
}

Decryption


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DICAss;

/**
 *
 * @author Pranshu
 */
public class DICAss18_OneTimePadDec {
    public static void main(String[] args){
        String Msg = "";
        String Key;        
        String CTxt;
        
        DICAss0_FileHandling ObjFileHand = new DICAss0_FileHandling();
        Key = ObjFileHand.FileReadFunc("OTP_Key.txt");
        CTxt = ObjFileHand.FileReadFunc("OTP_CipherText.txt");
                
        //DECRYPTION...
        int temp;
        char c;
        int sum;
        
        for(int i = 0 ; i < CTxt.length(); i++){
            sum = CTxt.charAt(i) - Key.charAt(i);
            if (sum >= 0 ){
                temp = sum + 64;
            }
            else {
                temp = sum + 90;
            }
                       
            c = (char) temp;
            Msg = Msg + c;
        }   
        System.out.println("Msg : " + Msg);
    }
}

1 comment:

  1. DICAss0_FileHandling ObjFileHand = new DICAss0_FileHandling();
    why error at this line?

    ReplyDelete