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" }
- if shift1 !=0:
- while shift1<0 or shift1>len(rotor1)-1:
- if shift1>len(rotor1):
- shift1 = shift1-len(rotor1)
- if shift1<0:
- shift1 = len(rotor1)+shift1
- rotor1 = rotor1[shift1:]+rotor1[0:shift1]
- if shift2 !=0:
- while shift2<0 or shift2>len(rotor2)-1:
- if shift2>len(rotor2):
- shift2 = shift2-len(rotor2)
- if shift2<0:
- shift2 = len(rotor2)+shift2
- rotor2 = rotor2[shift2:]+rotor2[0:shift2]
- if shift3 !=0:
- while shift3<0 or shift3>len(rotor3)-1:
- if shift3>len(rotor3):
- shift3 = shift3-len(rotor3)
- if shift3<0:
- shift3 = len(rotor3)+shift3
- rotor3 = rotor3[shift3:]+rotor3[0:shift3]
- res_text = ""
- result = ""
- for i in text:
- if i in rotor0:
- res_text += i
- for i in res_text:
- ind = rotor0.index(i)
- symbol = ""
- if rot3 == 3:
- symbol = rotor3[ind-(shift3-1)]
- if rot2 == 2:
- if rot3 == 3:
- ind = rotor0.index(symbol)
- symbol = rotor2[ind]
- else:
- symbol = rotor2[ind]
- if rot1 == 1:
- if rot2 == 2:
- ind = rotor0.index(symbol)
- symbol = rotor1[ind]
- else:
- symbol = rotor1[ind]
- symbol = reflectorB[symbol]
- if rot1 == 1:
- ind = rotor0.index(symbol)+shift1
- while (ind<0) or ind > (len(rotor0)-1):
- if ind>len(rotor3)-1:
- ind = ind - len(rotor0)
- if ind<0:
- ind = len(rotor0)-ind
- symbol = rotor0[ind]
- ind = rotor1.index(symbol)
- symbol = rotor0[ind]
- if rot2 == 2:
- ind = rotor0.index(symbol)+shift2
- while (ind<0) or ind > (len(rotor0)-1):
- if ind>len(rotor3)-1:
- ind = ind - len(rotor0)
- if ind<0:
- ind = len(rotor0)-ind
- symbol = rotor0[ind]
- ind = rotor2.index(symbol)
- symbol = rotor0[ind]
- if rot3 == 3:
- ind = rotor0.index(symbol)+shift3
- while (ind<0) or ind > (len(rotor0)-1):
- if ind>len(rotor3)-1:
- ind = ind - len(rotor0)
- if ind<0:
- ind = len(rotor0)-ind
- symbol = rotor0[ind]
- ind = rotor3.index(symbol)
- symbol = rotor0[ind]
- 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