-
Notifications
You must be signed in to change notification settings - Fork 0
/
q1.java
483 lines (407 loc) · 15.1 KB
/
q1.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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import java.util.Scanner;
public class q1{
public static int[][] constantMatrix = {{1, 4}, {4, 1}};
private static final String RCON1 = "1110";
private static final String RCON2 = "1010";
public static String inputBlock2[][]=new String[2][2];
public static String[][] matrixMultiply(String[][] inputBlock) {
String[][] outputBlock = new String[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
String sum = "0";
for (int k = 0; k < 2; k++) {
sum = finiteFieldAddition(sum, finiteFieldMultiplication(constantMatrix[i][k], inputBlock[k][j]));
}
outputBlock[i][j] = sum;
}
}
return outputBlock;
}
public static String finiteFieldMultiplication(int a, String b) {
int aValue = a;
int bValue = Integer.parseInt(b, 16);
int result = 0;
while (bValue > 0) {
if ((bValue & 1) == 1) {
result = result ^ aValue;
}
aValue = aValue << 1;
if ((aValue & 0b10000) != 0) {
aValue = aValue ^ 0b10011;
}
bValue = bValue >> 1;
}
return Integer.toHexString(result).toUpperCase();
}
public static String finiteFieldAddition(String a, String b) {
int aValue = Integer.parseInt(a, 16);
int bValue = Integer.parseInt(b, 16);
int result = aValue ^ bValue;
return Integer.toHexString(result).toUpperCase();
}
public static void main(String[] args) {
String[][] inputBlock = new String[2][2];
String[][] originalBinaryMatrix = new String[2][2];
String[][] modifiedBinaryMatrix = new String[2][2];
String subNibbles = "";
String shiftRow="";
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a hexadecimal string (up to 4 characters): ");
String hexInput = scanner.nextLine();
// Ensure the input is a valid hexadecimal string
if (isValidHexadecimal(hexInput) && hexInput.length() <= 4) {
// Pad with leading zeros if the length is less than 4
while (hexInput.length() < 4) {
hexInput = "0" + hexInput;
}
// Create the inputBlock matrix
inputBlock[0][0] = String.valueOf(hexInput.charAt(0));
inputBlock[1][0] = String.valueOf(hexInput.charAt(1));
inputBlock[0][1] = String.valueOf(hexInput.charAt(2));
inputBlock[1][1] = String.valueOf(hexInput.charAt(3));
System.out.println("Processed hexadecimal string: " + hexInput);
// Create a 2D matrix to store binary values
// Convert each character of the processed hexadecimal string to binary
for (int i = 0; i < hexInput.length(); i++) {
char hexChar = hexInput.charAt(i);
int index = Integer.parseInt(String.valueOf(hexChar), 16);
// Ensure each binary value is of size 4
String binaryValue = Integer.toBinaryString(index);
while (binaryValue.length() < 4) {
binaryValue = "0" + binaryValue;
}
originalBinaryMatrix[i % 2][i / 2] = binaryValue;
modifiedBinaryMatrix[i % 2][i / 2] = binaryValue;
}
// Display the original binary matrix
System.out.println("Original Binary Matrix:");
printMatrix(originalBinaryMatrix);
// Perform substitution in the modified matrix
substituteMatrixValues(modifiedBinaryMatrix);
// Print the modified matrix
System.out.println("\nModified Binary Matrix:");
printMatrix(modifiedBinaryMatrix);
// Swap elements of the first row in the modified matrix
swapFirstRowElements(originalBinaryMatrix);
// Print the matrix after swapping
System.out.println("\nBinary Matrix After Swapping:");
printMatrix(modifiedBinaryMatrix);
// Convert binary values back to hexadecimal and print the resulting matrix
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
String binaryValue = modifiedBinaryMatrix[j][i];
int decimalValue = Integer.parseInt(binaryValue, 2);
String hexValue = Integer.toHexString(decimalValue).toLowerCase();
subNibbles+=hexValue;
}
}
} else {
System.out.println("Invalid input. Please enter a valid hexadecimal string (up to 4 characters).");
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
String binaryValue = originalBinaryMatrix[j][i];
int decimalValue = Integer.parseInt(binaryValue, 2);
String hexValue = Integer.toHexString(decimalValue).toLowerCase();
shiftRow+=hexValue;
}
}
System.out.println("\nSub Nibbles:"+subNibbles);
System.out.println("\nShift Row:"+shiftRow);
System.out.println("Mix Column: " +inputBlock[0][0]+inputBlock[1][0]+inputBlock[0][1]+inputBlock[1][1]);
String[][] outputBlock = matrixMultiply(inputBlock);
System.out.println("Output Block:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(outputBlock[i][j] + " ");
}
System.out.println();
}
System.out.print("Enter the 4-indexed master key: ");
String masterKey = scanner.nextLine();
scanner.close();
while (masterKey.length() < 4) {
masterKey = "0" + masterKey;
}
String[] roundKeys = generateRoundKeys(masterKey);
System.out.println("Round Keys:");
System.out.println("Round Key1:"+roundKeys[0]);
System.out.println("Round Key2:"+roundKeys[1]);
scanner.close();
}
// Check if the input is a valid hexadecimal string
private static boolean isValidHexadecimal(String input) {
String hexPattern = "^[0-9A-Fa-f]*$";
return input.matches(hexPattern);
}
// Helper method to print a 2D matrix
private static void printMatrix(String[][] matrix) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
// Helper method to perform substitution in the matrix
private static void substituteMatrixValues(String[][] matrix) {
String[] substitutionTable = {
"", "0000", "1001", "1110",
"0110", "0011", "1111", "0101",
"0001", "1101", "1100", "0111",
"1011", "0100", "0010", "1000"
};
int xx=0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
xx=0;
// String binaryValue = matrix[i][j];
// int index = Integer.parseInt(binaryValue, 2);
if(matrix[i][j].equals("0000")&&xx!=1)
{
matrix[i][j]="1010";
xx=1;
}
if(matrix[i][j].equals("0001")&&xx!=1)
{
matrix[i][j]="0000";
xx=1;
}
if(matrix[i][j].equals("0010")&&xx!=1)
{
matrix[i][j]="1001";
xx=1;
}
if(matrix[i][j].equals("0011")&&xx!=1)
{
matrix[i][j]="1110";
xx=1;
}
if(matrix[i][j].equals("0100")&&xx!=1)
{
matrix[i][j]="0110";
xx=1;
}
if(matrix[i][j].equals("0101")&&xx!=1)
{
matrix[i][j]="0011";
xx=1;
}
if(matrix[i][j].equals("0110")&&xx!=1)
{
matrix[i][j]="1111";
xx=1;
}
if(matrix[i][j].equals("0111")&&xx!=1)
{
matrix[i][j]="0101";
xx=1;
}
if(matrix[i][j].equals("1000")&&xx!=1)
{
matrix[i][j]="0001";
xx=1;
}
if(matrix[i][j].equals("1001")&&xx!=1)
{
matrix[i][j]="1101";
xx=1;
}
if(matrix[i][j].equals("1010")&&xx!=1)
{
matrix[i][j]="1100";
xx=1;
}
if(matrix[i][j].equals("1011")&&xx!=1)
{
matrix[i][j]="0111";
xx=1;
}
if(matrix[i][j].equals("1100")&&xx!=1)
{
matrix[i][j]="1011";
xx=1;
}
if(matrix[i][j].equals("1101")&&xx!=1)
{
matrix[i][j]="0100";
xx=1;
}
if(matrix[i][j].equals("1110")&&xx!=1)
{
matrix[i][j]="0010";
xx=1;
}
if(matrix[i][j].equals("1111")&&xx!=1)
{
matrix[i][j]="1000";
xx=1;
}
}
}
}
private static String xorHex(String a, String b) {
int intValueA = Integer.parseInt(a, 16);
int intValueB = Integer.parseInt(b, 16);
int result = intValueA ^ intValueB;
return String.format("%02X", result);
}
// Helper method to swap elements of the first row in the matrix
private static void swapFirstRowElements(String[][] matrix) {
String temp = matrix[0][0];
matrix[0][0] = matrix[0][1];
matrix[0][1] = temp;
}
private static String[] generateRoundKeys(String masterKey) {
inputBlock2[0][0] = hexToBinary(String.valueOf(masterKey.charAt(0)));
inputBlock2[1][0] = hexToBinary(String.valueOf(masterKey.charAt(1)));
inputBlock2[0][1] = hexToBinary(String.valueOf(masterKey.charAt(2)));
inputBlock2[1][1] = hexToBinary(String.valueOf(masterKey.charAt(3)));
String[][] roundKey1 = new String[2][2];
String[][] roundKey2 = new String[2][2];
String y=subNibbles(inputBlock2[1][1]);
String z=xorNibbles(y, RCON1);
roundKey1[0][0]=xorNibbles(inputBlock2[0][0],z);
roundKey1[1][0]=xorNibbles(inputBlock2[1][0],roundKey1[0][0]);
roundKey1[0][1]=xorNibbles(inputBlock2[0][1],roundKey1[1][0]);
roundKey1[1][1]=xorNibbles(inputBlock2[1][1],roundKey1[0][1]);
y=subNibbles(roundKey1[1][1]);
z=xorNibbles(y, RCON2);
roundKey2[0][0]=xorNibbles(roundKey1[0][0],z);
roundKey2[1][0]=xorNibbles(roundKey1[1][0],roundKey2[0][0]);
roundKey2[0][1]=xorNibbles(roundKey1[0][1],roundKey2[1][0]);
roundKey2[1][1]=xorNibbles(roundKey1[1][1],roundKey2[0][1]);
roundKey1[0][0]=binaryToHex(roundKey1[0][0]);
roundKey1[1][0]=binaryToHex(roundKey1[1][0]);
roundKey1[0][1]=binaryToHex(roundKey1[0][1]);
roundKey1[1][1]=binaryToHex(roundKey1[1][1]);
roundKey2[0][0]=binaryToHex(roundKey2[0][0]);
roundKey2[1][0]=binaryToHex(roundKey2[1][0]);
roundKey2[0][1]=binaryToHex(roundKey2[0][1]);
roundKey2[1][1]=binaryToHex(roundKey2[1][1]);
String[] roundKeys = new String[2];
roundKeys[0]=roundKey1[0][0]+roundKey1[1][0]+roundKey1[0][1]+roundKey1[1][1];
roundKeys[1]=roundKey2[0][0]+roundKey2[1][0]+roundKey2[0][1]+roundKey2[1][1];
return roundKeys;
}
private static String subNibbles(String binary) {
int xx=0;
// String binaryValue = matrix[i][j];
// int index = Integer.parseInt(binaryValue, 2);
if(binary.equals("0000")&&xx!=1)
{
binary="1010";
xx=1;
}
if(binary.equals("0001")&&xx!=1)
{
binary="0000";
xx=1;
}
if(binary.equals("0010")&&xx!=1)
{
binary="1001";
xx=1;
}
if(binary.equals("0011")&&xx!=1)
{
binary="1110";
xx=1;
}
if(binary.equals("0100")&&xx!=1)
{
binary="0110";
xx=1;
}
if(binary.equals("0101")&&xx!=1)
{
binary="0011";
xx=1;
}
if(binary.equals("0110")&&xx!=1)
{
binary="1111";
xx=1;
}
if(binary.equals("0111")&&xx!=1)
{
binary="0101";
xx=1;
}
if(binary.equals("1000")&&xx!=1)
{
binary="0001";
xx=1;
}
if(binary.equals("1001")&&xx!=1)
{
binary="1101";
xx=1;
}
if(binary.equals("1010")&&xx!=1)
{
binary="1100";
xx=1;
}
if(binary.equals("1011")&&xx!=1)
{
binary="0111";
xx=1;
}
if(binary.equals("1100")&&xx!=1)
{
binary="1011";
xx=1;
}
if(binary.equals("1101")&&xx!=1)
{
binary="0100";
xx=1;
}
if(binary.equals("1110")&&xx!=1)
{
binary="0010";
xx=1;
}
if(binary.equals("1111")&&xx!=1)
{
binary="1000";
xx=1;
}
return binary;
}
private static String xorNibbles(String binary1, String binary2) {
String xorResult = "";
for (int i = 0; i < binary1.length(); i++) {
char bit1 = binary1.charAt(i);
char bit2 = binary2.charAt(i);
char xorBit = (bit1 == bit2) ? '0' : '1';
xorResult += xorBit;
}
return xorResult;
}
private static String hexToBinary(String hex) {
String binary = "";
for (int i = 0; i < hex.length(); i++) {
char hexChar = hex.charAt(i);
String hexString = Character.toString(hexChar);
int decimal = Integer.parseInt(hexString, 16);
String binaryString = Integer.toBinaryString(decimal);
while (binaryString.length() < 4) {
binaryString = "0" + binaryString;
}
binary += binaryString;
}
return binary;
}
private static String binaryToHex(String binary) {
String hex = "";
for (int i = 0; i < binary.length(); i += 4) {
String nibble = binary.substring(i, i + 4);
int decimal = Integer.parseInt(nibble, 2);
String hexString = Integer.toHexString(decimal).toLowerCase();
hex += hexString;
}
return hex;
}
}