Sunday, March 31, 2013

Cryptography with Java : Encryption using Java : Source Codes


Having Covered Basics of Java in Previous Programs, we now begin with Encryption Codes in Java (Cryptography) 

9. Caesar Cipher in Java, Source Code :

import java.io.*;
import java.util.*;
/**
 *
 * @author root
 */
public class Caeser_pranshu {
    public static void main(String[] args) throws IOException
    {
        System.out.println("Hi, Welcome to Pranshu's Caeser Cipher Program  ");
        System.out.println("Enter the PlainText Msg : ");
        BufferedReader brobj = new BufferedReader(new InputStreamReader(System.in));
        String plain = brobj.readLine();
        System.out.println("Msg to be Encrpyted is " + plain );
        String CipherText = "" ;
        for(int i = 0; i < plain.length() ; i++)
        {
            int ascii = plain.charAt(i);
            int cipher = ascii + 3 ;
            CipherText = CipherText + (char)cipher;
                
        }
        System.out.println("Encryption Complete!! \nThe Caeser Cihper is : " + CipherText );
        }
       
    }


10. XOR Encryption in Java, Source Code:



/**
 *
 * @author root
 */
public class xor_pranshu {
    
    public static void main(String[] args){
        String Msg = "PRANSHU";
        char Key = 'p';
        String CTxt = "";
        int xor;
        char temp;
        for(int i = 0 ; i < Msg.length() ; i++){
            xor = Msg.charAt(i) ^ Key;
            temp = (char)xor;
            CTxt = CTxt + temp;
        }        
        
        System.out.println(CTxt);
    }
}

 

 

11. One Time Pad Cryptography in Java, Source Code:

 

package dic_lab_pranshu;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;


/**
 *
 * @author pranshu
 */
public class onetimepad_pranshu {

    public static void main(String[] args) throws IOException
    {
        BufferedReader brobj = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("This is the One_Time_Pad Encyption...");
        System.out.println("Enter the Plaintext Msg : ");
        String temp_msg = brobj.readLine();
        System.out.println("Now Generating the Random String : ");
        String key = "";
        Random randGen = new Random();         //Hint: one time pad uses random key
        
        for ( int i = 0; i < temp_msg.length() ; i++)
        {
            int randInt = randGen.nextInt(26);  //Why nextInt(26)??
            key = key + (char)(65 + randInt) ;  //Why 65 + randInt ??
            
        }
        
        String cipherText = "";
        
        String msg = temp_msg.toUpperCase();
        
        for(int i = 0 ; i < msg.length() ; i++ )
        {
            int temp = (msg.charAt(i) + key.charAt(i)) ;
            
            
            if(temp >= 155)
                {
                   temp = temp - 90;
                }
                
                else
                {
                    temp = temp - 64;
                }
            char tempChar = (char)temp;
           
            cipherText = cipherText + tempChar ;
                
        }
        
        System.out.println("Encryption Done.." + "\n" + "One Time Cipher is : " + cipherText);
        
         System.out.println("Now Storing the Cipher and String to a Local File...");
         FileWriter fr = new FileWriter("/root/NetBeansProjects/DIC_Lab_Pranshu/one_time.txt", true);
         BufferedWriter br = new BufferedWriter(fr);
         br.write("Cipher is: " + cipherText + " & " + "Key is: " + key );
         br.write("\n");
         br.close();
         System.out.println("Done! Quitting now...");
    }
    
}

No comments:

Post a Comment