7/23/2014

Shift Cipher Code

Shift Cipher:
Another encryption algorithm yet les powerfull than RSA is shift cipher algorithm.
And caesar cipher is a special case of shift cipher where key=3



But the code provided below works for any alphabetical word and not for symbols and no's and even Capital alphabets.






CODE:


import java.util.*;
import java.io.*;
class caesarCipher
{             
                int len;
                String pText=new String();
                String pdText=new String();
                String cText=new String();int k;
                char pTextArray[],cTextArray[],cdTextArray[],pdTextArray[];
                Scanner sc=new Scanner(System.in);
                public void getInput()
                {
                                System.out.println("Enter the plaintext to be encrypted");
                                pText=sc.nextLine();
                                System.out.println("Enter the key");
                                k=sc.nextInt();
                }
                public void enCrypt()
                {
                                len=pText.length();int i;
                                System.out.println(len);
                                pTextArray=new char[len];
                                pTextArray=pText.toCharArray();
                                cTextArray=new char[len];
                               
                                for(i=0;i<len;i++)
                                {
                                                cTextArray[i]=(char)(((((int)pTextArray[i]-97)+k)%26)+97);
                                }
                               
                                cText="";
                                for(int j=0;j<len;j++)
                                cText=cText+cTextArray[j];
                                System.out.println("The Plaintext is "+pText);
                                System.out.println("The Ciphertext is "+cText);
                }
                public void deCrypt()
                {              int i=0;
                                cdTextArray=new char[len];
                                pdTextArray=new char[len];
                                cdTextArray=cText.toCharArray();
                                for(int l=0;l<26;l++)
                                {
                                                for(i=0;i<len;i++)
                                                {             
                                                               
                                                                if(((((int)cdTextArray[i]-97)-l))<0)
                                                                {
                                                                                pdTextArray[i]=(char)(((((int)cdTextArray[i]-97)-l+26)%26)+97);
                                                                }
                                                                else
                                                                {
                                                                                pdTextArray[i]=(char)(((((int)cdTextArray[i]-97)-l)%26)+97);
                                                                }
                                                                pdText=pdText+pdTextArray[i];
                                                }
                               
                                System.out.println("The plain text is: "+pdText+" for key="+l);
                                pdText="";
                                }
                }
}
class caesarCipherTest
{
                public static void main(String args[])
                {
                                caesarCipher c=new caesarCipher();
                                c.getInput();
                                c.enCrypt();
                                c.deCrypt();
                }
}
                               
OUTPUT:
C:\Users\Administrator.Home-PC\Desktop\INS_Java>javac caesarCipherTest.java
C:\Users\Administrator.Home-PC\Desktop\INS_Java>java caesarCipherTest
Enter the plaintext to be encrypted
helloworld
Enter the key
11
10
The Plaintext is helloworld
The Ciphertext is spwwzhzcwo
The plain text is: spwwzhzcwo for key=0
The plain text is: rovvygybvn for key=1
The plain text is: qnuuxfxaum for key=2
The plain text is: pmttwewztl for key=3
The plain text is: olssvdvysk for key=4
The plain text is: nkrrucuxrj for key=5
The plain text is: mjqqtbtwqi for key=6
The plain text is: lippsasvph for key=7
The plain text is: khoorzruog for key=8
The plain text is: jgnnqyqtnf for key=9
The plain text is: ifmmpxpsme for key=10
The plain text is: helloworld for key=11
The plain text is: gdkknvnqkc for key=12
The plain text is: fcjjmumpjb for key=13
The plain text is: ebiiltloia for key=14
The plain text is: dahhksknhz for key=15
The plain text is: czggjrjmgy for key=16
The plain text is: byffiqilfx for key=17
The plain text is: axeehphkew for key=18
The plain text is: zwddgogjdv for key=19
The plain text is: yvccfnficu for key=20
The plain text is: xubbemehbt for key=21
The plain text is: wtaadldgas for key=22
The plain text is: vszzckcfzr for key=23
The plain text is: uryybjbeyq for key=24
The plain text is: tqxxaiadxp for key=25

C:\Users\Administrator.Home-PC\Desktop\INS_Java>

No comments:

Post a Comment