@@ -33,16 +33,16 @@ public void setKey(String key) {
33
33
}
34
34
35
35
// Permutation table to convert initial 64 bit key to 56 bit key
36
- private static int [] PC1 = {57 , 49 , 41 , 33 , 25 , 17 , 9 , 1 , 58 , 50 , 42 , 34 , 26 , 18 , 10 , 2 , 59 , 51 , 43 , 35 , 27 , 19 , 11 , 3 , 60 , 52 , 44 , 36 , 63 , 55 , 47 , 39 , 31 , 23 , 15 , 7 , 62 , 54 , 46 , 38 , 30 , 22 , 14 , 6 , 61 , 53 , 45 , 37 , 29 , 21 , 13 , 5 , 28 , 20 , 12 , 4 };
36
+ private static int [] pc1 = {57 , 49 , 41 , 33 , 25 , 17 , 9 , 1 , 58 , 50 , 42 , 34 , 26 , 18 , 10 , 2 , 59 , 51 , 43 , 35 , 27 , 19 , 11 , 3 , 60 , 52 , 44 , 36 , 63 , 55 , 47 , 39 , 31 , 23 , 15 , 7 , 62 , 54 , 46 , 38 , 30 , 22 , 14 , 6 , 61 , 53 , 45 , 37 , 29 , 21 , 13 , 5 , 28 , 20 , 12 , 4 };
37
37
38
38
// Lookup table used to shift the initial key, in order to generate the subkeys
39
- private static int [] KEY_SHIFTS = {1 , 1 , 2 , 2 , 2 , 2 , 2 , 2 , 1 , 2 , 2 , 2 , 2 , 2 , 2 , 1 };
39
+ private static int [] keyShifts = {1 , 1 , 2 , 2 , 2 , 2 , 2 , 2 , 1 , 2 , 2 , 2 , 2 , 2 , 2 , 1 };
40
40
41
41
// Table to convert the 56 bit subkeys to 48 bit subkeys
42
- private static int [] PC2 = {14 , 17 , 11 , 24 , 1 , 5 , 3 , 28 , 15 , 6 , 21 , 10 , 23 , 19 , 12 , 4 , 26 , 8 , 16 , 7 , 27 , 20 , 13 , 2 , 41 , 52 , 31 , 37 , 47 , 55 , 30 , 40 , 51 , 45 , 33 , 48 , 44 , 49 , 39 , 56 , 34 , 53 , 46 , 42 , 50 , 36 , 29 , 32 };
42
+ private static int [] pc2 = {14 , 17 , 11 , 24 , 1 , 5 , 3 , 28 , 15 , 6 , 21 , 10 , 23 , 19 , 12 , 4 , 26 , 8 , 16 , 7 , 27 , 20 , 13 , 2 , 41 , 52 , 31 , 37 , 47 , 55 , 30 , 40 , 51 , 45 , 33 , 48 , 44 , 49 , 39 , 56 , 34 , 53 , 46 , 42 , 50 , 36 , 29 , 32 };
43
43
44
44
// Initial permutatation of each 64 but message block
45
- private static int [] IP = {58 , 50 , 42 , 34 , 26 , 18 , 10 , 2 , 60 , 52 , 44 , 36 , 28 , 20 , 12 , 4 , 62 , 54 , 46 , 38 , 30 , 22 , 14 , 6 , 64 , 56 , 48 , 40 , 32 , 24 , 16 , 8 , 57 , 49 , 41 , 33 , 25 , 17 , 9 , 1 , 59 , 51 , 43 , 35 , 27 , 19 , 11 , 3 , 61 , 53 , 45 , 37 , 29 , 21 , 13 , 5 , 63 , 55 , 47 , 39 , 31 , 23 , 15 , 7 };
45
+ private static int [] ip = {58 , 50 , 42 , 34 , 26 , 18 , 10 , 2 , 60 , 52 , 44 , 36 , 28 , 20 , 12 , 4 , 62 , 54 , 46 , 38 , 30 , 22 , 14 , 6 , 64 , 56 , 48 , 40 , 32 , 24 , 16 , 8 , 57 , 49 , 41 , 33 , 25 , 17 , 9 , 1 , 59 , 51 , 43 , 35 , 27 , 19 , 11 , 3 , 61 , 53 , 45 , 37 , 29 , 21 , 13 , 5 , 63 , 55 , 47 , 39 , 31 , 23 , 15 , 7 };
46
46
47
47
// Expansion table to convert right half of message blocks from 32 bits to 48 bits
48
48
private static int [] expansion = {32 , 1 , 2 , 3 , 4 , 5 , 4 , 5 , 6 , 7 , 8 , 9 , 8 , 9 , 10 , 11 , 12 , 13 , 12 , 13 , 14 , 15 , 16 , 17 , 16 , 17 , 18 , 19 , 20 , 21 , 20 , 21 , 22 , 23 , 24 , 25 , 24 , 25 , 26 , 27 , 28 , 29 , 28 , 29 , 30 , 31 , 32 , 1 };
@@ -70,33 +70,33 @@ public void setKey(String key) {
70
70
static int [] permutation = {16 , 7 , 20 , 21 , 29 , 12 , 28 , 17 , 1 , 15 , 23 , 26 , 5 , 18 , 31 , 10 , 2 , 8 , 24 , 14 , 32 , 27 , 3 , 9 , 19 , 13 , 30 , 6 , 22 , 11 , 4 , 25 };
71
71
72
72
// Table used for final inversion of the message box after 16 rounds of Feistel Function
73
- static int [] IPinverse = {40 , 8 , 48 , 16 , 56 , 24 , 64 , 32 , 39 , 7 , 47 , 15 , 55 , 23 , 63 , 31 , 38 , 6 , 46 , 14 , 54 , 22 , 62 , 30 , 37 , 5 , 45 , 13 , 53 , 21 , 61 , 29 , 36 , 4 , 44 , 12 , 52 , 20 , 60 , 28 , 35 , 3 , 43 , 11 , 51 , 19 , 59 , 27 , 34 , 2 , 42 , 10 , 50 , 18 , 58 , 26 , 33 , 1 , 41 , 9 , 49 , 17 , 57 , 25 };
73
+ static int [] ipInverse = {40 , 8 , 48 , 16 , 56 , 24 , 64 , 32 , 39 , 7 , 47 , 15 , 55 , 23 , 63 , 31 , 38 , 6 , 46 , 14 , 54 , 22 , 62 , 30 , 37 , 5 , 45 , 13 , 53 , 21 , 61 , 29 , 36 , 4 , 44 , 12 , 52 , 20 , 60 , 28 , 35 , 3 , 43 , 11 , 51 , 19 , 59 , 27 , 34 , 2 , 42 , 10 , 50 , 18 , 58 , 26 , 33 , 1 , 41 , 9 , 49 , 17 , 57 , 25 };
74
74
75
75
private String [] getSubkeys (String originalKey ) {
76
- StringBuilder permutedKey = new StringBuilder (); // Initial permutation of keys via PC1
76
+ StringBuilder permutedKey = new StringBuilder (); // Initial permutation of keys via pc1
77
77
int i , j ;
78
78
for (i = 0 ; i < 56 ; i ++) {
79
- permutedKey .append (originalKey .charAt (PC1 [i ] - 1 ));
79
+ permutedKey .append (originalKey .charAt (pc1 [i ] - 1 ));
80
80
}
81
81
String [] subKeys = new String [16 ];
82
82
String initialPermutedKey = permutedKey .toString ();
83
83
String C0 = initialPermutedKey .substring (0 , 28 ), D0 = initialPermutedKey .substring (28 );
84
84
85
85
// We will now operate on the left and right halves of the permutedKey
86
86
for (i = 0 ; i < 16 ; i ++) {
87
- String Cn = C0 .substring (KEY_SHIFTS [i ]) + C0 .substring (0 , KEY_SHIFTS [i ]);
88
- String Dn = D0 .substring (KEY_SHIFTS [i ]) + D0 .substring (0 , KEY_SHIFTS [i ]);
87
+ String Cn = C0 .substring (keyShifts [i ]) + C0 .substring (0 , keyShifts [i ]);
88
+ String Dn = D0 .substring (keyShifts [i ]) + D0 .substring (0 , keyShifts [i ]);
89
89
subKeys [i ] = Cn + Dn ;
90
90
C0 = Cn ; // Re-assign the values to create running permutation
91
91
D0 = Dn ;
92
92
}
93
93
94
- // Let us shrink the keys to 48 bits (well, characters here) using PC2
94
+ // Let us shrink the keys to 48 bits (well, characters here) using pc2
95
95
for (i = 0 ; i < 16 ; i ++) {
96
96
String key = subKeys [i ];
97
97
permutedKey .setLength (0 );
98
98
for (j = 0 ; j < 48 ; j ++) {
99
- permutedKey .append (key .charAt (PC2 [j ] - 1 ));
99
+ permutedKey .append (key .charAt (pc2 [j ] - 1 ));
100
100
}
101
101
subKeys [i ] = permutedKey .toString ();
102
102
}
@@ -163,7 +163,7 @@ private String encryptBlock(String message, String[] keys) {
163
163
StringBuilder permutedMessage = new StringBuilder ();
164
164
int i ;
165
165
for (i = 0 ; i < 64 ; i ++) {
166
- permutedMessage .append (message .charAt (IP [i ] - 1 ));
166
+ permutedMessage .append (message .charAt (ip [i ] - 1 ));
167
167
}
168
168
String L0 = permutedMessage .substring (0 , 32 ), R0 = permutedMessage .substring (32 );
169
169
@@ -178,7 +178,7 @@ private String encryptBlock(String message, String[] keys) {
178
178
String combinedBlock = R0 + L0 ; // Reverse the 16th block
179
179
permutedMessage .setLength (0 );
180
180
for (i = 0 ; i < 64 ; i ++) {
181
- permutedMessage .append (combinedBlock .charAt (IPinverse [i ] - 1 ));
181
+ permutedMessage .append (combinedBlock .charAt (ipInverse [i ] - 1 ));
182
182
}
183
183
return permutedMessage .toString ();
184
184
}
0 commit comments