Hi guys here is my assignment for a brief history.
Project 9
Random Monoalphabet Cipher
Random monoalphabet cipher. The Caesar cipher, which shifts all letters by a fixed amount, is ridiculously easy to crack - just try out all 25 possible keys. Here=s a better idea . For the key, don=t use numbers but words. Suppose the key word is FEATHER. First remove the duplicate letters, yielding FEATHR, and append the other letters of the alphabet in reverse order. Now encrypt the letters as follows
A = F
B = E
C = A
D = T
E = H
F = R
G = Z
H = Y
I = X
et etc
Write a program that encrypts or decrypts a file using this cipher. For example, decrypt a file using the keyword FEATHER. It is an error not to supply a keyword.
You will prepare a file encrypted using this keyword cipher. Make the keyword the first word
in the file. You will give your input file to me and I will pass it to another student so that s/he can decrypt it. You must also be prepared to decrypt the file s/he hands you, using this cipher.
You are obviously forbidden to use FEATHER as your keyword for the beta-testing.
Please name the data files for your program TEST1, TEST2, etc.. This is make beta-testing run more smoothly.
Assume that each file will contain only alphabetic characters and spaces between words.
You are not to accommodate digits, punctuation or any other special characters in your program code.
Here is my code.
import java.util.*; import java.io.*; public class Project9 { public static String encrypt(String msg, String cw) { String encryptionMessage = new String(); msg = msg.toUpperCase(); cw = cw.toUpperCase(); int i; for( i = 0; i < msg.length(); i++) { char ch = msg.charAt(i); int shift = (cw.charAt( i% cw.length() )-'A'); int oldPositionInAlphabet = ch - 'A'; int newPositionInAlphabet = (oldPositionInAlphabet + shift)%26; encryptionMessage = encryptionMessage + (char)(newPositionInAlphabet+ 'A'); } return encryptionMessage; } public static void main (String[] args) throws IOException { Scanner scan = new Scanner(System.in); String unencryptedFile; System.out.print( "Please input your Unencrypted file: " ); unencryptedFile = scan.next(); File inputFile = new File(unencryptedFile); Scanner scanOne = new Scanner(inputFile); if(!inputFile.exists()) { System.out.println("This file does not exist!"); System.exit(0); } String codeWord; System.out.println( "Please enter your code word. " ); codeWord = scan.nextLine(); File outFile = new File(unencryptedFile); PrintWriter pw = new PrintWriter(outFile); while(outFile.exists()) { String line = scanOne.nextLine(); String encryptedLine = encrypt(line,codeWord); pw.println(encryptedLine); pw.close(); } scanOne.close(); } }
For some reason I keep getting an error message pertaining to my data file. I don't know what else to do. Here is input and error message below.
Please input your Unencrypted file: TEST.dat
java.io.FileNotFoundException: TEST.dat (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.jav a:138)
at java.util.Scanner.<init>(Scanner.java:656)
at Project9.main(Project9.java:42)
Does anyone have any suggestions?