Code:
import re
key = {
#pos 0
0: {
"a": "m",
"b": "m",
"c": "m",
"d": "f",
"e": "F",
"f": "k",
"g": "K",
"h": "n",
"i": "T",
"k": "t",
"l": "q",
"m": "b",
"n": "fi",
"o": "fa",
"p": "Fi",
"r": "Ki",
"s": "Ka",
"t": "Ti",
"u": "TI",
"v": "TI",
"w": "na",
"y": "qii",
},
#pos 1
1: {
"a": "m",
"b": "m",
"c": "m",
"d": "i",
"e": "a",
"f": "o",
"g": "ii",
"h": "iii",
"i": "e",
"k": "E",
"l": "v",
"m": "z",
"n": "ai",
"o": "ae",
"p": "ia",
"r": "oo",
"s": "ei",
"t": "ii",
"u": "aa",
"v": "aa",
"w": "ee",
"x": "iii",
"y": "ooi",
"z": "iiu",
},
#pos 2
2: {
"a": "z",
"b": "x",
"c": "r",
"d": "l",
"e": "g",
"f": "k",
"g": "K",
"h": "n",
"i": "T",
"k": "t",
"l": "q",
"m": "b",
"n": "aei",
"o": "iea",
"p": "ouy",
"r": "ean",
"s": "nuy",
"t": "yun",
"u": "pen",
"w": "ben",
"x": "vinn",
"y": "uYnh",
"z": "YNh",
},
}
ciphertext = []
current_pos = 0
pos = 0
plaintext = ("Enigma has an electromechanical rotor mechanism that scrambles the 26 letters of the alphabet. In typical use, one person enters text on the Enigma's keyboard and another person writes down which of 26 lights above the keyboard lights up at each key press. If plain text is entered, the lit-up letters are the encoded ciphertext. Entering ciphertext transforms it back into readable plaintext. The rotor mechanism changes the electrical connections between the keys and the lights with each keypress. The security of the system depends on a set of machine settings that were generally changed daily during the war, based on secret key lists distributed in advance, and on other settings that were changed for each message. The receiving station has to know and use the exact settings employed by the transmitting station to successfully decrypt a message.")
plaintext = plaintext.lower()
plaintext = re.sub(r'[^A-Za-z]', '', plaintext) #get rid of non-letters
# Encrypt the string character by character
for char in plaintext:
ciphertext.append(key[current_pos][char])
# Cycle position: 0 -> 1 -> 2 -> 0 ...
current_pos = (current_pos + 1) % 3
pos = pos + 1
if current_pos == 0:
ciphertext.append(" ")
out = "".join(ciphertext)
original = "FaiT Kzz nmnuy maig qar Tiooiea bar nmaei Tmz qooiea Tiaeean bar nmaei Teib Tiiiiz Tieir Kimb mvg Kaiin Fvg Tiiig Kieiiea kiin Fmq Fiiiiz mayun Taiyun qiiiaT mmq TIeig faaig Fiaean Kaaeaei Faiyun Foonuy Tiavinn Tiaeaei Tiiiig FaiT Kzz KaEg qiimiea mool mail maiiea Tiiiig Kiiag Kieiiea fieeean Tiig Kaiiea naaiben ner naek qeK niinuy mmiea TIayun nat Fooix famean fvT Kiiiyun Kaaaouy miig mmn tauYnh Fioog KaeiT kiaq meaei Tiavinn Tienuy Faiyun Foog fiin FvT Tiaaouy qayun Tiaean Kamean Fiin Faaei mael Fir Tian Fooyun Fiiiyun Faiyun FooT fiiir Tian Fooyun Fiiiyun Tiooz fieik faoob Kaeyun mmr teaei Tiaeean Fml mmq Fiaq meaei Tiavinn Tiiin Fooiea Tiaeean bar nmaei Teib miiiz fiiig Kaiin Faq Fmyun Kier mvr faaiaei Fmyun Taeaei Kamg Tieeg Faiyun nat Fooinuy mail Tiiiig qeK niinuy naeyun naz miiit Fooiouy Kianuy Kaiin Feig maaean TiiuYnh faoyun nanuy qiieiyun Fzl Fiag fiinuy faaiz Kaayun faob mmn Taig Kaayun Tieaei Keiyun nmyun naaean Fiig fiaean mvq qiimn maiK Fil meq qiiipen Kieaei Kiin Feez Kimz Kaal faainuy Fmean Fiit Fooiq Teiyun KaiT Kaiiean Tmpen Tial Taiz faaz fimg mail faaiiea Tiiiig Kieig TiiiT fiiinuy Tiiiiz Tieeg Kiar nmaei Kal kaeean Fmr nzg Kaeiz Kayun naean Fmg TaaT fiiinuy Timyun Taeaei nmnuy Tiaet fiaeben mail TIeig Tiiiig Fiiiz miinuy Fiiyun TaiK Kaab Fiviea qiial mooiyun nayun Kimaei KazT TiiiT fiiinuy Timyun Taeaei Tiaenuy TImr Feinuy kaaq qooil Fmean qiiiayun mzg Kaeiz Ka"
print(out)
print ("\nMatch:" + str(out == original))