Tuesday, July 23, 2013

85. CAESER CIPHER IN JAVA

import java.io.*;
class Crypto
{
static int i,k,j,m,n;
static String pt="",ct="";
public static void main(String arg[])throws IOException
{
j=Integer.parseInt(arg[0]);
InputStreamReader isr= new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);

switch(j)
{
case 1:
System.out.println("\n\t ENCRYPTION\nEnter the plain text:(only basic latin supported)");
pt=br.readLine();
System.out.println("Enter the key:(0-127)");
k=Integer.parseInt(br.readLine());
for(i=0;i<pt.length();i++)
{
m=(int)(pt.charAt(i));
if(m>31&&m<128)
{
ct+=(char)((m+k)%128);
}
}
System.out.println("Plain Text:\t"+pt);
System.out.println("Cipher Text:\t"+ct);
break;

case 2:
System.out.println("\n\t DECRYPTION\nEnter the cipher text:(only basic latin supported)");
ct=br.readLine();
System.out.println("Enter the key:");
k=Integer.parseInt(br.readLine());
for(i=0;i<ct.length();i++)
{
m=(int)(ct.charAt(i));
if(m>31&&m<128)
{
pt+=(char)(((m-k)+128)%128);
}
}
System.out.println("Cipher Text:\t"+ct);
System.out.println("Plain Text:\t"+pt);
break;

default:
System.out.println("WRONG CHOICE!!!");
break;
}
}
}