Pages

Sunday, March 16, 2014

Caesar Cipher Encryption Decryption Java Source code

Written by Pranshu Bajpai |  | LinkedIn

Source code for Caesar Cipher Encryption




package DICAss;

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

/**
 *
 * @author Pranshu
 */
public class DICAss12_CaesorCipherEnc {
    public static void main(String[] args)
    {
        String Msg = "IAMUNDERATTACKW";
        int Key = 3;
        
        String CTxt = "";
        
        int temp;
        char c;
        for(int i = 0 ; i < Msg.length() ; i++){
            if (Msg.charAt(i) + Key > 90){
                temp = (Msg.charAt(i) + Key) -26;
            }
            else{
                temp = (Msg.charAt(i) + Key); 
            }
            
            c = (char) temp;
            CTxt = CTxt + c;
        }        
        
        System.out.println(CTxt);
    }
}

Source Code for Caesar Cipher Decryption

package DICAss;

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

/**
 *
 * @author Pranshu
 */
public class DICAss13_CaesorCipherDec {
    public static void main(String[] args)
    {
        String CTxt = "LDPXQGHUDWWDFNZC";
        int Key = 3;
        
        String Msg = "";
        
        int temp, num;
        char c;
        for(int i = 0 ; i < CTxt.length() ; i++){
            num = ((CTxt.charAt(i) - Key) - 64);
            if ( num <= 0){
                temp = (90 - (num * (-1))%26);
            }
            else {
                temp = (( num % 26) + 64);
            }
            
            c = (char) temp;
            Msg = Msg + c;
        }              
        System.out.println(Msg);
    }
}

No comments:

Post a Comment