Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def enigma(text, ref, rot1, shift1, rot2, shift2, rot3, shift3):
- text = text.upper()
- rotor0 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- if rot1 == 1:
- rotor1 = "EKMFLGDQVZNTOWYHXUSPAIBRCJ"
- if rot2 == 2:
- rotor2 = "AJDKSIRUXBLHWTMCQGZNPYFVOE"
- if rot3 == 3:
- rotor3 = "BDFHJLCPRTXVZNYEIWGAKMUSQO"
- if ref == 1:
- reflectorB = {"A":"Y", "Y":"A","B":"R","R":"B","C":"U","U":"C","D":"H","H":"D","E":"Q","Q":"E","F":"S","S":"F","G":"L","L":"G","I":"P","P":"I","J":"X","X":"J","K":"N","N":"K","M":"O","O":"M","T":"Z","Z":"T","V":"W","W":"V" }
- res_text = ""
- result = ""
- for i in text:
- if i in rotor0:
- res_text += i
- for i in res_text:
- ind = rotor0.index(i)
- ind = ind+shift3
- while (ind>len(rotor0)) or (ind<len(rotor0)-1):
- if ind>len(rotor0)-1:
- ind = ind-len(rotor0)
- if ind<0:
- ind = ind+len(rotor0)
- symbol = ""
- if rot3 == 3:
- symbol = rotor3[ind+(shift2-shift3)]
- if rot2 == 2:
- if rot3 == 3:
- ind = rotor0.index(symbol)+(-shift2+shift1)
- symbol = rotor2[ind]
- else:
- symbol = rotor2[ind]
- if rot1 == 1:
- if rot2 == 2:
- ind = rotor0.index(symbol)-shift1
- symbol = rotor1[ind]
- else:
- symbol = rotor1[ind]
- if ref == 1:
- symbol = reflectorB[symbol]
- ind = rotor1.index(symbol)+shift1
- symbol = rotor0[ind]
- if rot2 == 2:
- ind = rotor2.index(symbol)+(shift2-shift1)
- symbol = rotor0[ind]
- if rot3 == 3:
- ind = rotor3.index(symbol)-shift3
- symbol = rotor0[ind]
- result += symbol
- return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement