First, we consider a particularly simple implementation of a simple substitution
ID: 3795796 • Letter: F
Question
First, we consider a particularly simple implementation of a simple substitution cipher. In the simplest case, the message is encrypted by substituting the letter of the alphabet n places ahead of the current letter. For example, with n = 3, the substitution mdash which acts as the key mdash is plaintext: abcdefghijklmnopqrstuvwxyz ciphertext: DEFGHIJKLMNOPQRSTUVWXYZABC where we've followed the convention that the plaintext is lowercase, and the ciphertext is uppercase. In this example, the key could be given succinctly as "3" since the amount of the shift is, in effect, the key. Using the key 3, we can encrypt the plaintext message fourscoreandsevenyearsago by looking up each plaintext letter in the table above and then substituting the corresponding letter in the ciphertext row, or by simply replacing each letter by the letter that is three positions ahead of it in the alphabet. For the particular plaintext in (2.1), the resulting ciphertext is IRXUVFRUHDAGVHYHABHDUVDIR. To decrypt this simple substitution, we look up the ciphertext letter in the ciphertext row and replace it with the corresponding letter in the plaintext row, or we can shift each ciphertext letter backward by three. The simple substitution with a shift of three is known as the Caesar's cipher.^3^3Historians generally agree that the Caesar's cipher was named after the Roman dictator, not the salad.Explanation / Answer
Pleasr Note that output given in the above text is not correct.My program's output is correct.
Program:
import java.util.Scanner;
public class Cipher {
public static void main(String[] args) {
int offset; // The "key" for the encoding, a number from 1 to 25.
String plainText; // The original string that is to be encoded.
String cipherText; // The coded version of the string.
Scanner s = new Scanner(System.in);
do {
System.out.println();
System.out.print("Enter the key, a number from 1 to 25: ");
offset = s.nextInt();
if (offset < 0 || offset > 25)
System.out.println("Please enter a number in the correct range!");
} while (offset < 0 || offset > 25);
System.out.println();
System.out.print("Enter your plaintext: ");
plainText = s.next();
plainText = plainText.toLowerCase(); // convert to lower case
cipherText = "";
int i;
for (i = 0; i < plainText.length(); i++) {
char plainChar, encodedChar;
plainChar = plainText.charAt(i);
if (plainChar < 'a' || plainChar > 'z')
cipherText += plainChar; // it's not a letter; don't encode it.
else {
encodedChar = encode(plainChar, offset);
cipherText += encodedChar;
}
}
System.out.println();
System.out.println("Encoded text: " + cipherText);
plainText = ""; // to be reconstructed from cipher Text
for (i = 0; i < cipherText.length(); i++) {
char plainChar, encodedChar;
encodedChar = cipherText.charAt(i);
if (encodedChar < 'A' || encodedChar > 'Z')
plainText += encodedChar; // it's not a letter; don't decode it.
else {
plainChar = decode(encodedChar, offset);
plainText += plainChar;
}
}
System.out.println("Decoded text: " + plainText);
System.out.println();
}
private static char encode(char ch, int offset) {
int chCode;
char encodedChar;
chCode = (int) ch - (int) 'a'; // convert ch to a number from 0 to 25
chCode = chCode + offset; // add the offset to the character code
if (chCode >= 26) { // wrap around to the beginning of the alphabet
chCode = chCode - 26;
}
encodedChar = (char) (chCode + (int) 'A'); // convert back to a letter
return encodedChar;
}
private static char decode(char ch, int offset) {
int chCode;
char decodedChar;
chCode = (int) ch - (int) 'A'; // convert ch to a number from 0 to 25
chCode -= offset; // subtract the offset from the character code
if (chCode < 0) { // wrap around to the end of the alphabet
chCode = chCode + 26;
}
decodedChar = (char) (chCode + (int) 'a'); // convert back to a letter
return decodedChar;
}
}
Output:
Enter the key, a number from 1 to 25: 3
Enter your plaintext: fourscoreandsevenyearsago
Encoded text: IRXUVFRUHDQGVHYHQBHDUVDJR
Decoded text: fourscoreandsevenyearsago
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.