-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSAES.java
More file actions
436 lines (384 loc) · 12.7 KB
/
SAES.java
File metadata and controls
436 lines (384 loc) · 12.7 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
Programming Problem 6.14 -
Simplified AES implementation
16 bit plain text and 16 bit key preferred
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
class SAES
{
public static String[][] sBox = {{"1001","0100","1010","1011"},{"1101","0001","1000","0101"},{"0110","0010","0000","0011"},{"1100","1110","1111","0111"}};
public static String[][] inverseSBox = {{"1010","0101","1001","1011"},{"0001","0111","1000","1111"},{"0110","0000","0010","0011"},{"1100","0100","1101","1110"}};
public static int length = 16, blockSize = 4;
public static void main(String[] args)throws IOException
{
int i = 0, j = 0, x = 0, k = 0, ind = 0, rotateLength = 0, inverseRotateLength = 0;//, temp = 0;
//char[] readData = null;
String readData = null;
x = (int)Math.sqrt(length/blockSize);
char[][][] plainText = new char[x][x][blockSize], cipherText = new char[x][x][blockSize], key0 = new char[x][x][blockSize], key1 = new char[x][x][blockSize], key2 = new char[x][x][blockSize];
int[] sBoxIndices = new int[2];
char[] temp;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the plaintext-");
readData = reader.readLine();//.toCharArray();
for (i = 0; i < x; ++i)
for (j = 0; j < x; ++j)
for (k = 0; k < blockSize; ++k)
plainText[j][i][k] = readData.charAt(ind++);
ind = 0;
System.out.println("Enter the key-");
readData = reader.readLine();//.toCharArray();
for (i = 0; i < x; ++i)
for (j = 0; j < x; ++j)
for (k = 0; k < blockSize; ++k)
key0[j][i][k] = readData.charAt(ind++);
ind = 0;
//generate two new keys
key1 = generateNewKey(key0,new char[][]{{'1','0','0','0'},{'0','0','0','0'}});
key2 = generateNewKey(key1,new char[][]{{'0','0','1','1'},{'0','0','0','0'}});
System.out.println("------------------------------------------------------------------");
System.out.println(" Given Plain Text");
System.out.println("------------------------------------------------------------------");
for (i = 0; i < x; ++i)
{
for (j = 0; j < x; ++j)
{
for (k = 0; k < blockSize; ++k)
System.out.print(plainText[j][i][k]);
System.out.print(" ");
}
System.out.print(" ");
}
System.out.println();
System.out.println();
//////////////////////////////////////////////////////////////////////////
// ENCRYPTION
//////////////////////////////////////////////////////////////////////////
//XOR with key0
cipherText = performXor(plainText,key0);
//Nibble Substitution
for (i = 0; i < x; ++i)
{
for (j = 0; j < x; ++j)
{
sBoxIndices = getSBoxIndices(cipherText[i][j]);
cipherText[i][j] = sBox[sBoxIndices[0]][sBoxIndices[1]].toCharArray();
}
}
//Shift rows
for (k = 1; k < x; ++k)
{
++rotateLength;
for (i = 0, j = rotateLength-1; i < j; ++i, --j)
{
temp = cipherText[k][i];
cipherText[k][i] = cipherText[k][j];
cipherText[k][j] = temp;
}
for (i = rotateLength, j = x-1; i < j; ++i, --j)
{
temp = cipherText[k][i];
cipherText[k][i] = cipherText[k][j];
cipherText[k][j] = temp;
}
for (i = 0, j = x-1; i < j; ++i, --j)
{
temp = cipherText[k][i];
cipherText[k][i] = cipherText[k][j];
cipherText[k][j] = temp;
}
}
rotateLength = 0;
//Mix columns
/* b c
-----------------------
j0 j1
i0 0 1 2 3 0 1 2 3
i1 4 5 6 7 4 5 6 7*/
for (i = 0; i < x; ++i)
for (j = 0; j < x; ++j)
for (k = 0; k < blockSize; ++k)
plainText[i][j][k] = cipherText[i][j][k];
for (i = 0; i < x; ++i)
{
cipherText[0][i][0] = performXor(plainText[0][i][0],plainText[1][i][2]);
cipherText[0][i][1] = performXor(plainText[0][i][1],plainText[1][i][0],plainText[1][i][3]);
cipherText[0][i][2] = performXor(plainText[0][i][2],plainText[1][i][0],plainText[1][i][1]);
cipherText[0][i][3] = performXor(plainText[0][i][3],plainText[1][i][1]);
cipherText[1][i][0] = performXor(plainText[0][i][2],plainText[1][i][0]);
cipherText[1][i][1] = performXor(plainText[0][i][0],plainText[0][i][3],plainText[1][i][1]);
cipherText[1][i][2] = performXor(plainText[0][i][0],plainText[0][i][1],plainText[1][i][2]);
cipherText[1][i][3] = performXor(plainText[0][i][1],plainText[1][i][3]);
}
//XOR with key1
cipherText = performXor(cipherText,key1);
//Nibble substituion round-2
for (i = 0; i < x; ++i)
{
for (j = 0; j < x; ++j)
{
sBoxIndices = getSBoxIndices(cipherText[i][j]);
cipherText[i][j] = sBox[sBoxIndices[0]][sBoxIndices[1]].toCharArray();
}
}
//Shift rows round-2
for (k = 1; k < x; ++k)
{
++rotateLength;
for (i = 0, j = rotateLength-1; i < j; ++i, --j)
{
temp = cipherText[k][i];
cipherText[k][i] = cipherText[k][j];
cipherText[k][j] = temp;
}
for (i = rotateLength, j = x-1; i < j; ++i, --j)
{
temp = cipherText[k][i];
cipherText[k][i] = cipherText[k][j];
cipherText[k][j] = temp;
}
for (i = 0, j = x-1; i < j; ++i, --j)
{
temp = cipherText[k][i];
cipherText[k][i] = cipherText[k][j];
cipherText[k][j] = temp;
}
}
rotateLength = 0;
//XOR with key2
cipherText = performXor(cipherText,key2);
System.out.println("------------------------------------------------------------------");
System.out.println(" Cipher Text (After Encryption)");
System.out.println("------------------------------------------------------------------");
for (i = 0; i < x; ++i)
{
for (j = 0; j < x; ++j)
{
for (k = 0; k < blockSize; ++k)
System.out.print(cipherText[j][i][k]);
System.out.print(" ");
}
System.out.print(" ");
}
System.out.println();
System.out.println();
//////////////////////////////////////////////////////////////////////////
// DECRYPTION
//////////////////////////////////////////////////////////////////////////
//XOR with key2
plainText = performXor(cipherText,key2);
//Inverse Shift rows
for (k = 1; k < x; ++k)
{
++rotateLength;
inverseRotateLength = x-rotateLength;
for (i = 0, j = inverseRotateLength-1; i < j; ++i, --j)
{
temp = plainText[k][i];
plainText[k][i] = plainText[k][j];
plainText[k][j] = temp;
}
for (i = inverseRotateLength, j = x-1; i < j; ++i, --j)
{
temp = plainText[k][i];
plainText[k][i] = plainText[k][j];
plainText[k][j] = temp;
}
for (i = 0, j = x-1; i < j; ++i, --j)
{
temp = plainText[k][i];
plainText[k][i] = plainText[k][j];
plainText[k][j] = temp;
}
}
rotateLength = 0;
//Inverse Nibble substitution
for (i = 0; i < x; ++i)
{
for (j = 0; j < x; ++j)
{
sBoxIndices = getSBoxIndices(plainText[i][j]);
plainText[i][j] = inverseSBox[sBoxIndices[0]][sBoxIndices[1]].toCharArray();
}
}
//XOR with key1
plainText = performXor(plainText,key1);
//Inverse Mix Columns
/* k l
-----------------------
j0 j1
i0 0 1 2 3 0 1 2 3
i1 4 5 6 7 4 5 6 7*/
for (i = 0; i < x; ++i)
for (j = 0; j < x; ++j)
for (k = 0; k < blockSize; ++k)
cipherText[i][j][k] = plainText[i][j][k];
for (i = 0; i < x; ++i)
{
plainText[0][i][0] = performXor(cipherText[0][i][3],cipherText[1][i][1]);
plainText[0][i][1] = performXor(cipherText[0][i][0],cipherText[1][i][2]);
plainText[0][i][2] = performXor(cipherText[0][i][1],cipherText[1][i][0],cipherText[1][i][3]);
plainText[0][i][3] = performXor(cipherText[0][i][2],cipherText[0][i][3],cipherText[1][i][1]);
plainText[1][i][0] = performXor(cipherText[0][i][1],cipherText[1][i][3]);
plainText[1][i][1] = performXor(cipherText[0][i][2],cipherText[1][i][0]);
plainText[1][i][2] = performXor(cipherText[0][i][0],cipherText[0][i][3],cipherText[1][i][1]);
plainText[1][i][3] = performXor(cipherText[0][i][0],cipherText[1][i][2],cipherText[1][i][3]);
}
//Inverse Shift rows round-2
for (k = 1; k < x; ++k)
{
++rotateLength;
inverseRotateLength = x-rotateLength;
for (i = 0, j = inverseRotateLength-1; i < j; ++i, --j)
{
temp = plainText[k][i];
plainText[k][i] = plainText[k][j];
plainText[k][j] = temp;
}
for (i = inverseRotateLength, j = x-1; i < j; ++i, --j)
{
temp = plainText[k][i];
plainText[k][i] = plainText[k][j];
plainText[k][j] = temp;
}
for (i = 0, j = x-1; i < j; ++i, --j)
{
temp = plainText[k][i];
plainText[k][i] = plainText[k][j];
plainText[k][j] = temp;
}
}
//Inverse Nibble substitution round-2
for (i = 0; i < x; ++i)
{
for (j = 0; j < x; ++j)
{
sBoxIndices = getSBoxIndices(plainText[i][j]);
plainText[i][j] = inverseSBox[sBoxIndices[0]][sBoxIndices[1]].toCharArray();
}
}
//XOR with key0
plainText = performXor(plainText,key0);
System.out.println("------------------------------------------------------------------");
System.out.println(" Plain Text (After Decryption)");
System.out.println("------------------------------------------------------------------");
for (i = 0; i < x; ++i)
{
for (j = 0; j < x; ++j)
{
for (k = 0; k < blockSize; ++k)
System.out.print(plainText[j][i][k]);
System.out.print(" ");
}
System.out.print(" ");
}
System.out.println();
System.out.println();
}
public static char[][][] generateNewKey(char[][][] key, char[][] roundKey)
{
int x = (int)Math.sqrt(length/blockSize), i = 0, j = 0;
char[][][] newKey = new char[x][x][blockSize];
char[][] w0 = new char[x][blockSize];
char[][] w1 = new char[x][blockSize];
char[][] w2 = new char[x][blockSize];
char[][] w3 = new char[x][blockSize];
//store w0
for (i = 0; i < x; ++i)
for (j = 0; j < blockSize; ++j)
w0[i][j] = key[i][0][j];
//store w1
for (i = 0; i < x; ++i)
for (j = 0; j < blockSize; ++j)
w1[i][j] = key[i][1][j];
//step-1 swap two blocks
int r1 = 0, r2 = 1;
for (i = 0; i < blockSize; ++i)
w2[r1][i] = key[r2][1][i];
r1 = 1;
r2 = 0;
for (i = 0; i < blockSize; ++i)
w2[r1][i] = key[r2][1][i];
//substitution
int[] indices;
for (i = 0; i < x; ++i)
{
indices = getSBoxIndices(w2[i]);
w2[i] = sBox[indices[0]][indices[1]].toCharArray();
}
//XOR round key
w2 = performXor(w2,roundKey);
//XOR with w0
w2 = performXor(w0,w2); //this gives w2
for (i = 0; i < x; ++i)
for (j = 0; j < blockSize; ++j)
newKey[i][0][j] = w2[i][j];
//XOR with w1
w3 = performXor(w1,w2); //this gives w3
for (i = 0; i < x; ++i)
for (j = 0; j < blockSize; ++j)
newKey[i][1][j] = w3[i][j];
return newKey;
}
public static int[] getSBoxIndices(char[] block)
{
int[] indices = new int[2];
if (block[0] == '0' && block[1] == '0') indices[0] = 0;
else if (block[0] == '0' && block[1] == '1') indices[0] = 1;
else if (block[0] == '1' && block[1] == '0') indices[0] = 2;
else if (block[0] == '1' && block[1] == '1') indices[0] = 3;
if (block[2] == '0' && block[3] == '0') indices[1] = 0;
else if (block[2] == '0' && block[3] == '1') indices[1] = 1;
else if (block[2] == '1' && block[3] == '0') indices[1] = 2;
else if (block[2] == '1' && block[3] == '1') indices[1] = 3;
return indices;
}
public static char performXor(char ch1, char ch2)
{
if (ch1 == ch2) return '0';
return '1';
}
public static char performXor(char ch1, char ch2, char ch3)
{
if (ch1 == ch2)
{
if (ch2 == ch3) return ch1;
else return ch3;
}
return ch3 == '0' ? '1' : '0';
}
public static char[][] performXor(char[][] data1, char[][] data2)
{
char[][] result = new char[data1.length][data1[0].length];
int i = 0, j = 0;
for (i = 0; i < data1.length; ++i)
{
for (j = 0; j < data1[0].length; ++j)
{
if (data1[i][j] == data2[i][j]) result[i][j] = '0';
else result[i][j] = '1';
}
}
return result;
}
public static char[][][] performXor(char[][][] data1, char[][][] data2)
{
char[][][] result = new char[data1.length][data1[0].length][data1[0][0].length];
int i = 0, j = 0, k = 0;
for (i = 0; i < data1.length; ++i)
{
for (j = 0; j < data1[0].length; ++j)
{
for (k = 0; k < data1[0][0].length; ++k)
{
if (data1[i][j][k] == data2[i][j][k]) result[i][j][k] = '0';
else result[i][j][k] = '1';
}
}
}
return result;
}
}