123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- import java.io.*;
- public class CaesarCipher {
-
- static String encrypt(String PlainText,int Key)
- {
- String cipherText="";
- int index=0,offset=0,charAscii=0,CharVal=0;
- //char ctext[]=new char[PlainText.length()] ;
- //PlainText.toLowerCase();
- //ctext=cipherText.toCharArray();
- System.out.println("PlainText Length: "+PlainText.length());
- for(index=0;index<PlainText.length();index++)
- {
- if(PlainText.charAt(index)==' '){
- cipherText+=' ';
- continue;
- }
- charAscii=PlainText.charAt(index)+Key;
- CharVal=PlainText.charAt(index);
- if((charAscii-48)>=10 && CharVal>=48 && CharVal<=57)// && charAscii<=58 )
- {
- offset=(charAscii-48)%10;
- cipherText+=(char)(offset+48);
- }
- else if((charAscii-97) >= 26 && CharVal>=97 && CharVal<=122)
- {
- offset=(charAscii-97)%26;
- cipherText+=(char)(offset+97);
- }
- else if((charAscii-65) >= 26 && CharVal>=65 && CharVal<=90)
- {
- offset=(charAscii-65)%26;
- cipherText+=(char)(offset+65);
- }
- else
- {
- //ctext[index]=(char) (PlainText.charAt(index)+3);
- //System.out.println((char) (PlainText.charAt(index)+3));
- cipherText+=((char) (PlainText.charAt(index)+Key));
- //System.out.println(cipherText);
- }
- }
- System.out.println("CipherText is: "+cipherText);
- return cipherText;
- }
- static String decrypt(String cipherText,int Key)
- {
- String PlainText="";
- int index=0,offset=0,charAscii=0,CharVal=0;
- //char ctext[]=new char[PlainText.length()] ;
- //PlainText.toLowerCase();
- //ctext=cipherText.toCharArray();
- System.out.println("CipherText Length: "+cipherText.length());
- for(index=0;index<cipherText.length();index++)
- {
- if(cipherText.charAt(index)==' '){
- PlainText+=' ';
- continue;
- }
- charAscii=cipherText.charAt(index)-Key;
- CharVal=cipherText.charAt(index);
- if((charAscii)<48 && CharVal>=48 && CharVal<=57)//&& charAscii>=38)
- {
- offset=(charAscii-48)%10;
- if(offset<0)
- offset=-(offset);
- if(offset==0)
- PlainText+='0';
- else
- PlainText+=(char)(57-offset+1);
- }
- else if(charAscii<65 && CharVal>=65 && CharVal<=90)
- {
- offset=(charAscii-65)%26;
- if(offset<0)
- offset=-(offset);
- if(offset==0)
- PlainText+='A';
- else
- PlainText+=(char)(90-offset+1);
- }
- else if((charAscii)<97 && CharVal>=97 && CharVal<=122)// && charAscii>90)
- {
- offset=(charAscii-97)%26;
- if(offset<0)
- offset=-(offset);
- if(offset==0)
- PlainText+='a';
- else
- PlainText+=(char)(122-offset+1);
- }
-
- else
- {
- //ctext[index]=(char) (PlainText.charAt(index)+3);
- //System.out.println((char) (PlainText.charAt(index)+3));
- PlainText+=((char) (cipherText.charAt(index)-Key));
- //System.out.println(cipherText);
- }
- }
- System.out.println("PlainText is: "+PlainText);
- return cipherText;
- }
- public static void main(String args[])
- {
- String ptext=null,cipherText=null;
- int Key=0;
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
-
- try {
- System.out.println("Enter the PlainText");
- ptext=br.readLine();
- System.out.println("Enter the Key");
- Key=Integer.parseInt(br.readLine());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- System.out.println("Exception:"+e.getCause());
- }
- cipherText=encrypt(ptext,Key);
- decrypt(cipherText,Key);
- }
- }
|