Note** this question has already been answered on this site, but I need to do th
ID: 3760303 • Letter: N
Question
Note** this question has already been answered on this site, but I need to do this without the use of the command line. Can anybody help me out? Code should be in JAVA
Random monoalphabet cipher. The Caesar cipher, which shifts all letters by a fixed amount, is far too easy to crack. Here is a better idea. For the key, don't use numbers but words. Suppose the key word is FEATHER. Then first remove duplicate letters, yielding FEATHR, and append the other letters of the alphabet in reverse order. Now encrypt the letters as follows:
Write a program that encrypts or decrypts a file using this cipher. The key word is specified with the -k command line option. The -d command line option specifies decryption. For example:
decrypts a file using the keyword FEATHER. It is an error not to supply a keyword.
Explanation / Answer
import java.io.*;
import java.util.*;
class main{
public static int[] map = {5,4,0,19,7,17,25,24,23,22,21,20,18,16,15,14,13,12,10,9,8,7,6,3};
public static String encrypt(String value,String key){
value = key + value.reverse();
String res = "";
for (int i = 0; i < value.length(); i++)
res += (char)(97+map[value.charAt(i) - 97]);
return res;
}
public static void main(String[] args){
boolean[] l = new boolean[26];
String key = "";
for (int i = 0; i < args[2].length; i++){
if (l[args[2].charAt(i) - 97] == false){
key += args[2].charAt(i);
l[args[2].charAt(i) - 97] = true;
}
}
BufferedReader br = null;
try {
String line;
br = new BufferedReader(new FileReader(args[3]));
File file = new File(args[4]);
// if file doesnt exists, then create it
if (!file.exists())
file.createNewFile();
while ((line = br.readLine()) != null){
String[] all = line.split(' ');
for (int i = 0; i < all.length; i++)
all[i] = encrypt(all[i],key);
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i < all.length; i++){
bw.write(all[i]+" ");
}
br.write(' ');
}
bw.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.