-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
308 lines (279 loc) · 10.4 KB
/
Main.java
File metadata and controls
308 lines (279 loc) · 10.4 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import java.io.*;
import java.nio.file.Files;
public class Main {
// Number of bytes in a block
private static final int BLOCK_SIZE = 16;
/**
* Main function performing a simple AES version
* @param args - arguments options:
* {"-e","–k","<key-file-path>","–i","<input-file-path>","-o","<output-file-path>"}
* {"-d","–k","<key-file-path>","–i","<input-file-path>","-o","<output-file-path>"}
* {"-b","–m","<message-file-path>","–c","<cipher-file-path>","-o","<output-file-path>"}
*/
public static void main(String[] args) {
// Arguments for testing
// args = new String[]
// {
// "-e",
// "-k",
// "C:\\Users\\yogev.s\\IdeaProjects\\aes\\out\\artifacts\\aes_jar\\key_short",
// "-i",
// "C:\\Users\\yogev.s\\IdeaProjects\\aes\\out\\artifacts\\aes_jar\\message_short",
// "-o",
// "C:\\Users\\yogev.s\\IdeaProjects\\aes\\out\\artifacts\\aes_jar\\output"
// };
args = validateArguments(args);
switch (args[0]) {
case "-e": encrypt(args); break;
case "-d": decrypt(args); break;
case "-b": breakCipher(args); break;
default: System.out.println("Wrong input!"); break;
}
}
/**
* Encrypt a given message according to AES
* from the input file
* into the output file
* using keys from the keys file
* @param args - input parameters
*/
private static void encrypt(String[] args) {
// -e –k <path-to-key-file> -i <path-to-input-file> -o <path-to-output-file>
// get the input message from the input file
byte[] inputFile = getFileContent(args[4]);
byte[][] blocks = getContentBlocks(inputFile);
blocks = transpose(blocks);
// get the keys from the keys file
byte[] keysFile = getFileContent(args[2]);
byte[][] keys = transpose(getContentBlocks(keysFile));
byte[] key1 = keys[0];
byte[] key2 = keys[1];
byte[] key3 = keys[2];
// encrypt the message
byte[][] cipheredBlocks = new byte[blocks.length][blocks[0].length];
for (int i = 0; i < blocks.length; i++) {
byte[] block = blocks[i];
cipheredBlocks[i] =
addRoundKey(shiftRowsLeft(
addRoundKey(shiftRowsLeft(
addRoundKey(shiftRowsLeft(block), key1)), key2)), key3);
}
cipheredBlocks = transpose(cipheredBlocks);
// write the ciphered message to the output file
writeToFile(args[6], cipheredBlocks);
}
/**
* Decrypt a given message according to AES
* from the input file
* into the output file
* using keys from the keys file
* @param args - input parameters
*/
private static void decrypt(String[] args) {
// -d –k <path-to-key-file> -i <path-to-input-file> -o <path-to-output-file>
// get the ciphered message from the input file
byte[] inputFile = getFileContent(args[4]);
byte[][] blocks = getContentBlocks(inputFile);
blocks = transpose(blocks);
// get the keys from the keys file
byte[] keysFile = getFileContent(args[2]);
byte[][] keys = transpose(getContentBlocks(keysFile));
byte[] key1 = keys[0];
byte[] key2 = keys[1];
byte[] key3 = keys[2];
// decrypt the message
byte[][] decipheredBlocks = new byte[blocks.length][blocks[0].length];
for (int i = 0; i < blocks.length; i++) {
byte[] block = blocks[i];
decipheredBlocks[i] =
shiftRowsRight(addRoundKey(
shiftRowsRight(addRoundKey(
shiftRowsRight(addRoundKey(block, key3)), key2)), key1));
}
decipheredBlocks = transpose(decipheredBlocks);
// write the deciphered message to the output file
writeToFile(args[6], decipheredBlocks);
}
/**
* Break a given cipher
* with given message and cipher
* using random key1 and key2
* and finding the matching key3
* @param args - input parameters
*/
private static void breakCipher(String[] args) {
// -b –m <path-to-message> –c <path-to-cipher> -o <output-path>
// get the message
byte[] messageFile = getFileContent(args[2]);
byte[][] message = getContentBlocks(messageFile);
message = transpose(message);
// get the cipher
byte[] cipherFile = getFileContent(args[4]);
byte[][] cipher = getContentBlocks(cipherFile);
cipher = transpose(cipher);
// create keys
byte[] key1 = generateKey(1);
byte[] key2 = generateKey(0);
byte[] key3 = new byte[BLOCK_SIZE];
// break the cipher
byte[] block = message[0];
byte[] c2tag = shiftRowsLeft(
addRoundKey(shiftRowsLeft(
addRoundKey(shiftRowsLeft(block), key1)), key2));
byte[] cipherBlock = cipher[0];
key3 = addRoundKey(cipherBlock, c2tag);
byte[][] keys = new byte[][] {key1, key2, key3};
keys = transpose(keys);
// write the keys to the output file
writeToFile(args[6], keys);
}
/**
* Get the contents from a given path to a file
* @param arg - path
* @return byte array of the contents
*/
private static byte[] getFileContent(String arg) {
File file = new File(arg);
byte[] inputFile = null;
try {
inputFile = Files.readAllBytes(file.toPath());
} catch (IOException e) {
e.printStackTrace();
}
return inputFile;
}
/**
* Write content to a file in a given path
* @param arg - path
* @param content - content divided into blocks
*/
private static void writeToFile(String arg, byte[][] content) {
OutputStream os = null;
try {
os = new FileOutputStream(arg);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
for (byte[] aContent : content) {
for (int j = 0; j < content[0].length; j++) {
try {
if (os != null) {
os.write(aContent[j]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
assert os != null;
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Get the content of the blocks
* divide the contents into separate blocks
* @param content - the full content
* @return byte matrix of the content
*/
private static byte[][] getContentBlocks(byte[] content) {
int k = 0;
byte[][] blocks = new byte[content.length / BLOCK_SIZE][BLOCK_SIZE];
for (int i = 0; i < blocks.length; i++)
for (int j = 0; j < blocks[0].length; j++)
blocks[i][j] = content[k++];
return blocks;
}
/**
* Shift the bytes of a block to the LEFT
* From: 0 1 2 3 | 4 5 6 7 | 8 9 10 11 | 12 13 14 15
* To: 0 1 2 3 | 5 6 7 4 | 10 11 8 9 | 15 12 13 14
* @param block - given block
* @return block shifted
*/
private static byte[] shiftRowsLeft(byte[] block) {
int k = 0;
byte[] shifted = new byte[block.length];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < (4 - i); j++)
shifted[k++] = block[(i * 4) + j + i];
for (int j = 0; j < i; j++)
shifted[k++] = block[(i * 4) + j];
}
return shifted;
}
/**
* Shift the bytes of a block to the RIGHT
* From: 0 1 2 3 | 4 5 6 7 | 8 9 10 11 | 12 13 14 15
* To: 0 1 2 3 | 7 4 5 6 | 10 11 8 9 | 13 14 15 12
* @param block - given block
* @return block shifted
*/
private static byte[] shiftRowsRight(byte[] block) {
int k = 0;
byte[] shifted = new byte[block.length];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < i; j++)
shifted[k++] = block[(i * 4) + (4 - i) + j];
for (int j = 0; j < (4 - i); j++)
shifted[k++] = block[(i * 4) + j];
}
return shifted;
}
/**
* Use XOR between a block and a key
* @param block - the block
* @param key - the key
* @return block XOR key
*/
private static byte[] addRoundKey(byte[] block, byte[] key) {
byte[] xored = new byte[key.length];
int i = 0;
for (byte b : block) {
xored[i] = (byte) ((b ^ key[i]) & 0x000000ff);
i++;
}
return xored;
}
/**
* Transpose a given set of blocks
* Transposes each block with itself
* @param blocks - set of blocks
* @return transposed set of blocks
*/
private static byte[][] transpose(byte[][] blocks) {
byte[][] newBlocks = new byte[blocks.length][blocks[0].length];
for (int i = 0; i < blocks.length; i++)
for (int j = 0; j < blocks[0].length; j++)
newBlocks[i][((j % 4) * 4) + (j / 4)] = blocks[i][j];
return newBlocks;
}
/**
* Generate a byte key from a given integer
* @param num - given number
* @return generated byte array
*/
private static byte[] generateKey(Integer num) {
byte numByte = num.byteValue();
byte[] numBytes = new byte[BLOCK_SIZE];
for (int i = 0; i < numBytes.length; i++)
numBytes[i] = numByte;
return numBytes;
}
/**
* Validate and fix the given arguments
* @param args - given arguments
* @return fixed arguments
*/
private static String[] validateArguments(String[] args) {
if (args.length != 7)
System.out.println("Wrong number of input arguments!");
for (String arg : args) {
if (arg.contains(" "))
arg = arg.replace(" ", "");
}
return args;
}
}