-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolveCipher1.java
258 lines (184 loc) · 5.8 KB
/
solveCipher1.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
import java.io.FileNotFoundException;
public class solveCipher1 {
public static void main(String[] args) {
int n=0;
String cipherText="";
//Reading the text from the given file 'Secret.hex' into the String cipherText
//using the readFile method from the copyFile class
try {
cipherText=readFile.readFile();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//Converting the given text from ASCII to decimal form
int[] arr=new int[cipherText.length()];
for(int i=0;i<cipherText.length();i++){
arr[i]=(int) cipherText.charAt(i);
}
//Converting from decimal form to binary strings to perform the XOR function
String[] binary=new String[arr.length];
for(int i=0;i<arr.length;i++){
binary[i]=Integer.toBinaryString(arr[i]);
}
String str="";
//Adjusting length of the binary strings
for(int i=0;i<binary.length;i++){
if(binary[i].length()<8){
n=8-binary[i].length();
str=binary[i];
if(n==1){
binary[i]="0"+str;
}
else if(n==2){
binary[i]="00"+str;
}
else if(n==3){
binary[i]="000"+str;
}
else if(n==4){
binary[i]="0000"+str;
}
str=new String();
}
}
StringBuilder sb = new StringBuilder();
String[] plaintext=new String[binary.length];
//Calculating the first position of the key value from the given hint
//that the first character is 'A'
String key1=calculateKey.getKey(binary[0]);
//Using the first position of the key value to decrypt characters
//at positions which are multiples of k where k varies from 1 to 7,
//to get an estimate of the key length
System.out.println("Testing for key length:\n");
for(int k=1;k<8;k++){
//Performing XOR
for(int i=0;i<binary.length;i++){
//Performs XOR with key1 for characters in the first position
//and multiples of 'k'
if(i%k==0){
for (int j = 0; j < binary[i].length(); j++) {
sb.append(calculateChar(calculateBit(binary[i].charAt(j)) ^
calculateBit(key1.charAt(j))));
}
plaintext[i] = sb.toString();
sb = new StringBuilder();
}
else{
plaintext[i]=binary[i];
}
}
for(int i=0;i<plaintext.length;i++){
arr[i] = Integer.parseInt(plaintext[i], 2);
}
char[] result=new char[arr.length];
for(int i=0;i<arr.length;i++){
result[i]=(char) arr[i];
}
System.out.println("k="+k);
int count=0;
//Checking to see if positions which are multiples of k
// have invalid characters after decrypting with key1
for(int i=0;i<arr.length;i++){
if(i%k==0){
if(!(arr[i]>=65 && arr[i]<=90)&& !(arr[i]>=97 && arr[i]<=122) &&
!(arr[i]>=44 && arr[i]<=46) && arr[i]!=32){
System.out.println("Invalid characters present at positions "
+ "which are multiples of "+k+"!!\n");
count++;
break;
}
}
}
if(count==0){
System.out.println("No invalid characters!\nSuggests a valid key length.\n");
}
/*
for(int i=0;i<result.length;i++){
System.out.print(result[i]);
}
*/
// System.out.println("\n");
}
//Checking with Kasisky test
System.out.println("Kasisky Test:\n");
for(int i=0; i<cipherText.length()-2;i++){
String s1=new String();
s1=""+cipherText.charAt(i)+cipherText.charAt(i+1)
+cipherText.charAt(i+2);
for(int j=i+3; j<cipherText.length()-2;j++){
String s2=new String();
s2=""+cipherText.charAt(j)+cipherText.charAt(j+1)
+cipherText.charAt(j+2);
if(s1.equals(s2)){
if(i<3){
System.out.println("Match found at "+(i+1)+" and "+(j+1)
+" with distance:"+(j-i));
}
}
}
}
//Calculating the GCD of 84, 262, 308, 458, 712 on a calculator
System.out.println("\nGCD of distances=2.\nTherefore confirming key length=2.");
//Above analysis shows that for even values, decryption with
//key1 does not result in an invalid character, suggesting
//the key length is 2.
//'A?fEeR' suggests '?' could either be a letter or ' '(a space)
String key2=calculateKey.getKey(binary[1],' ');
for(int i=0;i<binary.length;i++){
//Performs XOR with key1 for characters in the first position
//and key2 in the second position
if(i%2==0){
for (int j = 0; j < binary[i].length(); j++) {
sb.append(calculateChar(calculateBit(binary[i].charAt(j)) ^
calculateBit(key1.charAt(j))));
}
plaintext[i] = sb.toString();
sb = new StringBuilder();
}
else{
for (int j = 0; j < binary[i].length(); j++) {
sb.append(calculateChar(calculateBit(binary[i].charAt(j)) ^
calculateBit(key2.charAt(j))));
}
plaintext[i] = sb.toString();
sb = new StringBuilder();
}
}
//Converting binary strings to characters to get the Plaintext
for(int i=0;i<plaintext.length;i++){
arr[i] = Integer.parseInt(plaintext[i], 2);
}
char[] result=new char[arr.length];
for(int i=0;i<arr.length;i++){
result[i]=(char) arr[i];
}
//Prints the Plaintext
System.out.println("\nPlaintext:");
for(int i=0;i<result.length;i++){
System.out.print(result[i]);
}
//Prints the Key in ASCII
System.out.println("\nKey:"+(char) Integer.parseInt(key1, 2)+
(char) Integer.parseInt(key2, 2));
}
//Calculating the bit-by-bit value of the XOR operation
//Gives the boolean result of a^b
private static boolean calculateBit(char input) {
boolean result=false;
if(input=='1'){
result=true;
}
return result;
}
//Converts boolean to character value
private static char calculateChar(boolean input) {
char result='\0';
if(input){
result='1';
}
else{
result='0';
}
return result;
}
}