Question

Develop a Java application that uses a type of encrypted alphabet, called a random monoalphabetic cipher,...

  • 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.
  • 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.

«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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Write the following code in file EncryptorTester.java

import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;

class MonoAlphaCipher {

String encrypt = "";
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

/*
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
*/

public void createTheEncrypter(String encrypter) {
StringBuilder sb = new StringBuilder();
char ch;
for (int i = 0; i < encrypter.length(); i++) {
ch = encrypter.charAt(i);
if (sb.indexOf(String.valueOf(ch)) == -1) {
sb.append(ch);
}
}
System.out.println("Raw key input: " + encrypter);
System.out.println("Unique character key used: " + sb.toString());
int j = 25;
for (int i = 0; i < 26; i++) {
ch = alphabet.charAt(j);
if (sb.indexOf(String.valueOf(ch)) == -1) {
sb.append(ch);
}
j--;
}
encrypt = sb.toString();
System.out.println("Alphabet and encrypted characters:");
System.out.println(alphabet);
System.out.println(sb.toString());
}

//First create your encrypted alphabetic
public String do_encrypt(String plainText) {
StringBuilder cipher = new StringBuilder();
char ch, new_ch;
int int_ch;
for (int i = 0; i < plainText.length(); i++) {
ch = plainText.charAt(i);
int_ch = ch;
if (ch == ' ') {
cipher.append(' ');
System.out.println("Plaintext byte " + int_ch + "(" + ch + ") was not encoded");

} else {
new_ch = encrypt.charAt(alphabet.indexOf(ch));

cipher.append(new_ch);
System.out.println("Plaintext byte " + int_ch + "(" + ch + ") encoded as " + new_ch);
}

}
System.out.println("Encoding operation complete.");

return cipher.toString();
}

//Do decryption
public String do_decrypt(String cipherText) {
StringBuilder decryptedText = new StringBuilder();
char ch, new_ch;
int int_ch;
for (int i = 0; i < cipherText.length(); i++) {
ch = cipherText.charAt(i);
int_ch = ch;
if (ch == ' ') {
decryptedText.append(' ');
System.out.println("Cyphertext byte " + int_ch + "(" + ch + ") was not decoded");
} else {
new_ch = alphabet.charAt(encrypt.indexOf(ch));
decryptedText.append(new_ch);
System.out.println("Cyphertext byte " + int_ch + "(" + ch + ") decoded as " + new_ch);
}
}

System.out.println("Decoding operation complete.");
return decryptedText.toString();
}

// Read from the text file
public String readFromTextFile(String fileName) {
try {
Scanner reader = new Scanner(new FileReader(fileName));
String line = reader.nextLine();
reader.close();
return line;
} catch (Exception e) {
System.err.format("Error occurred in reading '%s'.", fileName);
e.printStackTrace();
return null;
}
}

// Write to text file
public void writeToTextFile(String fileName, String line) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
writer.write(line);
writer.close();
} catch (Exception e) {
System.err.format("Error occurred in writing '%s'.", fileName);
e.printStackTrace();
}
}

}

// The Main class
public class EncryptorTester {
// The driver function
public static void main(String[] args) {
MonoAlphaCipher myApp = new MonoAlphaCipher();
myApp.createTheEncrypter(myApp.readFromTextFile("keyword.txt"));
String plain_text = myApp.readFromTextFile("input.txt");
String encrypted_text = myApp.do_encrypt(plain_text);
String decrypted_text = myApp.do_decrypt(encrypted_text);
myApp.writeToTextFile("encrypt.txt", encrypted_text);
myApp.writeToTextFile("output.txt", decrypted_text);
System.out.println("Plain Text: " + plain_text);
System.out.println("Encrypted Text: " + encrypted_text);
System.out.println("Decrypted Text: " + decrypted_text);
}
}

keyword.txt and input.txt files are as below

keyword.txt contains only one word and one line as below

TROYONLINE

input.txt contains following text in one line

THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG

The program output will be as below:

