-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolveCipher2.java
220 lines (146 loc) · 4.5 KB
/
solveCipher2.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
import java.util.ArrayList;
import java.util.List;
public class solveCipher2 {
public static void main(String[] args) {
//Given cipher text
String cipherText = "ZVM]AZCH^kPPMWs\\sCsCM@AU[_LRM\\[UXUOYG\\["
+ "YDBNUMkXZPEDY\\UXCM\\GBB_DWsTZUXBNUHYOPs\\Ous";
//Checking with Kasisky test to determine key length
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)){
System.out.println("Match found at "+(i+1)+" and "+(j+1)
+" with distance:"+(j-i));
}
}
}
//The short length of the message renders the Kasisky test
//unsuccessful.
System.out.println("Kasisky Test proves to be unsuccessful.\n");
int[] arr=new int[cipherText.length()];
int n=0;
String str="";
//Converting ASCII values to decimal values
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]);
}
//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];
List<String[]> binaryList=new ArrayList<String[]>();
//Employing Brute force technique
//Testing multiple key lengths by changing the
//value of k
int k=2;
// int k=3;
// int k=4;
//Fetching the list of all possible binary values
//which on XOR operation with each character from
//the cipher text
for(int i=0;i<k;i++){
binaryList.add(calculateXOR.binaryValues(cipherText.charAt(i)));
}
for(int itr=0;itr<binaryList.size();itr++){
String[] binaryKeys=new String[binaryList.get(itr).length];
binaryKeys=binaryList.get(itr);
for(int k1=0;k1<binaryKeys.length;k1++){
String key1="";
key1=binaryKeys[k1];
//Performing XOR for each binary string in the list obtained
//for each character in cipher-text
for(int i=0;i<binary.length;i++){
if(i%k==itr){
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{
//Replacing the characters in positions
//other than the multiples of k as 'x'
plaintext[i]=Integer.toBinaryString((int) 'x');
}
}
for(int i=0;i<plaintext.length;i++){
arr[i] = Integer.parseInt(plaintext[i], 2);
}
boolean print=true;
//Checking to see if the partially decrypted text
//have invalid characters in positions which are
//multiples of 'k'
for(int i=0;i<arr.length;i++){
if(!(arr[i]>=65 && arr[i]<=90)&& !(arr[i]>=97 && arr[i]<=122)){
print=false;
break;
}
}
char[] result=new char[arr.length];
for(int i=0;i<arr.length;i++){
result[i]=(char) arr[i];
}
//Only possible solutions are printed
if(print){
for(int i=0;i<result.length;i++){
System.out.print(result[i]);
}
//Key values:
System.out.println("\nKey:"+Integer.parseInt(key1, 2)+" at position k="+(itr+1));
System.out.print("\n");
}
}
}
}
//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;
}
}