Develop a Java application that uses a type of encrypted alphabet, called a rand
ID: 3912645 • Letter: D
Question
Develop a Java application that uses a type of encrypted alphabet, called a random monoalphabetic cipher, to encrypt and decrypt a message. Your encryption key and message are to be read in from two different files and then the encrypted message will be output to third file. Then the encrypted message is read in and decrypted to a fourth file.
How this encryption algorithm works is described fully below. You don’t need to research encryption algorithms beyond the following description.
A monoalphabetic cipher starts with an encryption word, removes the redundant letters from the word, and assigns what’s left to create an encrypted alphabet such that the first encrypted alphabet letter corresponds to A, the next to B, etc. Then the unused letters in the encryption word are then assigned to correspond to alphabetic letters starting with Z.
For example, if the encryption word is FEATHER, the non-redundant letters are F, E, A, T, H, and R. These are assigned to A through F like this:
Original alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encrypted alphabet:FEATHR
The letters of the alphabet not in the set of F, E, A, T, H, and R are then assigned to the other letters of the alphabet starting with Z and working back towards A to form a full alphabetic cipher like this:
Original alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encrypted alphabet:FEATHRZYXWVUSQPONMLKJIGDCB
For this program you must use the encryption word “TROYONLINE” to encrypt your text.
This program uses 4 total files. Use file “keyword.txt” to hold your encryption key word, file “input.txt” as your input plain text file, “encrypt.txt” as your encrypted output file, and file “output.txt” as your decrypted output file. Use Notepad to create the keyword.txt and input.txt files.
First create your encrypted alphabetic and then encrypt the following sentence:
THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG
Just encrypt and decrypt upper case letters to other upper case letters. Don’t worry about lower case letters and encrypting the spaces.
A sample of the four input and output files and example program output using the encryption word “FEATHER” is shown below. Using this keyword to test your program is a good idea as you should get the same outputs as shown in this example.
Here’s an example of the output from this example program with debugging statements added.
«M ----jGRASP exec: java EncryptorTester
MM§MRaw key input: FEATHER
MM§MUnique character key used: FEATHR
MM§MAlphabet and encrypted characters:
MM§MABCDEFGHIJKLMNOPQRSTUVWXYZ
MM§MFEATHRZYXWVUSQPONMLKJIGDCB
MM§MPlaintext byte 84 (T) encoded as K
MM§MPlaintext byte 72 (H) encoded as Y
MM§MPlaintext byte 69 (E) encoded as H
MM§MPlaintext byte 32 ( ) was not encoded
MM§MPlaintext byte 81 (Q) encoded as N
MM§MPlaintext byte 85 (U) encoded as J
MM§MPlaintext byte 73 (I) encoded as X
MM§MPlaintext byte 67 (C) encoded as A
MM§MPlaintext byte 75 (K) encoded as V
MM§MPlaintext byte 32 ( ) was not encoded
MM§MPlaintext byte 66 (B) encoded as E
MM§MPlaintext byte 82 (R) encoded as M
MM§MPlaintext byte 79 (O) encoded as P
MM§MPlaintext byte 87 (W) encoded as G
MM§MPlaintext byte 78 (N) encoded as Q
MM§MPlaintext byte 32 ( ) was not encoded
MM§MPlaintext byte 70 (F) encoded as R
MM§MPlaintext byte 79 (O) encoded as P
MM§MPlaintext byte 88 (X) encoded as D
MM§MPlaintext byte 32 ( ) was not encoded
MM§MPlaintext byte 74 (J) encoded as W
MM§MPlaintext byte 85 (U) encoded as J
MM§MPlaintext byte 77 (M) encoded as S
MM§MPlaintext byte 80 (P) encoded as O
MM§MPlaintext byte 69 (E) encoded as H
MM§MPlaintext byte 68 (D) encoded as T
MM§MPlaintext byte 32 ( ) was not encoded
MM§MPlaintext byte 79 (O) encoded as P
MM§MPlaintext byte 86 (V) encoded as I
MM§MPlaintext byte 69 (E) encoded as H
MM§MPlaintext byte 82 (R) encoded as M
MM§MPlaintext byte 32 ( ) was not encoded
MM§MPlaintext byte 84 (T) encoded as K
MM§MPlaintext byte 72 (H) encoded as Y
MM§MPlaintext byte 69 (E) encoded as H
MM§MPlaintext byte 32 ( ) was not encoded
MM§MPlaintext byte 76 (L) encoded as U
MM§MPlaintext byte 65 (A) encoded as F
MM§MPlaintext byte 90 (Z) encoded as B
MM§MPlaintext byte 89 (Y) encoded as C
MM§MPlaintext byte 32 ( ) was not encoded
MM§MPlaintext byte 68 (D) encoded as T
MM§MPlaintext byte 79 (O) encoded as P
MM§MPlaintext byte 71 (G) encoded as Z
MM§MCyphertext byte 75 (K) decoded as T
MM§MCyphertext byte 89 (Y) decoded as H
MM§MCyphertext byte 72 (H) decoded as E
MM§MCyphertext byte 32 ( ) was not decoded
MM§MCyphertext byte 78 (N) decoded as Q
MM§MCyphertext byte 74 (J) decoded as U
MM§MCyphertext byte 88 (X) decoded as I
MM§MCyphertext byte 65 (A) decoded as C
MM§MCyphertext byte 86 (V) decoded as K
MM§MCyphertext byte 32 ( ) was not decoded
MM§MCyphertext byte 69 (E) decoded as B
MM§MCyphertext byte 77 (M) decoded as R
MM§MCyphertext byte 80 (P) decoded as O
MM§MCyphertext byte 71 (G) decoded as W
MM§MCyphertext byte 81 (Q) decoded as N
MM§MCyphertext byte 32 ( ) was not decoded
MM§MCyphertext byte 82 (R) decoded as F
MM§MCyphertext byte 80 (P) decoded as O
MM§MCyphertext byte 68 (D) decoded as X
MM§MCyphertext byte 32 ( ) was not decoded
MM§MCyphertext byte 87 (W) decoded as J
MM§MCyphertext byte 74 (J) decoded as U
MM§MCyphertext byte 83 (S) decoded as M
MM§MCyphertext byte 79 (O) decoded as P
MM§MCyphertext byte 72 (H) decoded as E
MM§MCyphertext byte 84 (T) decoded as D
MM§MCyphertext byte 32 ( ) was not decoded
MM§MCyphertext byte 80 (P) decoded as O
MM§MCyphertext byte 73 (I) decoded as V
MM§MCyphertext byte 72 (H) decoded as E
MM§MCyphertext byte 77 (M) decoded as R
MM§MCyphertext byte 32 ( ) was not decoded
MM§MCyphertext byte 75 (K) decoded as T
MM§MCyphertext byte 89 (Y) decoded as H
MM§MCyphertext byte 72 (H) decoded as E
MM§MCyphertext byte 32 ( ) was not decoded
MM§MCyphertext byte 85 (U) decoded as L
MM§MCyphertext byte 70 (F) decoded as A
MM§MCyphertext byte 66 (B) decoded as Z
MM§MCyphertext byte 67 (C) decoded as Y
MM§MCyphertext byte 32 ( ) was not decoded
MM§MCyphertext byte 84 (T) decoded as D
MM§MCyphertext byte 80 (P) decoded as O
MM§MCyphertext byte 90 (Z) decoded as G
MM§M
MM©M ----jGRASP: operation complete.
This program used class Encryptor to perform the encryption and decryption and class EntryptorTester to handle all the file input and output and pass and receive text from the Encryptor class.
This program was taken from Programming Exercise P19.1 on page 796 of Chapter 19 of Cay Horstmann’s “Big Java (4th Edition)”. This reference is only included to cite the source of the program. You do not need to obtain this book!
??? ? | keyword.txt-WordPad File Home View Find ac Replace Cut Courier New Copy Paste Picture Paint Date and Insert ? drawing time object :D Select all Clipboard Font Paragraph Insert Editing THER 100% (Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class MonoAlphabeticCipher {
// File names
private static final String INPUT_FILE = "input.txt";
private static final String KEYWORD_FILE = "keyword.txt";
private static final String ENCRYPT_FILE = "encrypt.txt";
private static final String DECRYPT_FILE = "output.txt";
private static final int MAX_LETTERS = 26; // For the uppercase alphabets
// Instance variable
private String encoder;
private String decoder;
/**
* Constructor
*/
public MonoAlphabeticCipher() {
// Get key
String key = getKey();
// Generate encoder
genEncoder(key);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.