This repository has been archived by the owner on Oct 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubCipher.java
79 lines (65 loc) · 2.47 KB
/
SubCipher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.lang.*;
import java.io.*;
import java.util.*;
public class SubCipher {
public static String chars = "abcdefghijklmnopqrstuvwxyz";
public static String cryptList = "kngcadsxbvfhjtiumylzqropwe";
public static void main(String args[]) {
Console console = System.console();
System.out.println("Select an option\n1. Decrypt a file\n2. Encrypt a file\n");
String input = console.readLine("1 or 2; ");
int input1 = Integer.parseInt(input);
String inputFileName = console.readLine("Enter input file name: ");
String outputFileName = console.readLine("Enter output file name: ");
if(input1 == 1) {
decrypt(inputFileName, outputFileName);
}
if(input1 == 2) {
encrypt(inputFileName, outputFileName);
}
}
public static void decrypt(String inputFile, String outputFile) {
try {
BufferedReader br = new BufferedReader(new FileReader("cipher.txt"));
FileWriter fw = new FileWriter("output.txt");
String line = null;
while ((line = br.readLine()) != null) {
String newLine = "";
for(int i = 0; i < line.length(); i++) {
char in = line.charAt(i);
int index = chars.indexOf(in);
char out = cryptList.charAt(index);
newLine += out;
}
fw.write(newLine);
}
br.close();
fw.close();
}
catch(Exception e){
System.out.println("An unknown error occurred, please try again");
}
}
public static void encrypt(String inputFile, String outputFile) {
try {
BufferedReader br = new BufferedReader(new FileReader(inputFile));
FileWriter fw = new FileWriter(outputFile);
String line = null;
while ((line = br.readLine()) != null) {
String newLine = "";
for(int i = 0; i < line.length(); i++) {
char in = line.charAt(i);
int index = cryptList.indexOf(in);
char out = chars.charAt(index);
newLine += out;
}
fw.write(newLine);
}
br.close();
fw.close();
}
catch(Exception e){
System.out.println("An unknown error occurred, please try again");
}
}
}