$ java EncryptorTester
Raw key input: TROYONLINE
Unique character key used: TROYNLIE
Alphabet and encrypted characters:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
TROYNLIEZXWVUSQPMKJHGFDCBA
Plaintext byte 84(T) encoded as H
Plaintext byte 72(H) encoded as E
Plaintext byte 69(E) encoded as N
Plaintext byte 32( ) was not encoded
Plaintext byte 81(Q) encoded as M
Plaintext byte 85(U) encoded as G
Plaintext byte 73(I) encoded as Z
Plaintext byte 67(C) encoded as O
Plaintext byte 75(K) encoded as W
Plaintext byte 32( ) was not encoded
Plaintext byte 66(B) encoded as R
Plaintext byte 82(R) encoded as K
Plaintext byte 79(O) encoded as Q
Plaintext byte 87(W) encoded as D
Plaintext byte 78(N) encoded as S
Plaintext byte 32( ) was not encoded
Plaintext byte 70(F) encoded as L
Plaintext byte 79(O) encoded as Q
Plaintext byte 88(X) encoded as C
Plaintext byte 32( ) was not encoded
Plaintext byte 74(J) encoded as X
Plaintext byte 85(U) encoded as G
Plaintext byte 77(M) encoded as U
Plaintext byte 80(P) encoded as P
Plaintext byte 69(E) encoded as N
Plaintext byte 68(D) encoded as Y
Plaintext byte 32( ) was not encoded
Plaintext byte 79(O) encoded as Q
Plaintext byte 86(V) encoded as F
Plaintext byte 69(E) encoded as N
Plaintext byte 82(R) encoded as K
Plaintext byte 32( ) was not encoded
Plaintext byte 84(T) encoded as H
Plaintext byte 72(H) encoded as E
Plaintext byte 69(E) encoded as N
Plaintext byte 32( ) was not encoded
Plaintext byte 76(L) encoded as V
Plaintext byte 65(A) encoded as T
Plaintext byte 90(Z) encoded as A
Plaintext byte 89(Y) encoded as B
Plaintext byte 32( ) was not encoded
Plaintext byte 68(D) encoded as Y
Plaintext byte 79(O) encoded as Q
Plaintext byte 71(G) encoded as I
Encoding operation complete.
Cyphertext byte 72(H) decoded as T
Cyphertext byte 69(E) decoded as H
Cyphertext byte 78(N) decoded as E
Cyphertext byte 32( ) was not decoded
Cyphertext byte 77(M) decoded as Q
Cyphertext byte 71(G) decoded as U
Cyphertext byte 90(Z) decoded as I
Cyphertext byte 79(O) decoded as C
Cyphertext byte 87(W) decoded as K
Cyphertext byte 32( ) was not decoded
Cyphertext byte 82(R) decoded as B
Cyphertext byte 75(K) decoded as R
Cyphertext byte 81(Q) decoded as O
Cyphertext byte 68(D) decoded as W
Cyphertext byte 83(S) decoded as N
Cyphertext byte 32( ) was not decoded
Cyphertext byte 76(L) decoded as F
Cyphertext byte 81(Q) decoded as O
Cyphertext byte 67(C) decoded as X
Cyphertext byte 32( ) was not decoded
Cyphertext byte 88(X) decoded as J
Cyphertext byte 71(G) decoded as U
Cyphertext byte 85(U) decoded as M
Cyphertext byte 80(P) decoded as P
Cyphertext byte 78(N) decoded as E
Cyphertext byte 89(Y) decoded as D
Cyphertext byte 32( ) was not decoded
Cyphertext byte 81(Q) decoded as O
Cyphertext byte 70(F) decoded as V
Cyphertext byte 78(N) decoded as E
Cyphertext byte 75(K) decoded as R
Cyphertext byte 32( ) was not decoded
Cyphertext byte 72(H) decoded as T
Cyphertext byte 69(E) decoded as H
Cyphertext byte 78(N) decoded as E
Cyphertext byte 32( ) was not decoded
Cyphertext byte 86(V) decoded as L
Cyphertext byte 84(T) decoded as A
Cyphertext byte 65(A) decoded as Z
Cyphertext byte 66(B) decoded as Y
Cyphertext byte 32( ) was not decoded
Cyphertext byte 89(Y) decoded as D
Cyphertext byte 81(Q) decoded as O
Cyphertext byte 73(I) decoded as G
Decoding operation complete.
Plain Text: THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG
Encrypted Text: HEN MGZOW RKQDS LQC XGUPNY QFNK HEN VTAB YQI
Decrypted Text: THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG
$

