-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sigaba_CSP889.java
213 lines (160 loc) · 7.1 KB
/
Sigaba_CSP889.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
/*
Copyright (C) 2021 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/>. */
public class Sigaba_CSP889 extends Sigaba {
// Sigaba M-134-C/CSP889 variant. Default case of superclass
// Tested against 100,000 encrypts/decrypts of random 800 character texts against the Lasry
// code in the MTC3 "The SIGABA Challenge Part 1" (as adjusted for the z<->space difference)
Sigaba_CSP889(boolean mimicMTC3_STAMP, boolean mimicMTC3_LASRY,
int [] cipherOrder, int [] controlOrder, int [] indexOrder,
int [] cipherOffset,int [] controlOffset,int [] indexOffset,
boolean [] cipherRev, boolean [] controlRev) {
super(mimicMTC3_STAMP,mimicMTC3_LASRY,
cipherOrder,controlOrder,indexOrder,
cipherOffset,controlOffset,indexOffset,
cipherRev,controlRev);
}
//-------------------------------------------------------------------------------
public static Sigaba_CSP889 deterministicFactory(boolean MTC3_STAMP,boolean MTC3_LASRY,
String PT,boolean [] cipherRev,String init) {
// Use the 1st 4 characters of PT to set the control wheels
// deterministically - for testing purposes only
String indexW=RandomShuffle.shuffle("01234");
String cio="";
String coo="";
String io="";
String cirev="";
String corev="";
int [] cipherOrder=new int[5];
int [] controlOrder=new int[5];
int [] indexOrder=new int[5];
int [] cipherOffset=new int[5];
int [] controlOffset=new int[5];
int [] indexOffset=new int[5];
boolean [] controlRev=new boolean[5];
int deter=26*26*26*((int)PT.charAt(0)-65)+26*26*((int)PT.charAt(1)-65)+
26*((int)PT.charAt(2)-65)+((int)PT.charAt(3)-65);
boolean [] wh=new boolean[10];
for (int i=10;i>5;i--) {
int tmp=deter%i;
while (wh[tmp]) tmp++; // tmp=(tmp+1)%10;
wh[tmp]=true;
deter/=i;
cipherOrder[10-i]=tmp;
}
String rest="";
for (int i=0;i<10;i++)
if (!wh[i]) rest+=(char)(48+i);
String wheels=RandomShuffle.shuffle(rest);
for (int i=0;i<5;i++) {
controlOrder[i]=(int)wheels.charAt(i)-48;
indexOrder[i]=(int)indexW.charAt(i)-48;
cipherOffset[i]=(int)init.charAt(i)-65;
controlOffset[i]=rand.nextInt(26);
indexOffset[i]=rand.nextInt(10);
controlRev[i]=(rand.nextInt(2)==0);
cio+=(char)(65+cipherOffset[i]);
coo+=(char)(65+controlOffset[i]);
io+=(char)(48+indexOffset[i]);
cirev+=(cipherRev[i]?"1":"0");
corev+=(controlRev[i]?"1":"0");
}
return new Sigaba_CSP889(MTC3_STAMP,MTC3_LASRY,cipherOrder, controlOrder, indexOrder,
cipherOffset,controlOffset,indexOffset,
cipherRev, controlRev);
}
//-------------------------------------------------------------------------------
public static Sigaba_CSP889 randomFactory(boolean MTC3_STAMP,boolean MTC3_LASRY) { // Give me a random machine
String wheels=RandomShuffle.shuffle("0123456789");
String indexW=RandomShuffle.shuffle("01234");
String cio="";
String coo="";
String io="";
String cirev="";
String corev="";
int [] cipherOrder=new int[5];
int [] controlOrder=new int[5];
int [] indexOrder=new int[5];
int [] cipherOffset=new int[5];
int [] controlOffset=new int[5];
int [] indexOffset=new int[5];
boolean [] cipherRev=new boolean[5];
boolean [] controlRev=new boolean[5];
for (int i=0;i<5;i++) {
cipherOrder[i]=(int)wheels.charAt(i)-48;
controlOrder[i]=(int)wheels.charAt(i+5)-48;
indexOrder[i]=(int)indexW.charAt(i)-48;
cipherOffset[i]=rand.nextInt(26);
controlOffset[i]=rand.nextInt(26);
indexOffset[i]=rand.nextInt(10);
cipherRev[i]=(rand.nextInt(2)==0);
controlRev[i]=(rand.nextInt(2)==0);
cio+=(char)(65+cipherOffset[i]);
coo+=(char)(65+controlOffset[i]);
io+=(char)(48+indexOffset[i]);
cirev+=(cipherRev[i]?"1":"0");
corev+=(controlRev[i]?"1":"0");
}
return new Sigaba_CSP889(MTC3_STAMP,MTC3_LASRY,cipherOrder, controlOrder, indexOrder,
cipherOffset,controlOffset,indexOffset,
cipherRev, controlRev);
}
//-------------------------------------------------------------------------------
public static Sigaba_CSP889 statedFactory(boolean MTC3_STAMP,boolean MTC3_LASRY,String ipkey) {
ipkey=ipkey.replace(" ","");
// From Stamp's format "987601234501243 1100001010 AAABBCCCDD98703"
String wheels=ipkey.substring(0,10);
String indexW=ipkey.substring(10,15);
int [] cipherOrder=new int[5];
int [] controlOrder=new int[5];
int [] indexOrder=new int[5];
int [] cipherOffset=new int[5];
int [] controlOffset=new int[5];
int [] indexOffset=new int[5];
boolean [] cipherRev=new boolean[5];
boolean [] controlRev=new boolean[5];
for (int i=0;i<5;i++) {
cipherOrder[i]=(int)wheels.charAt(i)-48;
controlOrder[i]=(int)wheels.charAt(i+5)-48;
indexOrder[i]=(int)indexW.charAt(i)-48;
cipherOffset[i]=(ipkey.charAt(25+i))-'A';
controlOffset[i]=(ipkey.charAt(30+i))-'A';
indexOffset[i]=(ipkey.charAt(35+i))-'0';
cipherRev[i]=(ipkey.charAt(15+i))=='1';
controlRev[i]=(ipkey.charAt(20+i))=='1';;
}
return new Sigaba_CSP889(MTC3_STAMP,MTC3_LASRY,cipherOrder, controlOrder, indexOrder,
cipherOffset,controlOffset,indexOffset,
cipherRev, controlRev);
}
//-------------------------------------------------------------------------------
public static void main(String [] args) {
boolean passed=true;
// Test from MTC3 STAMP challenge additional files
String StampCT="SGYRGRHCQQXBBMBLDCFUJNEEEBPGNYCZYOWZYBORMREBQBGMJWQURFQQAOIKGNNOCSFRLWUADGLUMEQIDDEWAEEDCYJJINVXDJCHODWRAOJSINFGAPTRUUNYTQVBZTTABDWZNMAVEEOK";
String StampPT="AD HOC AD LOC QUID PRO QUO SO LITTLE TIME SO MUCH TO KNOW FOUR SCORE AND SEVEN YEARS AGO SPACE THE FINAL FRONTIER IN THE BEGINNING BUZZ BUZZ";
// Encryption is "Sigaba_CSP889 987601234501243 1100001010 AAABBCCCDD98703 0 plain.txt cipher.txt"
Sigaba_CSP889 sigaba=Sigaba_CSP889.statedFactory(true,false,"98760 12345 01243 11000 01010 AAABB CCCDD 98703");
passed&=sigaba.decode(StampCT).equals(StampPT.replace("Z","X")); // Any Zs will have corrupted into Xs
sigaba.reset();
passed&=sigaba.encode(StampPT).equals(StampCT);
// Comparison with results of Lasry code
String LasryCT="JTSCALXDRWOQKRXHKMVD";
String LasryPT="AAAAAAAAAAAAAAAAAAAA";
sigaba=Sigaba_CSP889.statedFactory(false,true,"01234 56789 01234 10001 00100 ABCDE FGHIJ 01234");
passed&=sigaba.decode(LasryCT).equals(LasryPT);
sigaba.reset();
passed&=sigaba.encode(LasryPT).equals(LasryCT);
if (passed) System.out.println("PASS");
else System.out.println("*** FAIL ***");
}
}