In: Computer Science
Programing Language: Java
The Problem:
You are writing a program that encrypts or decrypts messages using
a simple substitution
cipher. Your program will use two constant strings. One will
represent the code for
encryption: going from the original message (called the plaintext)
to the encrypted
version of the message. The other will be
“abcdefghijklmnopqrstuvwxyz” (the lowercase
alphabet.
Your program will ask the user whether they want to 1) encrypt a
message, 2) decrypt a
message, or 3) quit.
If they choose encrypt or decrypt, you will then get the message
from the user and change
it to all lower case. Then you will print out the encrypted or
decrypted message character
by character, using the information in the code to be used for
encryption or decryption.
The String you will use for encryption is
“bopcardxshewityjlqfguvzmkn”.
How the substitution code works:
With a substitution encryption, we select a substitute letter for
each letter. Our encryption
String has the substitute for ‘a’ at the first index (0) and the
substitute for ‘b’ at the next
index (1) and so on up to the substitute for ‘z’ at the last index
(25). So what we will do is
to look at the letter we want to encrypt, determine the index that
corresponds to that letter
using our alphabet string (and indexOf()) and then get the
character at that index from the
encryption code.
Decryption will be similar except that we will get the index of the
letter in the encryption
string and then get the character at that index from the alphabet
string.
We will only substitute letters. All other characters will get
passed on as is.
Sample Program Run (user input in bold):
Welcome to the Normal Encrypter!
You may:
1) Encrypt a message
1
2) Decrypt a message
3) Quit
Enter choice (1, 2, or 3): 1
Enter the message: Hello, world!
Your encrypted message is:
auwwr, jrvwm!
You may:
1) Encrypt a message
2) Decrypt a message
3) Quit
Enter choice (1, 2, or 3): 2
Enter the message: Auwwr, jrvwm!
Your decrypted message is:
Hello, world!
You may:
1) Encrypt a message
2) Decrypt a message
3) Quit
Enter choice (1, 2, or 3): 3
Thank you for using the Normal Encrypter!
Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
String plaintext= "abcdefghijklmnopqrstuvwxyz";
String encrypt="bopcardxshewityjlqfguvzmkn";
System.out.print("Welcome to the Normal
Encrypter!\nYou may:\n1) Encrypt a message\n2) Decrypt a
message\n3) Quit\nEnter choice (1, 2 or 3):");
n=Integer.parseInt(sc.nextLine());
while(n!=3)
{
if(n == 1 || n == 2)
{
System.out.print("Enter the message:");
String s=sc.nextLine();
int l=s.length();
String text="";
if(n == 1)
{
s=s.toLowerCase();
for(int i = 0;i < l; i++)
{
char p=s.charAt(i);
if(Character.isLetter(p))
text = text +
encrypt.charAt(plaintext.indexOf(p));
else
text=text+p;
}
System.out.println("Your decrypted message
is:\n"+text);
}
else if(n == 2)
{
for(int i = 0;i < l; i++)
{
char p=s.charAt(i);
if(Character.isLetter(p))
{
if(Character.isUpperCase(p))
text = text +
Character.toUpperCase(plaintext.charAt(encrypt.indexOf(Character.toLowerCase(p))));
else
text = text +
plaintext.charAt(encrypt.indexOf(p));
}
else
text=text+p;
}
System.out.println("Your encrypted message
is:\n"+text);
}
}
else
System.out.println("Invalid choice");
System.out.print("You may:\n1) Encrypt a message\n2)
Decrypt a message\n3) Quit\nEnter choice (1, 2 or 3):");
n=Integer.parseInt(sc.nextLine());
}
System.out.println("Thank you for using the Normal
Encrypter!");
}
}
Please refer to the screenshot of the code to understand the indentation of the code:
Output:
Here the encryption key given in the question and the encryption key used for Sample Program Run are not same, so the answer varies.
For any doubt or question comment below.