Sunday, March 16, 2014

Vignere Cipher 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;

/**
 *
 * @author Pranshu
 */
public class DICAss14_VignereEnc {
    public static void main(String[] args){
        String Msg = "IAMUNDERATTACK";
        //String Msg = "vaibs";
        String Key = "CRYPTO";
        
        String CTxt = "";
        
        int MsgLen = Msg.length();
        int KeyLen = Key.length();
        
        int i;
        
        //CREATING FINAL KEY TO ENCRYPT THE MESSAGE!!!
        String KeyFinal = "";
        if( MsgLen < KeyLen){
            for( i = 0 ; i < MsgLen ; i++){
                KeyFinal = KeyFinal + Key.charAt(i);
            }
            //System.out.println(KeyFinal);
        }
        else{
            int Div = MsgLen / KeyLen;
            int Rem = MsgLen % KeyLen;
            
            for (i = 0 ; i < Div ; i++){
                KeyFinal = KeyFinal + Key;
            }
            
            for( i = 0 ; i < Rem ; i++){
                KeyFinal = KeyFinal + Key.charAt(i);
            }
            //System.out.println(KeyFinal);
        }
        
        System.out.println("Msg : " + Msg);
        System.out.println("Key : " + KeyFinal);
        
        //ENCRYPTION...
        int temp;
        char c;
        int sum;
        for(i = 0 ; i < MsgLen; i++){
            sum = Msg.charAt(i) + KeyFinal.charAt(i);
            if (sum >= 155 ){
                temp = sum - 90;
            }
            else {
                temp = sum - 64;
            }
                       
            c = (char) temp;
            CTxt = CTxt + c;
        }   
        System.out.println("CiP : " + CTxt);
    }
}

Decryption


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

/**
 *
 * @author Pranshu
 */
public class DICAss15_VignereDec {
    public static void main(String[] args){
        String Msg = "";
        //String Msg = "vaibs";
        String Key = "CRYPTO";
        
        String CTxt = "LSLKHSHJZJNPFC";
        
        int CTxtLen = CTxt.length();
        int KeyLen = Key.length();

        //GENERATING FINAL KEY TO DECRYPT CIPHER TEXT!!!
        int i;
        String KeyFinal = "";
        if( CTxtLen < KeyLen){
            for( i = 0 ; i < CTxtLen ; i++){
                KeyFinal = KeyFinal + Key.charAt(i);
            }
            //System.out.println(KeyFinal);
        }
        else{
            int Div = CTxtLen / KeyLen;
            int Rem = CTxtLen % KeyLen;
            
            for (i = 0 ; i < Div ; i++){
                KeyFinal = KeyFinal + Key;
            }
            
            for( i = 0 ; i < Rem ; i++){
                KeyFinal = KeyFinal + Key.charAt(i);
            }
            //System.out.println(KeyFinal);
        }
        
        System.out.println("CiP : " + CTxt);
        System.out.println("Key : " + KeyFinal);
        
        //DECRYPTION...
        int temp;
        char c;
        int sum;
        
        for(i = 0 ; i < CTxtLen; i++){
            sum = CTxt.charAt(i) - KeyFinal.charAt(i);
            if (sum >= 0 ){
                temp = sum + 64;
            }
            else {
                temp = sum + 90;
            }
                       
            c = (char) temp;
            Msg = Msg + c;
        }   
        System.out.println("Msg : " + Msg);
    }
}

No comments:

Post a Comment