-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColumnarTransposition.java
More file actions
235 lines (220 loc) · 6.52 KB
/
Copy pathColumnarTransposition.java
File metadata and controls
235 lines (220 loc) · 6.52 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import java.util.*;
public class ColumnarTransposition
{
public static final int DEFAULT_ITERATIONS = 51;
public static String encrypt (String m, String key)
{
char[][] grid = new char[(int) Math.ceil((double) m.length() / key.length())][key.length()];
for (int i = 0; i < grid.length; i++)
for (int j = 0; j < grid[i].length; j++)
if (i * grid[i].length + j >= m.length())
grid[i][j] = 'x';
else
grid[i][j] = m.charAt(i * grid[i].length + j);
String k = "";
for (char c : key.toCharArray())
if (!k.contains(c + ""))
k += c;
char[] arr = k.toCharArray();
Arrays.sort(arr);
String c = "";
for (char ch : arr)
for (int i = 0; i < grid.length; i++)
c += grid[i][k.indexOf(ch)];
return c;
}
public static String decrypt (String c, String key)
{
char[][] grid = new char[c.length() / key.length()][key.length()];
for (int j = 0; j < grid[0].length; j++)
for (int i = 0; i < grid.length; i++)
grid[i][j] = c.charAt(j * grid.length + i);
String k = "";
ArrayList<Character> arr = new ArrayList<>();
for (char ch : key.toCharArray())
if (!k.contains(ch + ""))
{
k += ch;
arr.add(ch);
}
Collections.sort(arr);
String m = "";
for (int i = 0; i < grid.length; i++)
for (char ch : k.toCharArray())
m += grid[i][arr.indexOf(ch)];
return m;
}
public static String[] crack (String c, int minKeyLength, int maxKeyLength, int iterations)
{
String[] decKey = {c, ""};
double maxScore = Cipher.getQuadScore(c);
for (int keyLen = minKeyLength; keyLen <= maxKeyLength; keyLen++)
{
String[] newDecKey = hillClimb(c, keyLen, iterations);
double score = Cipher.getQuadScore(newDecKey[0]);
if (score > maxScore)
{
maxScore = score;
decKey = newDecKey;
}
}
return decKey;
}
public static String[] hillClimb (String c, int keyLength)
{
return hillClimb(c, keyLength, DEFAULT_ITERATIONS);
}
public static String[] hillClimb (String c, int keyLength, int iterations)
{
double maxScoreAll = Cipher.getQuadScore(c);
String m = c;
String maxKey = "";
ArrayList<Long> swaps = new ArrayList<>(), usedSwaps = new ArrayList<>();
long pow = (long) Math.floor(Math.log10(keyLength - 1)) + 1;
for (int s0 = 0; s0 < keyLength - 1; s0++)
for (int s1 = s0 + 1; s1 <= keyLength - 1; s1++)
swaps.add(s0 * (long) Math.pow(10, pow) + s1);
ArrayList<Integer> rotations = new ArrayList<>(), usedRotations = new ArrayList<>();
for (int start = 1; start < keyLength; start++)
rotations.add(start);
for (int starts = 0; starts < iterations; starts++)
{
swaps.addAll(usedSwaps);
usedSwaps.clear();
rotations.addAll(usedRotations);
usedRotations.clear();
String key = keyGen(keyLength);
String dec = decrypt(c, key);
double maxScore = Cipher.getQuadScore(dec);
while (swaps.size() > 0 || rotations.size() > 0)
{
String newKey = "";
double probSwap = (double) swaps.size() / (swaps.size() + rotations.size());
if (Math.random() < probSwap)
{
Long swapHash = swaps.remove((int) (Math.random() * swaps.size()));
usedSwaps.add(swapHash);
int s0 = (int) (swapHash / (long) Math.pow(10, pow)), s1 = (int) (swapHash % (long) Math.pow(10, pow));
for (int x = 0; x < key.length(); x++)
if (x == s0)
newKey += key.charAt(s1);
else if (x == s1)
newKey += key.charAt(s0);
else
newKey += key.charAt(x);
}
else
{
int shift = rotations.remove((int) (Math.random() * rotations.size()));
usedRotations.add(shift);
newKey = key.substring(shift) + key.substring(0, shift);
}
String newDec = decrypt(c, newKey);
double score = Cipher.getQuadScore(newDec);
if (score > maxScore)
{
maxScore = score;
dec = newDec;
key = newKey;
swaps.addAll(usedSwaps);
usedSwaps.clear();
rotations.addAll(usedRotations);
usedRotations.clear();
}
}
if (maxScore > maxScoreAll)
{
maxScoreAll = maxScore;
m = dec;
maxKey = key;
}
}
return new String[] {m, maxKey};
}
public static String bruteForce (String c, int maxKeyLength)
{
return bruteForce(c, 2, maxKeyLength);
}
public static String bruteForce (String c, int minKeyLength, int maxKeyLength)
{
if (minKeyLength < 2)
minKeyLength = 2;
double maxScore = Cipher.getScore(c);
String m = c;
for (int keyLength = minKeyLength; keyLength <= maxKeyLength; keyLength++)
if (c.length() % keyLength == 0)
{
String dec = genPerms("", keyLength, c);
double score = Cipher.getScore(dec);
if (score > maxScore)
{
maxScore = score;
m = dec;
}
}
return m;
}
private static String genPerms (String cur, int keyLength, String c)
{
if (cur.length() == keyLength)
return decrypt(c, cur);
double maxScore = -Double.MAX_VALUE;
String m = "";
HashSet<Character> used = new HashSet<>();
for (char ch : cur.toCharArray())
used.add(ch);
for (int next = 0; next < keyLength; next++)
{
if (used.contains(next + 'A'))
continue;
String dec = genPerms(cur + (char) (next + 'A'), keyLength, c);
double score = Cipher.getScore(dec);
if (score > maxScore)
{
maxScore = score;
m = dec;
}
}
return m;
}
public static String keyGen (int keyLength)
{
if (keyLength < 2)
return "0";
String key = "";
ArrayList<Integer> toUse = new ArrayList<>();
for (int i = 0; i < keyLength; i++)
toUse.add(i);
while (toUse.size() > 0)
{
int rand = (int) (Math.random() * toUse.size());
key += (char) (toUse.remove(rand) + 'A');
}
return key;
}
public static void demo (int keyLen, int iterations)
{
System.out.println("---------------Columnar Transposition------------------------");
String m = Cipher.getPlaintext();
String key = keyGen(keyLen);
String c = encrypt(m, key);
System.out.println("Ciphertext: " + c + "\n");
m = decrypt(c, key);
long time = System.currentTimeMillis();
String[] decKey = hillClimb(c, keyLen, iterations);
time = System.currentTimeMillis() - time;
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("Runtime: " + (time / 1000.0) + " seconds");
System.out.println("*******************************************");
System.out.println();
}
}