In: Computer Science
Write a Java program that will encode plain text into cipher text and decode cipher text into plain text. Create following methods and add them to your class:
• a method named encode(String p, int key) that takes a plain text p and encodes it to a cipher text by adding the key value to each alphabet character of plain text p using the ASCII chart. The method should return the encoded String. For example, if p = "attack at dawn" and key is 5, it should be encrypted as "fyyfhpefyeifas" because ‘a’+5 = ‘f’, ‘t’+5 = ‘y’, ‘c’+5 = ‘h’, ‘k’+5 = ‘p’, ‘ ‘+5 = ‘e’, ‘d’+5 = ‘i’, ‘w’+5 = ‘a’, and ‘n’+5 = ‘s’.
• a method named decode(String c, int key) that takes a cipher text c and decodes it to a plain text by subtracting the key value from each alphabet character of c. The method should return the encoded String.
Assume that the only characters allowed in Strings p and c are lower-case alphabet letters and spaces. You don’t have to validate input for correct characters, but you should make sure you only pass Strings with all lowercase letters for p and c to your methods.
Provide the following menu using a do..while loop in the main method to the user. Options 1 and 2 in the menu should print the returned encoded or decoded text for the user.
1. Convert plain text to encoded cipher text
2. Convert cipher text to decoded plain text
3. Exit
Java Program:
import java.util.Scanner;
/* You can change class name what you want */
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Encryption <===> Decryption\n");
while(true){
System.out.println("1. Convert plain text to encoded cipher text");
System.out.println("2. Convert cipher text to decoded plain text");
System.out.println("3. Exit");
System.out.print("Enter your choice : ");
int choice = sc.nextInt(); //accept user choice
switch(choice){
case 1:
System.out.print("Enter Plain text : ");
sc.nextLine(); // skips the input line
String p = sc.nextLine().toLowerCase();
System.out.print("Enter Key : ");
int key = sc.nextInt();
encode(p, key);
break;
case 2:
System.out.print("Enter Cipher text : ");
sc.nextLine(); // skips the input line
String c = sc.nextLine().toLowerCase();
System.out.print("Enter Key : ");
int key1 = sc.nextInt();
decode(c, key1);
break;
case 3:
System.out.println("Thank You !!!");
System.exit(0);
default:
System.out.println("Incorrect input!!! Please re-enter choice from our menu");
}
}
}
public static void encode(String p, int key)
{
String cipher = "";
char letter;
for(int i=0; i < p.length(); i++)
{
letter = p.charAt(i); //shift one letter at a time
// if letter lies in between a and z
if(letter >= 'a' && letter <= 'z')
{
// shifting letter
letter = (char) (letter + key);
// if shift letter greater than 'z'
if(letter > 'z') {
// reshift the letter to the starting position
letter = (char) (letter+'a'-'z'-1);
}
cipher = cipher + letter;
}
else {
cipher = cipher + letter;
}
}
System.out.println("Encoded Cipher Text : " + cipher);
}
public static void decode(String c, int key1)
{
String plain = "";
char letter;
for(int i=0; i < c.length(); i++)
{
letter = c.charAt(i); //shift one letter at a time
// if letter lies in between a and z
if(letter >= 'a' && letter <= 'z')
{
// shifting letter
letter = (char) (letter - key1);
// if shift letter less than 'a'
if(letter < 'a') {
// reshift the letter to the starting position
letter = (char) (letter-'a'+'z'+1);
}
plain = plain + letter;
}
else{
plain = plain + letter;
}
}
System.out.println("Decoded Plain Text : " + plain);
}
}
Output:
Thumbs Up Please !!!