Sunday, March 16, 2014

XOR Encryption Decryption Java Source Code

Written by Pranshu Bajpai |  | LinkedIn

Encryption


package DICAss;

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

/**
 *
 * @author Pranshu
 */
public class DICAss10_XOREncryption {
    public static void main(String[] args){
        String Msg = "I AM UNDER ATTACK!";
        char Key = 'k';
        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);
    }
}

Decryption



package DICAss;

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

/**
 *
 * @author Pranshu
 */
public class DICAss11_XORDecryption {
    public static void main(String[] args){
        String CTxt = "\"K*&K>%/.9K*??*( J";
        char Key = 'k';
        String Msg = "";
        int xor;
        char temp;
        for(int i = 0 ; i < CTxt.length() ; i++){
            xor = CTxt.charAt(i) ^ Key;
            temp = (char)xor;
            Msg = Msg + temp;
        }        
        
        System.out.println(Msg);
    }
}

1 comment:

  1. my ide is demanding to initialize xor variable ,i just equated it to zero but i am not sure is it a correct decision?

    ReplyDelete