-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutokey.java
More file actions
215 lines (207 loc) · 5.89 KB
/
Copy pathAutokey.java
File metadata and controls
215 lines (207 loc) · 5.89 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
public class Autokey
{
public static String encrypt (String m, String key)
{
key = key + m;
key = key.replaceAll("[^A-za-z]", "").toLowerCase();
String c = "";
int offset = 0;
for (int i = 0; i < m.length(); i++)
{
char ch = m.charAt(i);
if (ch >= 'A' && ch <= 'Z')
c += (char) ((ch - 'A' + key.charAt(offset++) - 'a') % 26 + 'A');
else if (ch >= 'a' && ch <= 'z')
c += (char) ((ch - 'a' + key.charAt(offset++) - 'a') % 26 + 'a');
else
c += ch;
}
return c;
}
public static String decrypt (String c, String key)
{
key = key.replaceAll("[^A-za-z]", "").toLowerCase();
String m = "";
int offset = 0;
for (int i = 0; i < c.length(); i++)
{
char ch = c.charAt(i);
if (ch >= 'A' && ch <= 'Z')
{
char d = (char) ((ch - 'A' - key.charAt(offset++) + 'a' + 26) % 26 + 'A');
m += d;
key += (char) (d - 'A' + 'a');
}
else if (ch >= 'a' && ch <= 'z')
{
char d = (char) ((ch - 'a' - key.charAt(offset++) + 'a' + 26) % 26 + 'a');
m += d;
key += d;
}
else
m += ch;
}
return m;
}
public static String[] comboCrack (String c, int minKeyLength, int maxKeyLength)
{
String[] decKey = crack(c, minKeyLength, maxKeyLength);
return crackGuessKey(c, decKey[1]);
}
public static String[] crack (String c, int minKeyLength, int maxKeyLength)
{
double maxScoreAll = Cipher.getQuadScore(c);
String m = c;
String maxKey = "";
String cStripped = c.replaceAll("[^A-za-z]", "").toLowerCase();
if (minKeyLength < 1)
minKeyLength = 1;
if (maxKeyLength >= cStripped.length())
maxKeyLength = cStripped.length() - 1;
for (int keyLen = minKeyLength; keyLen <= maxKeyLength; keyLen++)
{
double[] pairScores = new double[keyLen];
String[] pairChars = new String[keyLen];
for (int pair = 0; pair < pairScores.length; pair++)
{
pairScores[pair] = -Double.MAX_VALUE;
pairChars[pair] = "";
for (char ch0 = 'a'; ch0 <= 'z'; ch0++)
for (char ch1 = 'a'; ch1 <= 'z'; ch1++)
{
double score = 0, total = 0;
String prevPlain = ch0 + "" + ch1;
for (int i = pair; i + 2 <= cStripped.length(); i += keyLen, total++)
{
String dec = decrypt(cStripped.substring(i, i + 2), prevPlain);
score += Cipher.getBiScore(dec);
prevPlain = dec;
}
score = score / total;
if (score > pairScores[pair])
{
pairScores[pair] = score;
pairChars[pair] = ch0 + "" + ch1;
}
}
}
String key = "";
for (int i = 0; i < keyLen; i++)
if (pairScores[i] > pairScores[(i - 1 + keyLen) % keyLen])
key += pairChars[i].charAt(0);
else
key += pairChars[(i - 1 + keyLen) % keyLen].charAt(1);
String dec = decrypt(c, key);
double score = Cipher.getQuadScore(dec);
if (score > maxScoreAll)
{
maxScoreAll = score;
m = dec;
maxKey = key;
}
}
return new String[] {m, maxKey};
}
public static String[] crack2 (String c, int minKeyLength, int maxKeyLength)
{
String cStripped = c.replaceAll("[^A-za-z]", "").toLowerCase();
double maxScoreAll = Cipher.getQuadScore(c);
String maxKey = "";
if (minKeyLength < 1)
minKeyLength = 1;
if (maxKeyLength > cStripped.length())
maxKeyLength = cStripped.length();
for (int keyLen = minKeyLength; keyLen <= maxKeyLength; keyLen++)
{
String key = keyGen(keyLen);
int lastChanged = -1;
double maxScore = -Double.MAX_VALUE;
for (int i = 0; i != lastChanged; i = (i + 1) % keyLen)
{
double maxScoreChar = -Double.MAX_VALUE;
String maxKeyChar = "";
for (char ch = 'a'; ch <= 'z'; ch++)
{
String newKey = key.substring(0, i) + ch + key.substring(i + 1);
String newDec = decrypt(c, newKey);
double score = Cipher.getQuadScore(newDec);
if (score > maxScoreChar)
{
maxScoreChar = score;
maxKeyChar = newKey;
}
}
if (maxScoreChar > maxScore)
{
maxScore = maxScoreChar;
key = maxKeyChar;
lastChanged = i;
}
}
if (maxScore > maxScoreAll)
{
maxScoreAll = maxScore;
maxKey = key;
}
}
return new String[] {decrypt(c, maxKey), maxKey};
}
public static String[] crackGuessKey (String c, String startKey)
{
double maxScore = Cipher.getQuadScore(c);
String key = startKey;
int lastChanged = -1;
for (int i = 0; i != lastChanged; i = (i + 1) % startKey.length())
{
double maxScoreChar = -Double.MAX_VALUE;
String maxKeyChar = "";
for (char ch = 'a'; ch <= 'z'; ch++)
{
String newKey = key.substring(0, i) + ch + key.substring(i + 1);
String newDec = decrypt(c, newKey);
double score = Cipher.getQuadScore(newDec);
if (score > maxScoreChar)
{
maxScoreChar = score;
maxKeyChar = newKey;
}
}
if (maxScoreChar > maxScore)
{
maxScore = maxScoreChar;
key = maxKeyChar;
lastChanged = i;
}
}
return new String[] {decrypt(c, key), key};
}
public static String keyGen (int keyLength)
{
String key = "";
while (key.length() < keyLength)
key += (char) (Math.random() * 26 + 'a');
return key;
}
public static void demo ()
{
System.out.println("---------------Autokey------------------------");
String m = Cipher.getPlaintext();
int keyLen = (int) (Math.random() * m.replaceAll("[^A-Za-z]", "").length() / 7.0) + 1;
String key = keyGen(keyLen);
String c = encrypt(m, key);
System.out.println("Ciphertext: " + c + "\n");
String[] decKey = comboCrack(c, key.length() / 2, key.length());
String dec = decKey[0];
System.out.println("Decryption: " + dec + "\n");
System.out.println("Key found: " + decKey[1]);
System.out.println("Correct key: " + key);
//System.out.println("Correct score: " + Cipher.getQuadScore(m));
//System.out.println("Produced score: " + Cipher.getQuadScore(dec));
if (dec.equals(m))
System.out.println("Decryption successful.");
else
System.out.println("Decryption failed.");
System.out.println("*******************************************");
System.out.println();
}
}