Created
November 8, 2013 15:09
-
-
Save rd13/7372334 to your computer and use it in GitHub Desktop.
Enigma Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import string | |
_rotors = [ 'EKMFLGDQVZNTOWYHXUSPAIBRCJ', | |
'AJDKSIRUXBLHWTMCQGZNPYFVOE', | |
'BDFHJLCPRTXVZNYEIWGAKMUSQO' | |
] | |
_reflector = "YRUHQSLDPXNGOKMIEBFZCWVJAT" | |
_key = "ABC" | |
def li(c): | |
# Letter to index | |
return string.uppercase.index(c) | |
def il(i): | |
# Index to letter | |
return string.uppercase[i] | |
def crypt(ct): | |
L = li(_key[0]) | |
M = li(_key[1]) | |
R = li(_key[2]) | |
output = '' | |
for x in ct: | |
ct_letter = li(x) | |
R = (R + 1) % 26 | |
a = _rotors[2][(R + ct_letter) % 26] | |
b = _rotors[1][(M + li(a) - R) % 26] | |
c = _rotors[0][(L + li(b) - M) % 26] | |
ref = _reflector[(li(c) - L) % 26]; | |
d = (_rotors[0].index(il((li(ref) + L) % 26)) - L) % 26; | |
e = (_rotors[1].index(il((d + M) % 26)) - M) % 26; | |
f = il((_rotors[2].index(il((e + R) % 26)) - R) % 26); | |
output += f | |
return output | |
# for n in range(0, 10): | |
crypt('PZUFWDSASJGQGNRMAEODZJXQQKHSYGVUSGSU') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment