Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.84 KB | None | 0 0
  1. import re
  2. def enigma(text, ref, rot1, shift1, rot2, shift2, rot3, shift3):
  3.     text = text.upper()
  4.     rotor0 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  5.     if rot1 == 1:
  6.         rotor1 = "EKMFLGDQVZNTOWYHXUSPAIBRCJ"
  7.     if rot2 == 2:
  8.         rotor2 = "AJDKSIRUXBLHWTMCQGZNPYFVOE"
  9.     if rot3 == 3:
  10.         rotor3 = "BDFHJLCPRTXVZNYEIWGAKMUSQO"
  11.     if ref == 1:
  12.         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" }
  13.     result = ""
  14.     res_text = re.sub(r'[^A-Z]', '', text)
  15.     for i in res_text:
  16.         symbol = i
  17.         ind = rotor0.index(i)+shift3
  18.         while (ind>len(rotor0)) or (ind<len(rotor0)-1):
  19.             if ind>len(rotor0)-1:
  20.                 ind = ind-len(rotor0)
  21.             elif ind<0:
  22.                 ind = ind+len(rotor0)
  23.             else:
  24.                 break
  25.         symbol = rotor0[ind]
  26.         if rot3 == 3:
  27.             symbol = rotor3[ind]
  28.             ind = rotor0.index(symbol)
  29.             if ind +(shift2-shift3)>len(rotor0)-1:
  30.                 ind = (ind +(shift2-shift3))-len(rotor0)
  31.             elif ind +(shift2-shift3)<0:
  32.                 ind = ind +(shift2-shift3)+len(rotor0)
  33.             else:
  34.                 ind = ind+(shift2-shift3)
  35.             symbol = rotor0[ind]
  36.         if rot2 == 2:
  37.             symbol = rotor2[ind]
  38.             if rot3 == 3:
  39.                 if rotor0.index(symbol)+(-shift2+shift1)<0:
  40.                     ind = rotor0.index(symbol)+(-shift2+shift1)+len(rotor0)
  41.                 elif rotor0.index(symbol)+(-shift2+shift1)>len(rotor0)-1:
  42.                     ind = len(rotor0)-rotor0.index(symbol)+(-shift2+shift1)
  43.                 else:
  44.                     ind = rotor0.index(symbol)+(-shift2+shift1)
  45.                 symbol = rotor0[ind]
  46.         if rot1 == 1:
  47.             symbol = rotor1[ind]
  48.             if rot2 == 2:
  49.                 if rotor0.index(symbol)-shift1<0:
  50.                     ind = rotor0.index(symbol)-shift1+len(rotor0)
  51.                 elif rotor0.index(symbol)-shift1>len(rotor0)-1:
  52.                     ind = len(rotor0)-(rotor0.index(symbol)-shift1)
  53.                 else:
  54.                     ind = rotor0.index(symbol)-shift1
  55.                 symbol = rotor0[ind]
  56.         if ref == 1:
  57.             symbol = reflectorB[symbol]
  58.         if rot1 == 1:
  59.             if rotor0.index(symbol)+shift1>len(rotor0)-1:
  60.                 ind = len(rotor0)-(rotor0.index(symbol)+shift1)
  61.             elif rotor0.index(symbol)+shift1<0:
  62.                 ind = rotor0.index(symbol)+shift1+len(rotor0)
  63.             else:
  64.                 ind = rotor0.index(symbol)+shift1
  65.             symbol = rotor0[ind]
  66.             ind = rotor1.index(symbol)
  67.             symbol = rotor0[ind]
  68.         if rot2 == 2:
  69.             if rotor0.index(symbol)+(shift2-shift1)>len(rotor0)-1:
  70.                 ind = len(rotor0)-(rotor0.index(symbol)+(shift2-shift1))
  71.             elif rotor0.index(symbol)+(shift2-shift1)<0:
  72.                 ind = rotor0.index(symbol)+(shift2-shift1)+len(rotor0)
  73.             else:
  74.                 ind = rotor0.index(symbol)+(shift2-shift1)
  75.             symbol = rotor0[ind]
  76.             ind = rotor2.index(symbol)
  77.             symbol = rotor0[ind]
  78.         if rot3 == 3:
  79.             if rotor0.index(symbol)-shift3>len(rotor0)-1:
  80.                 ind = len(rotor0)-(rotor0.index(symbol)-shift3)
  81.             elif rotor0.index(symbol)-shift3<0:
  82.                 ind = rotor0.index(symbol)-shift3+len(rotor0)
  83.             else:
  84.                 ind = rotor0.index(symbol)-shift3
  85.             symbol = rotor0[ind]
  86.             ind = rotor3.index(symbol)
  87.             symbol = rotor0[ind]
  88.         symbol = rotor0[ind-shift3]
  89.         result += symbol
  90.     return result
  91. 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