Monday, June 12, 2017

Caesar Cipher

Tags

Caesar Cipher:The Caesar cipher, also known as a shift cipher, is one of the simplest forms of encryption. It is a substitution cipher where each letter in the original message (called the plaintext) is replaced with a letter corresponding to a certain number of letters up or down in the alphabet.
In this way, a message that initially was quite readable, ends up in a form that can not be understood at a simple glance.
For example, here's the Caesar Cipher encryption of a message, using a right shift of 3.
Plaintext:  
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
Ciphertext: 
QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD


Following Code will Encrypt Plain Text Using Caesar Cipher :

Code:
import java.util.Scanner;
public class Caesar 
{
    
public static void main(String[] nt) 
{
Scanner in =new Scanner(System.in);
System.out.print("Enter Plain Text: ");
String s=in.nextLine();
char[] a=new char[s.length()];
char[] b={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
s=s.toUpperCase();
for(int i=0;i<s.length();i++)
{
a[i]=s.charAt(i);
}
for(int j=0;j<a.length;j++)
{  if(a[j]!=' ')
{for(int i=0;i<b.length;i++)
{
if(a[j]==b[i])
{
a[j]=b[(i+3)%26];
break;
}
}}
}
System.out.print("     Cipher Text: ");
for(char d: a)
System.out.print(d);
System.out.println();

for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i]==b[j])
{
if(j>=3)
{
a[i]=b[(j-3)%26];
}
else
{
a[i]=b[(23+j)%26];
}
break;
}
}
}
System.out.print("     Plain  Text:");
for(char j:a){
System.out.print(j);
}
System.out.println();
}


}

Output: