-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
70 lines (64 loc) · 2.08 KB
/
main.py
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
alphabet= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
rotoropt=["EMSADNQPJWGCIZKOTLHUFYBVXR",#1
"LBNSIXPZKAMECOVDRHTYUWGFJQ",#2
"KGQWXITYPUROBDJVAFCMLHENSZ",#3
"TKBFUXORLSACVHNYJIZQPWDEGM",#4
"WYNLAEHVJKFIPQBXZTUCGSMORD",#5
"ZETKLGHCXOVNBPAYUIQFSWJMDR",#6
"QASMODBWTNLEFXRPHKICVUGJYZ",#7
"FITNGJQKCHSVYZXEMLURBWAOPD"#8
]
reflector="UKNEDTQFBRPOXWYZVSAGJLIMHC"
def getpair(letter, pairset):
for count in range(len(pairset)):
if pairset[count] == letter:
if count%2 == 0:
return pairset[count+1]
else:
return pairset[count-1]
return letter
def shiftRotor(rotor, amount):
newRotor = ""
for l in range(len(rotor)):
newRotor += rotor[(l+amount)%len(rotor)]
return newRotor
def stepRotors(keys):
keys[0]+=1
keys[0]%=26
if keys[0]==0:
keys[1]+=1
keys[1]%=26
if keys[1]==0:
keys[2]+=1
keys[2]%=26
return keys
def cipher(text, key):
keys = [*key.upper().split()[1]]
rotorarrangement = key.split()[0]
plugs = ""
if len(key.upper().split())>=3:
plugs = key.upper().split()[2]
rotors = []
for rotor in rotorarrangement:
rotors.append(rotoropt[int(rotor)-1])
keys[0] = alphabet.index(keys[0])
keys[1] = alphabet.index(keys[1])
keys[2] = alphabet.index(keys[2])
text = text.upper()
results = ""
for letter in text:
letter = getpair(letter, plugs)
if letter in alphabet:
a = alphabet.index(shiftRotor(rotors[0], keys[0])[alphabet.index(letter)])
b=shiftRotor(rotors[2],keys[2])[alphabet.index(shiftRotor(rotors[1],keys[1])[a])]
c = getpair(b, reflector)
d = shiftRotor(rotors[0],keys[0]).index(alphabet[shiftRotor(rotors[1],keys[1]).index(alphabet[shiftRotor(rotors[2], keys[2]).index(c)])])
e = alphabet[d]
e = getpair(e, plugs)
results += e
else:
results += letter
keys = stepRotors(keys)
return results
print("Based on the German Engima Machine")
print(cipher(input("Enter text\n"),input("Enter key (num num num [space] letter letter letter [space] plugboard pairs)\n")))