You are on page 1of 2

import java.io.*; import java.lang.

*; class Caesar_Cipher { static int x,old_ascii,new_ascii; public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("\nEnter the shift parameter :"); x=Integer.parseInt(br.readLine()); System.out.print("\nEnter plaintext : "); String a=br.readLine(); int n=a.length(); int i; char y[]=a.toCharArray(); System.out.print("After Encryption =\t"); for(i=0;i<n;i++) { if(y[i]>='a' && y[i]<='z') y[i]=encrypt(y[i],'l'); else if(y[i]>='A' && y[i]<='Z') y[i]=encrypt(y[i],'u'); else y[i]=encrypt(y[i],'n'); System.out.print(y[i]);

System.out.print("\nAfter Decryption =\t"); for(i=0;i<n;i++) { if(y[i]>='a' && y[i]<='z') y[i]=decrypt(y[i],'l'); else if(y[i]>='A' && y[i]<='Z') y[i]=decrypt(y[i],'u'); else y[i]=decrypt(y[i],'n'); System.out.print(y[i]);

static char encrypt(char d,char t) { old_ascii=(int) d; new_ascii=old_ascii+x; if((new_ascii>122 && t=='l') || (new_ascii>90 && t=='u') || (new_ascii>57 && t=='n')) { if(t=='n') new_ascii=new_ascii-10; else new_ascii=new_ascii-26; } d=(char) new_ascii; return(d);

static char decrypt(char d,char t) { old_ascii=(int) d; new_ascii=old_ascii-x; if((new_ascii<97 && t=='l') || (new_ascii<65 && t=='u') || (new_ascii<48 && t=='n')) { if(t=='n') new_ascii=new_ascii+10; else new_ascii=new_ascii+26; } d=(char) new_ascii; return(d);

} /*

H:\CMPN\SS>javac e.java H:\CMPN\SS>java e Enter the shift parameter :3 Enter plaintext : CaesarCipher12354 After Encryption = FdhvduFlskhu45687 After Decryption = CaesarCipher12354 */