Beruflich Dokumente
Kultur Dokumente
Q.1 Generate the Caesar cipher text of given plain text and also decrypt the generated cipher text.
Also give the user the facility to change the value of key.
Testing Data
Encryption
Plain Text :- meet me after the toga party
Cipher Text :- PHHW PH DIWHU WKH WRJD SDUWB ( n = 3)
Decryption
Cipher Text :- khoor eurwkhu
Plain Text :- HELLO BROTHER
Java Code:
import java.util.*;
class CeasarCipher
{
public static void findPlainText()
{
String letter[] = {"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" };
String plainText="", cipherTxt;
System.out.println("Enter a ciphertext");
Scanner in = new Scanner(System.in);
cipherTxt = in.nextLine();
int index = 0;
for(int i=0; i<cipherTxt.length(); i++)
{
char c = cipherTxt.charAt(i);
String val = String.valueOf(c);
if(val.equals(" "))
{
plainText = plainText + " ";
}
else
{
for(int j=0; j<letter.length; j++)
{
if(val.equalsIgnoreCase(letter[j]))
{
index = j;
break;
}
}
if(index < 3)
{
1
index = (25 - index);
plainText = plainText + letter[index];
}
else
{
plainText = plainText + letter[index - 3];
}
}
}
System.out.println("Plain text of "+cipherTxt+" is "+ plainText);
}
public static void findCeaserCipher()
{
String letter[] = {"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" };
String plainText, cipherTxt = "";
System.out.println("Enter a plaintext");
Scanner in = new Scanner(System.in);
plainText = in.nextLine();
int index = 0;
for(int i=0; i<plainText.length(); i++)
{
char c = plainText.charAt(i);
String val = String.valueOf(c);
if(val.equals(" "))
{
cipherTxt = cipherTxt + " ";
}
else
{
for(int j=0; j<letter.length; j++)
{
if(val.equalsIgnoreCase(letter[j]))
{
index = j;
break;
}
2
}
}
}
}
Output: