Source Codes > Network Security Algorithms > modified caesar cipher
Modified Caesar Cipher
//Modified Caesar Cipher
import java.io.*;
class Oneb
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your plain text");
String accept=br.readLine();
System.out.println("Enter the value of K");
int k=Integer.parseInt(br.readLine());
String cipher="",plain="";int i=0;
for(i=0;i<accept.length();i++)
{
int xyz=(int)(accept.charAt(i)+k);
int currVar=(int)(accept.charAt(i));
if(accept.charAt(i)==' ')
{ cipher=cipher+' '; }
else if(xyz>122)
{ cipher=cipher+((char)((currVar-26)+k)); }
else
{ cipher=cipher+((char)(((int)accept.charAt(i))+k)); }
}
System.out.println("\nCipher text is -> "+cipher);
System.out.println("\nDecrypted text is : ");
for(i=0;i<cipher.length();i++)
{
int xyz=(int)(cipher.charAt(i)-k);
int currVar=(int)(cipher.charAt(i));
if(cipher.charAt(i)==' ')
{ plain=plain+' '; }
else if(xyz<97)
{plain=plain+((char)((currVar+26)-k)); }
else
{plain=plain+((char)(((int)cipher.charAt(i))-k)); }
}
System.out.println("\nPlain text is -> "+plain);
}
}
import java.io.*;
class Oneb
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your plain text");
String accept=br.readLine();
System.out.println("Enter the value of K");
int k=Integer.parseInt(br.readLine());
String cipher="",plain="";int i=0;
for(i=0;i<accept.length();i++)
{
int xyz=(int)(accept.charAt(i)+k);
int currVar=(int)(accept.charAt(i));
if(accept.charAt(i)==' ')
{ cipher=cipher+' '; }
else if(xyz>122)
{ cipher=cipher+((char)((currVar-26)+k)); }
else
{ cipher=cipher+((char)(((int)accept.charAt(i))+k)); }
}
System.out.println("\nCipher text is -> "+cipher);
System.out.println("\nDecrypted text is : ");
for(i=0;i<cipher.length();i++)
{
int xyz=(int)(cipher.charAt(i)-k);
int currVar=(int)(cipher.charAt(i));
if(cipher.charAt(i)==' ')
{ plain=plain+' '; }
else if(xyz<97)
{plain=plain+((char)((currVar+26)-k)); }
else
{plain=plain+((char)(((int)cipher.charAt(i))-k)); }
}
System.out.println("\nPlain text is -> "+plain);
}
}