-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
executable file
·116 lines (105 loc) · 4.97 KB
/
App.java
File metadata and controls
executable file
·116 lines (105 loc) · 4.97 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.io.*;
import java.nio.channels.FileChannel;
/**
* App for Decryption of ciphertexts, using main() method.
* Provides static outputFile() method to generate a TXT file with the decrypted plaintext of a given Cipher.
* Any description included in these files is retained between consecutive runs of the main() method.
*
* @author David W. Arnold
* @version 09/11/2019
*/
public class App
{
public static void main(String[] args) throws IOException
{
File tess26 = new File("." + File.separator + "tess26.txt");
File tess27 = new File("." + File.separator + "tess27.txt");
// Exercise 1
File cexercise1 = new File("." + File.separator + "ciphertexts" + File.separator + "cexercise1.txt");
CaesarDecrypt caesar = new CaesarDecrypt(tess26, cexercise1);
outputFile(caesar.decrypt(), genPlaintextFileName(cexercise1));
// Exercise 2
File cexercise2 = new File("." + File.separator + "ciphertexts" + File.separator + "cexercise2.txt");
VigDecrypt vig1 = new VigDecrypt(tess26, cexercise2);
outputFile(vig1.decrypt("TESSOFTHEDURBERVILLES"), genPlaintextFileName(cexercise2));
// Exercise 3
File cexercise3 = new File("." + File.separator + "ciphertexts" + File.separator + "cexercise3.txt");
VigDecrypt vig2 = new VigDecrypt(tess26, cexercise3);
outputFile(vig2.decrypt(6), genPlaintextFileName(cexercise3));
// Exercise 4
File cexercise4 = new File("." + File.separator + "ciphertexts" + File.separator + "cexercise4.txt");
VigDecrypt vig3 = new VigDecrypt(tess26, cexercise4);
outputFile(vig3.decrypt(4, 6), genPlaintextFileName(cexercise4));
// Exercise 5
File cexercise5 = new File("." + File.separator + "ciphertexts" + File.separator + "cexercise5.txt");
TranspositionDecrypt transpositionDecrypt1 = new TranspositionDecrypt(tess26, cexercise5);
outputFile(transpositionDecrypt1.decrypt(4, 6), genPlaintextFileName(cexercise5));
// Exercise 6
File cexercise6 = new File("." + File.separator + "ciphertexts" + File.separator + "cexercise6.txt");
TranspositionDecrypt transpositionDecrypt2 = new TranspositionDecrypt(tess26, cexercise6);
outputFile(transpositionDecrypt2.decrypt(6), genPlaintextFileName(cexercise6));
// Exercise 7
File cexercise7 = new File("." + File.separator + "ciphertexts" + File.separator + "cexercise7.txt");
GenSubDecrypt genSubDecrypt = new GenSubDecrypt(tess27, cexercise7);
outputFile(genSubDecrypt.decrypt(), genPlaintextFileName(cexercise7));
}
private static void outputFile(String plaintext, String name) throws IOException
{
File outputDirectory = new File("." + File.separator + "outputs");
String questionNumber = name.substring(name.length() - 5).substring(0, 1);
if (!outputDirectory.exists()) {
outputDirectory.mkdir();
}
File outputFile = new File("." + File.separator + "outputs" + File.separator + name);
if (outputFile.exists()) {
File tmpFile = new File("." + File.separator + "outputs" + File.separator + "tmp.txt");
copyFile(outputFile, tmpFile);
outputFile.delete();
BufferedWriter out = new BufferedWriter(new FileWriter(outputFile, true));
out.write(plaintext.substring(0, 30) + "\n");
out.write("\n" + "Full Decrypted Plaintext for Exercise " + questionNumber + ": " + plaintext + "\n");
String currentLine;
BufferedReader tmp = new BufferedReader(new FileReader(tmpFile));
int index = 1;
while ((currentLine = tmp.readLine()) != null) {
if (index > 4) {
out.write("\n" + currentLine);
}
index++;
}
out.close();
tmpFile.delete();
} else {
BufferedWriter out = new BufferedWriter(new FileWriter(outputFile, true));
out.write(plaintext.substring(0, 30) + "\n");
out.write("\n" + "Full Decrypted Plaintext for Exercise " + questionNumber + ": " + plaintext + "\n");
out.close();
}
}
private static void copyFile(File sourceFile, File destFile) throws IOException
{
if (!sourceFile.exists()) {
return;
}
if (!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source;
FileChannel destination;
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
if (destination != null && source != null) {
destination.transferFrom(source, 0, source.size());
}
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
private static String genPlaintextFileName(File file)
{
return file.getName().substring(1);
}
}