Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- 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" }
- result = ""
- res_text = re.sub(r'[^A-Z]', '', text)
- for i in res_text:
- symbol = i
- ind = rotor0.index(i)+shift3
- while (ind>len(rotor0)) or (ind<len(rotor0)-1):
- if ind>len(rotor0)-1:
- ind = ind-len(rotor0)
- elif ind<0:
- ind = ind+len(rotor0)
- else:
- break
- symbol = rotor0[ind]
- if rot3 == 3:
- symbol = rotor3[ind]
- ind = rotor0.index(symbol)
- if ind +(shift2-shift3)>len(rotor0)-1:
- ind = (ind +(shift2-shift3))-len(rotor0)
- elif ind +(shift2-shift3)<0:
- ind = ind +(shift2-shift3)+len(rotor0)
- else:
- ind = ind+(shift2-shift3)
- symbol = rotor0[ind]
- if rot2 == 2:
- symbol = rotor2[ind]
- if rot3 == 3:
- if rotor0.index(symbol)+(-shift2+shift1)<0:
- ind = rotor0.index(symbol)+(-shift2+shift1)+len(rotor0)
- elif rotor0.index(symbol)+(-shift2+shift1)>len(rotor0)-1:
- ind = len(rotor0)-rotor0.index(symbol)+(-shift2+shift1)
- else:
- ind = rotor0.index(symbol)+(-shift2+shift1)
- symbol = rotor0[ind]
- if rot1 == 1:
- symbol = rotor1[ind]
- if rot2 == 2:
- if rotor0.index(symbol)-shift1<0:
- ind = rotor0.index(symbol)-shift1+len(rotor0)
- elif rotor0.index(symbol)-shift1>len(rotor0)-1:
- ind = len(rotor0)-(rotor0.index(symbol)-shift1)
- else:
- ind = rotor0.index(symbol)-shift1
- symbol = rotor0[ind]
- if ref == 1:
- symbol = reflectorB[symbol]
- if rot1 == 1:
- if rotor0.index(symbol)+shift1>len(rotor0)-1:
- ind = len(rotor0)-(rotor0.index(symbol)+shift1)
- elif rotor0.index(symbol)+shift1<0:
- ind = rotor0.index(symbol)+shift1+len(rotor0)
- else:
- ind = rotor0.index(symbol)+shift1
- symbol = rotor0[ind]
- ind = rotor1.index(symbol)
- symbol = rotor0[ind]
- if rot2 == 2:
- if rotor0.index(symbol)+(shift2-shift1)>len(rotor0)-1:
- ind = len(rotor0)-(rotor0.index(symbol)+(shift2-shift1))
- elif rotor0.index(symbol)+(shift2-shift1)<0:
- ind = rotor0.index(symbol)+(shift2-shift1)+len(rotor0)
- else:
- ind = rotor0.index(symbol)+(shift2-shift1)
- symbol = rotor0[ind]
- ind = rotor2.index(symbol)
- symbol = rotor0[ind]
- if rot3 == 3:
- if rotor0.index(symbol)-shift3>len(rotor0)-1:
- ind = len(rotor0)-(rotor0.index(symbol)-shift3)
- elif rotor0.index(symbol)-shift3<0:
- ind = rotor0.index(symbol)-shift3+len(rotor0)
- else:
- ind = rotor0.index(symbol)-shift3
- symbol = rotor0[ind]
- ind = rotor3.index(symbol)
- symbol = rotor0[ind]
- symbol = rotor0[ind-shift3]
- result += symbol
- return result
- enigma(text="A", ref=1, rot1=1, shift1=1, rot2=2, shift2=2, rot3=3, shift3=1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement