In: Computer Science
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
static Scanner sc=new Scanner(System.in);
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
// TODO code application logic here
System.out.print("Enter any String: ");
String str = br.readLine();
System.out.print("\nEnter the Key: ");
int key = sc.nextInt();
String encrypted = encrypt(str, key);
System.out.println("\nEncrypted String is: " +encrypted);
String decrypted = decrypt(encrypted, key);
System.out.println("\nDecrypted String is: "
+decrypted); System.out.println("\n");
}
public static String encrypt(String str, int key)
{ String encrypted = "";
for(int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = c + (key % 26);
if (c > 'Z')
c = c - 26;
}
else if (Character.isLowerCase(c)) {
c = c + (key % 26);
if (c > 'z')
c = c - 26;
}
encrypted += (char) c;
}
return encrypted;
}
public static String decrypt(String str, int key)
{ String decrypted = "";
for(int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = c - (key % 26);
if (c < 'A')
c = c + 26;
}
else if (Character.isLowerCase(c)) {
c = c - (key % 26);
if (c < 'a')
c = c + 26;
}
decrypted += (char) c;
}
return decrypted;
}
}
What is the Caesar cipher?
Write the algorithm of the illustrated program?
What is the Caesar cipher?
Ciphertext is an encrypted version of plaintext. A plaintext is converted to a cipher text ny using an encryption algorithm. It is the unreadable output of an encryption algorithm and it can not be understandable until it has been converted into plain text using a key. Caesar Cipher technique is one of the earliest and most widely used encryption technique. It is also known as shift cipher, Caesar's code or Caesar shift. It’s simply a type of substitution cipher, i.e., each letter of a given text is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 2, A would be replaced by C, B would become D, and so on.
Write the algorithm of the illustrated program?
The above program is based on the substitution cipher technique where any character of plain text from the given fixed set of characters is substituted by some other character from the same set depending on a key. The algorithm is mentioned below.
Algorithm :
************************************************************************************************************************
Begin:
Declare variable: str,key,c,encrypted, decrypted;
Print: "Enter any string: "
Input: str
Print: "Enter the key: "
Input: key
// Encryption
for each character (c) in str
if c is UpperCase then
c= c + (key % 26)
if (c> 'Z') then
c=
c-26
else if c is UpperCase then
c= c + (key % 26)
if (c> 'z') then
c=
c-26
encrypted = encrypted + (char) c
End Loop
Print: encrypted
// Decryption
for each character (c) in str
if c is UpperCase then
c= c - (key % 26)
if (c <'A') then
c=
c+26
else if c is UpperCase then
c= c - (key % 26)
if (c<'a') then
c=
c+26
decrypted = decrypted + (char) c
End Loop
Print: decrypted
End:
***************************************************************************************************************************
The output of the above program is :