Advertisement
Guest User

Untitled

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