-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenigma.sh
executable file
·123 lines (105 loc) · 2.13 KB
/
enigma.sh
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
#!/bin/bash
# (c) 2015 Charles Bailey
alphabet=ABCDEFGHIJKLMNOPQRSTUVWXYZ
swap_fd () {
eval "exec {tmp}>&${!1} {$1}>&${!2}"
eval "exec {$2}>&$tmp {tmp}>&-"
}
open_files () {
for (( i=0; i != ${#alphabet}; i++ ))
do
letter=${alphabet:i:1}
eval "exec {orig$letter}>lamp$letter"
done
}
init_fds () {
for (( i=0; i != ${#alphabet}; i++ ))
do
letter=${alphabet:i:1}
eval "exec {fd$letter}>&\$orig$letter"
done
}
close_fds () {
for (( i=0; i != ${#alphabet}; i++ ))
do
letter=${alphabet:i:1}
eval "exec {fd$letter}>&-"
done
}
reflector () {
# Umkehrwalze B: AY BR CU DH EQ FS GL IP JX KN MO TZ VW
swap_fd fdA fdY
swap_fd fdB fdR
swap_fd fdC fdU
swap_fd fdD fdH
swap_fd fdE fdQ
swap_fd fdF fdS
swap_fd fdG fdL
swap_fd fdI fdP
swap_fd fdJ fdX
swap_fd fdK fdN
swap_fd fdM fdO
swap_fd fdT fdZ
swap_fd fdV fdW
}
move_to_tmp_fds () {
for (( i=0; i != ${#alphabet}; i++ ))
do
letter=${alphabet:i:1}
eval "exec {tmp$letter}>&\$fd$letter {fd$letter}>&-"
done
}
rotor_fwd () {
move_to_tmp_fds
for (( i=0; i != ${#alphabet}; i++ ))
do
letter1=${alphabet:i:1}
idx=$(expr index $alphabet ${1:(i+$2)%26:1})
letter2=${alphabet:(idx+25-$2)%26:1}
eval "exec {fd$letter2}>&\$tmp$letter1 {tmp$letter1}>&-"
done
}
rotor_rev () {
move_to_tmp_fds
for (( i=0; i != ${#alphabet}; i++ ))
do
letter1=${alphabet:i:1}
idx=$(expr index $alphabet ${1:(i+$2)%26:1})
letter2=${alphabet:(idx+25-$2)%26:1}
eval "exec {fd$letter1}>&\$tmp$letter2 {tmp$letter2}>&-"
done
}
scramble_outputs () {
init_fds
rotor_fwd "$1" "$4"
rotor_fwd "$2" "$5"
rotor_fwd "$3" "$6"
reflector
rotor_rev "$3" "$6"
rotor_rev "$2" "$5"
rotor_rev "$1" "$4"
}
rotor1=EKMFLGDQVZNTOWYHXUSPAIBRCJ
rotor2=AJDKSIRUXBLHWTMCQGZNPYFVOE
rotor3=BDFHJLCPRTXVZNYEIWGAKMUSQO
rotor4=ESOVPZJAYQUIRHXLNFTGKDCMWB
rotor5=VZBRGITYUPSDNHLXAWMJQOFECK
r1pos=0
r2pos=0
r3pos=0
next_pos () { (( r1pos=(r1pos+1)%26 )); }
next () {
next_pos
scramble_outputs $rotor1 $rotor2 $rotor3\
$r1pos $r2pos $r3pos
}
encode () {
next
eval "printf '*' >&\$fd$1"
close_fds
}
open_files
while read -n 1 ch
do
expr index $alphabet "$ch" >/dev/null && encode "$ch"
done