Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 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.     res_text = ""
  13.     result = ""
  14.     for i in text:
  15.         if i in rotor0:
  16.             res_text += i
  17.     for i in res_text:
  18.         ind = rotor0.index(i)
  19.         ind = ind+shift3
  20.         while (ind>len(rotor0)) or (ind<len(rotor0)-1):
  21.             if ind>len(rotor0)-1:
  22.                 ind = ind-len(rotor0)
  23.             if ind<0:
  24.                 ind = ind+len(rotor0)
  25.         symbol = ""
  26.         if rot3 == 3:
  27.             symbol = rotor3[ind]
  28.         if rot2 == 2:
  29.             if rot3 == 3:
  30.                 ind = rotor0.index(symbol)
  31.                 symbol = rotor2[ind]
  32.             else:
  33.                 symbol = rotor2[ind]
  34.         if rot1 == 1:
  35.             if rot2 == 2:
  36.                 ind = rotor0.index(symbol)
  37.                 symbol = rotor1[ind]
  38.             else:
  39.                 symbol = rotor1[ind]
  40.         if ref == 1:
  41.             symbol = reflectorB[symbol]
  42.         ind = rotor1.index(symbol)
  43.         symbol = rotor0[ind]
  44.         if rot2 == 2:
  45.             ind = rotor2.index(symbol)
  46.             symbol = rotor0[ind]
  47.         if rot3 == 3:
  48.             ind = rotor3.index(symbol)
  49.             symbol = rotor0[ind]
  50.         result += symbol
  51.     return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement