Assignment 1
Assignment 1
PRN : 2017BTECS00206
Theory :
The Caesar cipher is one of the earliest known and simplest ciphers. It is a type of
substitution cipher in which each letter in the plaintext is 'shifted' a certain
number of places down the alphabet. For example, with a shift of 1, A would be
replaced by B, B would become C, and so on.
More complex encryption schemes such as the Vigenère cipher employ the Caesar
cipher as one element of the encryption process. The widely known ROT13
'encryption' is simply a Caesar cipher with an offset of 13. The Caesar cipher offers
essentially no communication security, and it will be shown that it can be easily
broken even by hand.
First we translate all of our characters to numbers.We can now represent the
caesar cipher encryption function, e(x), where x is the character we are encrypting,
as:
Where k is the key (the shift) applied to each letter. After applying this function the
result is a number which must then be translated back into a letter. The decryption
function is :
Program :
import java.util.*;
class CaesarCipher
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int shift,i,n;
String str;
String str1="";
String str2="";
System.out.println("Enter the plaintext:");
str=sc.nextLine();
str=str.toLowerCase();
n=str.length();
char ch1[]=str.toCharArray();
char ch3,ch4;
System.out.println("\nEnter the Key :");
shift=sc.nextInt();
System.out.println();
System.out.println("\nEncrypted text is :");
for(i=0;i<n;i++)
{
if(Character.isLetter(ch1[i]))
{
ch3=(char)(((int)ch1[i]+shift-97)%26+97);
//System.out.println(ch1[i]+" = "+ch3);
str1=str1+ch3;
}
else if(ch1[i]==' ')
{
str1=str1+ch1[i];
}
}
System.out.println(str1);
System.out.println();
System.out.println("\nDecrypted text is :");
char ch2[]=str1.toCharArray();
for(i=0;i<str1.length();i++)
{
if(Character.isLetter(ch2[i]))
{
if(((int)ch2[i]-shift)<97)
{
ch4=(char)(((int)ch2[i]-shift-97+26)%26+97);
}
else
{
ch4=(char)(((int)ch2[i]-shift-97)%26+97);
}
str2=str2+ch4;
}
else if(ch2[i]==' ')
{
str2=str2+ch2[i];
}
}
System.out.println(str2);
}
}
Output :
Conclusion :
In this assignment, We came to know various uses of Caesar cipher and its
implementation.