The content of output files are as below

encrypt.txt
HEN MGZOW RKQDS LQC XGUPNY QFNK HEN VTAB YQI

output.txt
​​​​​​THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG

Add a comment
Know the answer?
Add Answer to:
Develop a Java application that uses a type of encrypted alphabet, called a random monoalphabetic cipher,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 12.22 Chapter 4: Encrypt Characters Simple Caesar Cipher challenge. You'll need to correct code to print...

    12.22 Chapter 4: Encrypt Characters Simple Caesar Cipher challenge. You'll need to correct code to print ciphertext character correctly. With offset of 3 the output should be ORIGINAL CHARACTERS A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ENCRYPTED CHARACTERS D E F G H I J K L M N O P Q R S T U V W X Y Z...

  • Assignment 7: Caesar Cipher Assignment 7 You will create a Caesar cipher which allows the user...

    Assignment 7: Caesar Cipher Assignment 7 You will create a Caesar cipher which allows the user to specify the key and the text to be encrypted. A Caesar cipher is a simple substitution cipher wherein each letter in the message is shifted a certain number of spaces down the alphabet -- this number is called the key. a b c d e f g h i j k l m n o p q r s t u v w...

  • Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt...

    Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt a message is to use a date’s 6 digits to shift the letters. For example, if a date is picked as December 18, 1946, then the 6 digits are 121846. Assume the dates are in the 20th century. To encrypt a message, you will shift each letter of the message by the number of spaces indicated by the corresponding digit. For example, to encrypt...

  • CODE IN C++ 13.5 Alphabet Histogram Write a program that reads input character by character from...

    CODE IN C++ 13.5 Alphabet Histogram Write a program that reads input character by character from the given data file "data.txt" The program should be case insensitive and count the number of occurances of each character in the alphabet. It should print a histogram of the results as follows: A : B : C : * D : * E : * F : G : H : I : J : * K : L : M : N...

  • Cryptography, the study of secret writing, has been around for a very long time, from simplistic...

    Cryptography, the study of secret writing, has been around for a very long time, from simplistic techniques to sophisticated mathematical techniques. No matter what the form however, there are some underlying things that must be done – encrypt the message and decrypt the encoded message. One of the earliest and simplest methods ever used to encrypt and decrypt messages is called the Caesar cipher method, used by Julius Caesar during the Gallic war. According to this method, letters of the...

  • WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you...

    WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you will write functions to encrypt and decrypt messages using simple substitution ciphers. Your solution MUST include: a function called encode that takes two parameters: key, a 26-character long string that identifies the ciphertext mapping for each letter of the alphabet, in order; plaintext, a string of unspecified length that represents the message to be encoded. encode will return a string representing the ciphertext. a...

  • Java... Write a program that has 4 separate threads. The threads will have the following arrays:...

    Java... Write a program that has 4 separate threads. The threads will have the following arrays: Thread1: A E I M Q U Y Thread2: B F J N R V Z Thread3: C G K O S W ThreadD: D H L P T X Your goal is to synchronize the threads in such a way that they print out all the letters of the alphabet in order.

  • 5) Find a systematic random sample of 8 letters of the alphabet using the following random...

    5) Find a systematic random sample of 8 letters of the alphabet using the following random numbers generated by a calculator: 1st random number = 13, 2nd random number = 9 Use the data set-up in the table below. Put your answer on the line below the table. (8 points) с 4 5 6 7 8 H 91 10 J 11 K 12 L 13 M 14 N 22 V 23 W 24 X 25 Y 26 Z D E...

  • the w 2. This problem explores the use of a one-time pad version of t In...

    the w 2. This problem explores the use of a one-time pad version of t In this scheme, the key is a stream of random numbers between 0 and example, if the key is 3 19 5..., then the first letter of plaintext is encrypted with a shift of 3 letters, the second with a shift of 19 letters, the third with a shift of 5 letters, and so on. a. Encrypt the plaintext sendmoremoney with the key stream 9...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT