-
Notifications
You must be signed in to change notification settings - Fork 0
/
enigma.go
370 lines (333 loc) · 10.2 KB
/
enigma.go
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
enigma - a M3 enigma machine emulator
Written in 2021 by Mirko Perillo
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide.
This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
package main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
)
const LETTERS_IN_ALPHABET = 26
var rotorI = rotor{[26]rune{'E', 'K', 'M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y', 'H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J'}, 'R'}
var rotorII = rotor{[26]rune{'A', 'J', 'D', 'K', 'S', 'I', 'R', 'U', 'X', 'B', 'L', 'H', 'W', 'T', 'M', 'C', 'Q', 'G', 'Z', 'N', 'P', 'Y', 'F', 'V', 'O', 'E'}, 'F'}
var rotorIII = rotor{[26]rune{'B', 'D', 'F', 'H', 'J', 'L', 'C', 'P', 'R', 'T', 'X', 'V', 'Z', 'N', 'Y', 'E', 'I', 'W', 'G', 'A', 'K', 'M', 'U', 'S', 'Q', 'O'}, 'W'}
var rotorIV = rotor{[26]rune{'E', 'S', 'O', 'V', 'P', 'Z', 'J', 'A', 'Y', 'Q', 'U', 'I', 'R', 'H', 'X', 'L', 'N', 'F', 'T', 'G', 'K', 'D', 'C', 'M', 'W'}, 'K'}
var rotorV = rotor{[26]rune{'V', 'Z', 'B', 'R', 'G', 'I', 'T', 'Y', 'U', 'P', 'S', 'D', 'N', 'H', 'L', 'X', 'A', 'W', 'M', 'J', 'Q', 'O', 'F', 'E', 'C', 'K'}, 'A'}
var reflectorB = rotor{[26]rune{'Y', 'R', 'U', 'H', 'Q', 'S', 'L', 'D', 'P', 'X', 'N', 'G', 'O', 'K', 'M', 'I', 'E', 'B', 'F', 'Z', 'C', 'W', 'V', 'J', 'A', 'T'}, '0'}
var reflectorC = rotor{[26]rune{'F', 'V', 'P', 'J', 'I', 'A', 'O', 'Y', 'E', 'D', 'R', 'Z', 'X', 'W', 'G', 'C', 'T', 'K', 'U', 'Q', 'S', 'B', 'N', 'M', 'H', 'L'}, '0'}
type plug struct {
a rune
b rune
}
type rotor struct {
letters [26]rune
notch rune
}
type config struct {
steckerboard [10]plug
rotors [3]rotor
reflector rotor
positions [3]rune
doubleRotorsStep bool
rings [3]int
debug bool
}
type arrayFlags []string
func (a *arrayFlags) Set(value string) error {
values := strings.Split(value, ",")
for _, v := range values {
*a = append(*a, strings.ToUpper(v))
}
return nil
}
func (a *arrayFlags) String() string {
return strings.Join(*a, ",")
}
type steckerFlags []plug
func (s *steckerFlags) Set(value string) error {
values := strings.Split(value, ",")
if len(values)%2 != 0 {
return errors.New("Steckerboard options should be even")
}
for i := 0; i < len(values); i = i + 2 {
a := string2rune(values[i])
b := string2rune(values[i+1])
*s = append(*s, plug{a, b})
}
return nil
}
func (s *steckerFlags) String() string {
var result string
for _, pair := range *s {
result = result + string(pair.a) + "," + string(pair.b)
}
return result
}
func string2rune(s string) rune {
r := []rune(strings.ToUpper(s))
return r[0]
}
func validateRotorsFlag(rotors []string) (bool, error) {
var validNames = [5]string{"I", "II", "III", "IV", "V"}
if len(rotors) != 3 {
panic("You have to select 3 rotors")
}
for _, r := range rotors {
if !isInArray(r, validNames) {
return false, errors.New(fmt.Sprintf("Rotor %s not exist", r))
}
}
return true, nil
}
func validateReflectorFlag(reflector string) (bool, error) {
var validNames = [5]string{"B", "C"}
if !isInArray(reflector, validNames) {
return false, errors.New(fmt.Sprintf("Reflector %s not exist", reflector))
}
return true, nil
}
func validatePositionsFlag(positions []string) (bool, error) {
if len(positions) != 3 {
panic("You have to select 3 positions")
}
for _, r := range positions {
letter := []rune(r)
if !isLetter(letter[0]) {
return false, errors.New(fmt.Sprintf("Position %s is not valid", string(letter[0])))
}
}
return true, nil
}
func isLetter(r rune) bool {
return r >= 65 && r <= 90
}
func validateSteckerboardFlag(stecker []plug) (bool, error) {
for _, p := range stecker {
if !isLetter(p.a) || !isLetter(p.b) {
return false, errors.New(fmt.Sprintf("Not valid value in (%s,%s)", string(p.a), string(p.b)))
}
}
return true, nil
}
func isInArray(elem string, collection [5]string) bool {
for _, e := range collection {
if e == elem {
return true
}
}
return false
}
func flagToRotor(flag string) rotor {
switch flag {
case "I":
return rotorI
case "II":
return rotorII
case "III":
return rotorIII
case "IV":
return rotorIV
case "V":
return rotorV
case "B":
return reflectorB
case "C":
return reflectorC
default:
return rotorI
}
}
func toConfig(rotors arrayFlags, reflector string, positions arrayFlags, steckerboard steckerFlags, verbose bool) config {
debugCfg := verbose
var steckerboardCfg [10]plug
for idx, p := range steckerboard {
steckerboardCfg[idx] = p
}
reflectorCfg := flagToRotor(reflector)
rotorsCfg := [3]rotor{flagToRotor(rotors[0]), flagToRotor(rotors[1]), flagToRotor(rotors[2])}
positionsCfg := [3]rune{string2rune(positions[0]), string2rune(positions[1]), string2rune(positions[2])}
return config{rotors: rotorsCfg, reflector: reflectorCfg, positions: positionsCfg, steckerboard: steckerboardCfg, debug: debugCfg}
}
func main() {
// example of commang flags
// --rotors=I,II,IV --reflector=B --positions=A,A,A --verbose --steckerboard=A,B,C,E
lastArg := os.Args[len(os.Args)-1]
var message string
if strings.HasPrefix(lastArg, "--") {
message = ""
} else {
message = lastArg
}
var rotorsFlag arrayFlags
var reflectorFlag *string
var positionsFlag arrayFlags
var steckerboardFlag steckerFlags
var verboseFlag *bool
flag.Var(&rotorsFlag, "rotors", "comma separated list of rotors. Available values: I,II,III,IV,V. Default I,II,III")
reflectorFlag = flag.String("reflector", "B", "reflector rotor to use. Available values: B,C. Default B")
flag.Var(&positionsFlag, "positions", "positions of rotors. Default A,A,A")
flag.Var(&steckerboardFlag, "steckerboard", "comma separated list of pairs. Optional flag")
verboseFlag = flag.Bool("verbose", false, "more verbose output")
flag.Parse()
if rotorsFlag == nil {
rotorsFlag = []string{"I", "II", "III"}
} else {
_, err := validateRotorsFlag(rotorsFlag)
if err != nil {
panic(err)
}
}
_, err := validateReflectorFlag(*reflectorFlag)
if err != nil {
panic(err)
}
if positionsFlag == nil {
positionsFlag = []string{"A", "A", "A"}
} else {
_, err := validatePositionsFlag(positionsFlag)
if err != nil {
panic(err)
}
}
_, err = validateSteckerboardFlag(steckerboardFlag)
if err != nil {
panic(err)
}
config := toConfig(rotorsFlag, *reflectorFlag, positionsFlag, steckerboardFlag, *verboseFlag)
resultMsg := encrypt(&config, message)
fmt.Println(resultMsg)
}
func applyRotors(cfg *config, letter rune) rune {
position := letter - 'A'
var encryptedLetter rune
for i := len(cfg.rotors) - 1; i >= 0; i-- {
rotorPosition := cfg.positions[i] - 'A'
encryptedLetter = cfg.rotors[i].letters[((position + rotorPosition) % LETTERS_IN_ALPHABET)]
encryptedLetter = ((LETTERS_IN_ALPHABET + encryptedLetter - 'A' - rotorPosition) % LETTERS_IN_ALPHABET) + 'A'
position = encryptedLetter - 'A'
if cfg.debug {
fmt.Println("Rotor step: ", string(encryptedLetter))
}
}
return encryptedLetter
}
func applyInverseRotors(cfg *config, letter rune) rune {
var encryptedLetter rune
var position int
for i, r := range cfg.rotors {
rotorPosition := cfg.positions[i] - 'A'
letter = 'A' + ((letter - 'A' + rotorPosition) % LETTERS_IN_ALPHABET)
for pos, l := range r.letters {
if l == letter {
position = pos
break
}
}
encryptedLetter = 'A' + ((LETTERS_IN_ALPHABET + rune(position) - rotorPosition) % LETTERS_IN_ALPHABET)
letter = encryptedLetter
if cfg.debug {
fmt.Println("Inverse rotor step: ", string(encryptedLetter))
}
}
return encryptedLetter
}
func applyReflector(cfg *config, letter rune) rune {
position := letter - 'A'
return cfg.reflector.letters[position]
}
func steckerboard(cfg *config, letter rune) rune {
encrypted := letter
for _, plug := range cfg.steckerboard {
if plug.a == letter {
encrypted = plug.b
break
} else if plug.b == letter {
encrypted = plug.a
break
}
}
return encrypted
}
func rotorsMovement(cfg *config) {
if cfg.doubleRotorsStep {
currentPos := cfg.positions[1]
cfg.positions[1] = 'A' + (currentPos+1-'A')%LETTERS_IN_ALPHABET
currentPos = cfg.positions[0]
cfg.positions[0] = 'A' + (currentPos+1-'A')%LETTERS_IN_ALPHABET
cfg.doubleRotorsStep = false
if cfg.debug {
fmt.Println("doubleRotorStep executed")
}
}
currentPos := cfg.positions[2]
nextPos := 'A' + (currentPos+1-'A')%LETTERS_IN_ALPHABET
cfg.positions[2] = nextPos
if cfg.positions[2] == cfg.rotors[2].notch {
if cfg.debug {
fmt.Println("Rotor position 3 notch")
}
currentPos = cfg.positions[1]
cfg.positions[1] = 'A' + (currentPos+1-'A')%LETTERS_IN_ALPHABET
if cfg.positions[1] == cfg.rotors[1].notch {
cfg.doubleRotorsStep = true
if cfg.debug {
fmt.Println("Rotor position 2 notch, doubleRotorStep on next movement")
}
}
}
}
func encrypt(cfg *config, msg string) string {
msg = strings.ToUpper(msg)
if cfg.debug {
fmt.Println("Input: ", msg)
}
var encryptedMsg string
for _, l := range msg {
if l == ' ' {
l = 'X'
}
if l >= 65 && l <= 90 {
// 0. rotors movement
rotorsMovement(cfg)
if cfg.debug {
fmt.Println("positions: ", string(cfg.positions[0]), string(cfg.positions[1]), string(cfg.positions[2]))
}
if cfg.debug {
fmt.Println("Input step: ", string(l))
}
// 1. stecker
encryptedLetter := steckerboard(cfg, l)
if cfg.debug {
fmt.Println("steckerboard encryption: ", string(encryptedLetter))
}
// 2. rotors
encryptedLetter = applyRotors(cfg, encryptedLetter)
if cfg.debug {
fmt.Println("rotors encryption: ", string(encryptedLetter))
}
// 3. reflector
encryptedLetter = applyReflector(cfg, encryptedLetter)
if cfg.debug {
fmt.Println("reflector encryption: ", string(encryptedLetter))
}
// 4. inverse rotors
encryptedLetter = applyInverseRotors(cfg, encryptedLetter)
if cfg.debug {
fmt.Println("rotors encryption: ", string(encryptedLetter))
}
// 5. stecker
encryptedLetter = steckerboard(cfg, encryptedLetter)
if cfg.debug {
fmt.Println("steckerboard encryption: ", string(encryptedLetter))
}
encryptedMsg += string(encryptedLetter)
}
}
return encryptedMsg
}