Need Help making this work when user is asked for what text file and the rotatio
ID: 642480 • Letter: N
Question
Need Help making this work when user is asked for what text file and the rotational cipher key(an integer number). I need multiple methods and need to have this program completed in an hour. Please help and hurry. Here is all information i have.
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
Scanner keyboard = new Scanner (System.in);
String filename = ("C:\Users\Dylan\Documents\CS1180\message.txt");
File file=new File(filename);
if(!file.exists()){
System.out.println("Input File Does not exists");
return;
}//program exits or crashes if input file does not exist
//reading from file
String originalText="";
Scanner scanner=new Scanner(file);
while(scanner.hasNextLine()){
originalText+=scanner.nextLine()+" ";
}
scanner.close();
String message = "";
int cyperKey;
System.out.print("Enter name of the text file to encrypt: ");
message = keyboard.next();
System.out.println("Enter the rotational cipher key(an integer number): ");
cyperKey = keyboard.nextInt();
//printing file content
System.out.println("original content");
System.out.println(originalText);
String cipherText="";
System.out.println("Cipher file contents");
//printing cipher content
for(int i=0;i<originalText.length();i++)
{
char letter=originalText.charAt(i);
cipherText+=++letter;
}
System.out.println(cipherText);
//writing to another file
System.out.println(filename.replace(".", "-rot."));
file=new File(filename.replace(".", "-rot."));
if(!file.exists())
file.createNewFile();
System.out.println(file.getAbsolutePath());
FileWriter writer=new FileWriter(file);
writer.write(cipherText);
writer.close();
}//end of method main
}//end of class Rotational Cipher
Here is what the guidlines are for this program.
you are to create a text file (such as message.txt) that contains a brief text message of your choice (50-100 words). Your program is read the file, show the contents of the file to the user (print the contents of the file to the console), and then ask the user for an integer number to use as a rot-n encryption key.
rot-n encryption: Rotational ciphers are one of the oldest (and easiest to break) encryption techniques. They are a form of substitution cipher where each character is replaced with a character/letter that is a fixed number of positions away in alphabetic location. For at Rot1 encryption of a set of symbols consisting of just the lowercase letters, for example, the letter a would be replaced with the letter b, the letter e would be replaced by the letter f, and the letter z would be replaced by the letter a. The encrypted message would be decrypted by reversing the process using a Rot negative 1.
Your program will be performing a rotational cipher over the entire set of UNICODE characters. Adjusting the value of each character in the message (by adding/subtracting the provided integer key) and output the results to BOTH the console and a file. Use the same file name as the input file, but append -rot to the base file name. For example, message.txt would be encrypted as message-rot.txt.
Your program should use methods for decomposition for major program tasks, including methods for:
- obtaining the file name from the user
- obtaining the rotational key from the user
- encrypting a string
- printing a file to console
For this program, you may assume that the user enters perfect input. They will correctly enter the name of the file and an appropriate integer key, the file will exist, there will be space and permission on the hard drive to open and write the output, etc. If the user fails to enter in valid inputs, your program is allowed to crash. Note, however, that future programs may soon have the expectation that you deal with run time exceptions for this sort in a more elegant way.
Be sure you write both javadoc (including method description, pre, and post condition descriptions) as well as internal documentation which describe your programs parameters, return values, and logic.
Hint: Use a file name that ends in .txt so that you can check your files contents with notepad. If you do not specify a file path your file can be found in your project folder at the same level as the folder containing your programs src folder.
Sample output is shown below
Enter name of the text file to encrypt: message.txt
Enter the rotational cipher key (an integer number): 1
Original file contents:
Rotational ciphers are one of the oldest (and easiest
to break) encryption techniques. They are a form of
substitution cipher where each character is replaced
with a character/letter that is a fixed number of
positions away in alphabetric location.
Ciphered file contents:
Spubujpobm!djqifst!bsf!pof!pg!uif!pmeftu!)boe!fbtjftu
up!csfbl*!fodszqujpo!ufdiojrvft/!!Uifz!bsf!b!gpsn!pg!
tvctujuvujpo!djqifs!xifsf!fbdi!dibsbdufs!jt!sfqmbdfe!
xjui!b!dibsbdufs0mfuufs!uibu!jt!b!gjyfe!ovncfs!pg!
qptjujpot!bxbz!jo!bmqibcfusjd!mpdbujpo/
Ensure that you can both encrypt and decrypt messages using inverse rotational cipher keys. Save your lab code by creating a zip file of the entire project.
rot-n encryption: Rotational ciphers are one of the oldest (and easiest to break) encryption techniques. They are a form of substitution cipher where each character is replaced with a character/letter that is a fixed number of positions away in alphabetic location. For at Rot1 encryption of a set of symbols consisting of just the lowercase letters, for example, the letter a would be replaced with the letter b, the letter e would be replaced by the letter f, and the letter z would be replaced by the letter a. The encrypted message would be decrypted by reversing the process using a Rot negative 1.
Explanation / Answer
import java.util.Scanner;
import java.io.*;
public class Rot_Cipher {
public static String readTextFromFile(String fileName) throws IOException {
File inputFile = new File(fileName);
Scanner input = new Scanner(inputFile);
String res = "";
while (input.hasNextLine()) {
res += input.nextLine();
}
return res;
}
public static String encodeCaesar(String str, int key) {
String res = "";
for (int i = 0; i < str.length(); ++i) {
// get character at position i
char chr = str.charAt(i);
char encoded_chr;
if ('a' <= chr && chr <= 'z') {
encoded_chr = (char)((chr - 'a' + key + 26) % 26 + 'a');
}
else if ('A' <= chr && chr <= 'Z') {
encoded_chr = (char)((chr - 'A' + key + 26) % 26 + 'A');
}
else
encoded_chr = chr; // no modification
res += encoded_chr;
}
return res;
}
public static double getEntropy(String str) {
double[] freq = {0.08167, 0.01492, 0.02782, 0.04253, 0.12702, 0.02228,
0.02015, 0.06094, 0.06966, 0.00153, 0.00772, 0.04025,
0.02406, 0.06749, 0.07507, 0.01929, 0.00095, 0.05987,
0.06327, 0.09056, 0.02758, 0.00978, 0.02360, 0.00150,
0.01974,0.00074};
double res = 0;
for (int i = 0; i < str.length(); ++i) {
char ch = str.charAt(i);
if ('a' <= ch && ch <= 'z')
res += -Math.log(freq[ch - 'a']);
else if ('A' <= ch && ch <= 'Z')
res += -Math.log(freq[ch - 'A']);
}
return res;
}
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.out.println("Wrong number of parameters. Usage: java Caesar <input file> <output file>");
return;
}
String text = readTextFromFile(args[0]);
double lowestEntropy = Double.MAX_VALUE;
String lowestEntropyString = "";
for (int key = 0; key < 26; ++key) {
String decodedText = encodeCaesar(text, -key);
double entropy = getEntropy(decodedText);
if (entropy < lowestEntropy) {
lowestEntropy = entropy;
lowestEntropyString = decodedText;
}
}
File outputFile = new File(args[1]);
BufferedWriter outputWriter =
new BufferedWriter(new FileWriter(outputFile.getAbsoluteFile()));
outputWriter.write(lowestEntropyString);
outputWriter.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.