-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomp3260_A2.java
182 lines (154 loc) · 6.86 KB
/
comp3260_A2.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Authors: Hamish Broadhurst-Tynan (3238465) & Brock Brinkworth (3331952)
Purpose: This class is main and the entry point for the program
*/
import java.io.File;
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class comp3260_A2
{
public static void main(String args[])
{
String p = "";
String nP = "";
String k = "";
String nK = "";
//Attempts to read the Encryption input file, and exits out if it doesn't.
//Reads each line as P, P', K, K' respectively
try
{
File inFile = new File("Encrypt-Input.txt");
Scanner fReader = new Scanner(inFile);
while(fReader.hasNextLine()){
p = fReader.nextLine();
nP = fReader.nextLine();
k = fReader.nextLine();
nK = fReader.nextLine();
}
fReader.close();
}
catch (FileNotFoundException e)
{
System.out.println("File not found...\nExiting program...");
return;
}
//Begins making the header for the output string
String header = "Avalanche Demonstration\n";
header += "Plaintext P: " + p + "\n";
header += "Plaintext P': " + nP + "\n";
header += "Key K: " + k + "\n";
header += "Key K': " + nK + "\n";
//Removes all whitespaces in the strings for easier manipulation going forward
p = p.replace(" ", "");
nP = nP.replace(" ", "");
k = k.replace(" ", "");
nK = nK.replace(" ", "");
//Creates the P & P' under K and P under K & K' avalanche tables
String table1 = "\nP and P' under K\n";
String table2 = "\nP under K and K'\n";
//Starts timer on DES for analysis
long start = System.currentTimeMillis();
//Creates a DES0 obj called DES0, DES0 is the default version of DES
//Takes a name, p, p', k,k' as parameters
DES0 des0 = new DES0("DES0", p, nP, k, nK);
//Adds details to the relevant tables
table1 += "Ciphertext C: " + des0.getCPK() + "\n";
table1 += "Ciphertext C': " + des0.getCNPK() + "\n";
table2 += "Ciphertext C: " + des0.getCPK() + "\n";
table2 += "Ciphertext C': " + des0.getCPNK() + "\n";
//Same as DES0 constructors, modified DES algorithims as outlined in the assginment spec
DES1 des1 = new DES1("DES1", p, nP, k, nK);
DES2 des2 = new DES2("DES2", p, nP, k, nK);
DES3 des3 = new DES3("DES3", p, nP, k, nK);
//Formats and adds details about the avalanche effect to relevant tables
table1 += "Round " + des0.getName() + " " + des1.getName() + " " + des2.getName() + " " + des3.getName() + "\n";
for(int i = 0; i < 17; i++){
table1 += String.format("%5d %4d %4d %4d %4d", i, des0.compareP().get(i), des1.compareP().get(i), des2.compareP().get(i), des3.compareP().get(i)) + "\n";
}
table2 += "Round " + des0.getName() + " " + des1.getName() + " " + des2.getName() + " " + des3.getName() + "\n";
for(int i = 0; i < 17; i++){
table2 += String.format("%5d %4d %4d %4d %4d", i, des0.compareK().get(i), des1.compareK().get(i), des2.compareK().get(i), des3.compareK().get(i)) + "\n";
}
//End timer for avalanche effect analysis
long end = System.currentTimeMillis();
//Finishes formatting and combining output string to write to file
header += "Total Running Time: " + (end - start) + "(ms)\n";
String output = header + table1 + table2;
//Creates an Encrypt output file called "Encrypt-Output.txt" in the same folder as the project, if one doesn't exist
try{
File enOutFile = new File("Encrypt-Output.txt");
if(enOutFile.createNewFile()){
System.out.println(enOutFile.getName() + " created");
}else{
System.out.println(enOutFile.getName() + " already exists");
}
} catch(IOException e){
System.out.println("Error: ");
e.printStackTrace();
}
//Writes the output string to the file called "Encrypt-Output.txt"
try{
FileWriter outW = new FileWriter("Encrypt-Output.txt");
outW.write(output);
outW.close();
System.out.println("Successfully wrote to Encrypt-Output.txt");
} catch(IOException e){
System.out.println("Error: ");
e.printStackTrace();
}
String cipherText = "";
String key = "";
//Reads in data from the Decrypt-Input file, with the 1st line being the cipherText and the 2nd line being the original Key
//Exits program if unsuccesful
try
{
File inFile = new File("Decrypt-Input.txt");
Scanner fReader = new Scanner(inFile);
while(fReader.hasNextLine()){
cipherText = fReader.nextLine();
key = fReader.nextLine();
}
fReader.close();
}
catch (FileNotFoundException e)
{
System.out.println("File not found...\nExiting program...");
return;
}
//Begins formatting output string for decryption demonstration
String decryptOut = "DECRYPTION\n";
decryptOut += "CipherText C: " + cipherText + "\n";
decryptOut += "Key K: " + key + "\n";
//Removes all whitespaces from cipherText and key for easy of use later
cipherText = cipherText.replace(" ", "");
key = key.replace(" ", "");
//Creates another DES0 object with a modified constructor, only requiring the cipherText and the key
DES0 decryptMod = new DES0(cipherText, key);
//Adds the decrypted cipherText to the output file
decryptOut += "Plaintext P: " + decryptMod.getCPK();
//Attempts to create a file called "Decrypt-Output.txt" in the same folder as the project, if one doesn't already exist
try{
File deOutFile = new File("Decrypt-Output.txt");
if(deOutFile.createNewFile()){
System.out.println(deOutFile.getName() + " created");
}else{
System.out.println(deOutFile.getName() + " already exists");
}
} catch(IOException e){
System.out.println("Error: ");
e.printStackTrace();
}
//Attempts to write the decryption output to the "Decrypt-Output.txt" file
try{
FileWriter outW = new FileWriter("Decrypt-Output.txt");
outW.write(decryptOut);
outW.close();
System.out.println("Successfully wrote to Decrypt-Output.txt");
} catch(IOException e){
System.out.println("Error: ");
e.printStackTrace();
}
}
}