-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpirale.java
239 lines (206 loc) · 6.61 KB
/
Spirale.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
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Date;
class Spirale extends ClassicalCipher {
// This was a MTC3 challenge Cipher
// Note inconsistent use of 0 and 1 base for keys - partially due to similar in design
/*
Copyright (C) 2019 S Combes
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
static final int KLEN=7;
final static boolean TESTING=false;
protected String [] key;
int [] pi1;
int [] ip1; // Inverse of pi
int [] pi2;
int [] ip2; // Inverse of pi2
int [] lk;
// ----------------------------------------------------------------------
Spirale(String k1,String k2,String k3,String k4)
{
super(new Codespace(Codespace.StockAlphabet.CAPITALS));
key=new String[4];
if (k1==null) { // Random - not completely elegant, prefer Spirale() constructor
for (int i=0;i<4;i++) {
StringBuilder sb=new StringBuilder(7);
for (int j=0;j<7;j++)
sb.append((char)(65+rand.nextInt(26)));
key[i]=sb.toString();
}
k1=key[0];
k2=key[1];
k3=key[2];
k4=key[3];
}
else {
key[0]=new String(k1);
key[1]=new String(k2);
key[2]=new String(k3);
key[3]=new String(k4);
}
ip1=permed(k1);
pi1=new int[26];
for (int i=0;i<26;i++)
pi1[ip1[i]]=i;
ip2=permed(k2);
pi2=new int[26];
for (int i=0;i<26;i++)
pi2[ip2[i]]=i;
lk=new int[49]; // Create 'long key'
int i=0;
int j=0;
for (int z=0;z<49;z++) {
lk[z]=(pi1[(int)k3.charAt(j)-65]+pi2[(int)k4.charAt(i)-65])%26; // j, i in that order because k4 across top of table
if (i==6) {
i=j+1;
j=6;
} else if (j==0) {
j=i+1;
i=0;
}
else { j--; i++; }
}
}
// ----------------------------------------------------------------------
Spirale(int [] k1,int [] k2,int [] k3,int [] k4)
{
super(new Codespace(Codespace.StockAlphabet.CAPITALS));
key=new String[4];
/* // Should convert to string, but does not affect operation EXCEPT perturb
setkey(k1,k2,k3,k4);
*/
ip1=permed(k1);
pi1=new int[26];
for (int i=0;i<26;i++)
pi1[ip1[i]]=i;
ip2=permed(k2);
pi2=new int[26];
for (int i=0;i<26;i++)
pi2[ip2[i]]=i;
lk=new int[49]; // Create 'long key'
int i=0;
int j=0;
for (int z=0;z<49;z++) {
lk[z]=(pi1[k3[j]]+pi2[k4[j]])%26; // j, i in that order because k4 across top of table
if (i==6) {
i=j+1;
j=6;
} else if (j==0) {
j=i+1;
i=0;
}
else { j--; i++; }
}
}
// ----------------------------------------------------------------------
public Spirale copyInstance(ClassicalCipher c) // Copy factory method
{ return new Spirale(((Spirale)c).key[0],((Spirale)c).key[1],
((Spirale)c).key[2],((Spirale)c).key[3]); }
// ----------------------------------------------------------------------
protected void setkey(int [] k1,int [] k2,int [] k3,int [] k4)
{ // Only for cosmetic purposes
StringBuilder sb=new StringBuilder(10);
for (int j=0;j<7;j++) {
sb.append((char)(65+k1[j]));
}
key[0]=sb.toString();
sb=new StringBuilder(10);
for (int j=0;j<7;j++) {
sb.append((char)(65+k2[j]));
}
key[1]=sb.toString();
sb=new StringBuilder(10);
for (int j=0;j<7;j++) {
sb.append((char)(65+k3[j]));
}
key[2]=sb.toString();
sb=new StringBuilder(10);
for (int j=0;j<7;j++) {
sb.append((char)(65+k4[j]));
}
key[3]=sb.toString();
}
// ----------------------------------------------------------------------
public static int [] permed(String s)
{ // When starting with a string of length 7 (the key) convert to a 7
// index array, and pass that array to permed(int []) to return the
// 26-index permutation.
if (s.length()!=KLEN) throw new IllegalArgumentException("Length !=7");
// Comment out for speed, if you're sure
int [] perm=new int[KLEN];
for (int i=0;i<KLEN;i++)
perm[i]=s.charAt(i)-64;
return permed(perm);
}
// ----------------------------------------------------------------------
public static int [] permed(int [] perm) // perm is 1-based
{ // When starting with a 7 index int[], convert to the 26 index permutation array
int index=0;
int place=26;
int [] result=new int[26];
boolean [] used=new boolean[26];
for (int i=0;i<26;i++) {
for (int j=0;j<perm[i%KLEN];) { // Increment inside loop
place--;
if (place<0) place=25;
if (!used[place]) {
j++;
}
}
used[place%26]=true;
result[index++]=place%26;
}
return result;
}
// ----------------------------------------------------------------------
public String toString()
{ return (" "+key[0]+
" "+key[1]+" "+key[2]+" "+key[3]);}
// ----------------------------------------------------------------------
@Override
public String encode(String PT)
{
int [] stream=new int[PT.length()];
for (int i=0;i<49 && i<PT.length();i++)
stream[i]=lk[i];
for (int i=49;i<PT.length();i++)
stream[i]=(pi1[stream[i-49]]+pi2[stream[i-24]])%26;
StringBuilder sb=new StringBuilder(PT.length());
for (int i=0;i<PT.length();i++)
sb.append((char)(65+(pi1[PT.charAt(i)-65]+pi2[stream[i]])%26));
return sb.toString();
}
// ----------------------------------------------------------
@Override
public String decode(String CT)
{
int [] stream=new int[CT.length()];
for (int i=0;i<49 && i<CT.length();i++)
stream[i]=lk[i];
for (int i=49;i<CT.length();i++)
stream[i]=(pi1[stream[i-49]]+pi2[stream[i-24]])%26;
StringBuilder sb=new StringBuilder(CT.length());
for (int i=0;i<CT.length();i++)
sb.append((char)(65+ip1[(CT.charAt(i)-65-pi2[stream[i]]+52)%26]));
return sb.toString();
}
// ----------------------------------------------------------
public static void main(String [] args) {
// Example from MTC3 Spirale Description
String PT="SPIRALEISAONETIMEPADCRYPTOSYSTEMDESIGNEDTOREPLACESOLITAIREWHENONEHASNOCARDS";
String CT="HXYYEQXLUFBJQLAHYTYMHXONCHQKYEAWSJRRREUQQWNKGIUNWNMTRSPDXFONSMCJHAEDFKZQAFL";
Spirale spirale=new Spirale("NVIKKIH","CTSQEOU","DNGDKSZ","EAIWDSH");
if (spirale.knownTest(PT,CT)) System.out.println("PASS");
else System.out.println("***** FAIL *******");
}
}