Advertisement
teslariu

Untitled

Jan 7th, 2021
71
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.59 KB | None
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. #Enigma Encoder - www.101computing.net/enigma-encoder/
  5.  
  6. # ----------------- Enigma Settings -----------------
  7. rotors = ("I","II","III")
  8. reflector = "UKW-B"
  9. ringSettings ="ABC"
  10. ringPositions = "DEF"
  11. plugboard = "AT BS DE FM IR KN LZ OW PV XY"
  12. # ---------------------------------------------------
  13.  
  14. def caesarShift(str, amount):
  15.     output = ""
  16.  
  17.     for i in range(0,len(str)):
  18.         c = str[i]
  19.         code = ord(c)
  20.         if ((code >= 65) and (code <= 90)):
  21.             c = chr(((code - 65 + amount) % 26) + 65)
  22.         output = output + c
  23.    
  24.     return output
  25.  
  26. def encode(plaintext):
  27.   global rotors, reflector,ringSettings,ringPositions,plugboard
  28.   #Enigma Rotors and reflectors
  29.   rotor1 = "EKMFLGDQVZNTOWYHXUSPAIBRCJ"
  30.   rotor1Notch = "Q"
  31.   rotor2 = "AJDKSIRUXBLHWTMCQGZNPYFVOE"
  32.   rotor2Notch = "E"
  33.   rotor3 = "BDFHJLCPRTXVZNYEIWGAKMUSQO"
  34.   rotor3Notch = "V"
  35.   rotor4 = "ESOVPZJAYQUIRHXLNFTGKDCMWB"
  36.   rotor4Notch = "J"
  37.   rotor5 = "VZBRGITYUPSDNHLXAWMJQOFECK"
  38.   rotor5Notch = "Z"
  39.  
  40.   rotorDict = {"I":rotor1,"II":rotor2,"III":rotor3,"IV":rotor4,"V":rotor5}
  41.   rotorNotchDict = {"I":rotor1Notch,"II":rotor2Notch,"III":rotor3Notch,"IV":rotor4Notch,"V":rotor5Notch}  
  42.  
  43.   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"}
  44.   reflectorC = {"A":"F","F":"A","B":"V","V":"B","C":"P","P":"C","D":"J","J":"D","E":"I","I":"E","G":"O","O":"G","H":"Y","Y":"H","K":"R","R":"K","L":"Z","Z":"L","M":"X","X":"M","N":"W","W":"N","Q":"T","T":"Q","S":"U","U":"S"}
  45.  
  46.   alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  47.   rotorANotch = False
  48.   rotorBNotch = False
  49.   rotorCNotch = False
  50.  
  51.   if reflector=="UKW-B":
  52.     reflectorDict = reflectorB
  53.   else:
  54.     reflectorDict = reflectorC
  55.  
  56.   #A = Left,  B = Mid,  C=Right
  57.   rotorA = rotorDict[rotors[0]]
  58.   rotorB = rotorDict[rotors[1]]
  59.   rotorC = rotorDict[rotors[2]]
  60.   rotorANotch = rotorNotchDict[rotors[0]]
  61.   rotorBNotch = rotorNotchDict[rotors[1]]
  62.   rotorCNotch = rotorNotchDict[rotors[2]]
  63.  
  64.   rotorALetter = ringPositions[0]
  65.   rotorBLetter = ringPositions[1]
  66.   rotorCLetter = ringPositions[2]
  67.  
  68.   rotorASetting = ringSettings[0]
  69.   offsetASetting = alphabet.index(rotorASetting)
  70.   rotorBSetting = ringSettings[1]
  71.   offsetBSetting = alphabet.index(rotorBSetting)
  72.   rotorCSetting = ringSettings[2]
  73.   offsetCSetting = alphabet.index(rotorCSetting)
  74.  
  75.   rotorA = caesarShift(rotorA,offsetASetting)
  76.   rotorB = caesarShift(rotorB,offsetBSetting)
  77.   rotorC = caesarShift(rotorC,offsetCSetting)
  78.  
  79.   if offsetASetting>0:
  80.     rotorA = rotorA[26-offsetASetting:] + rotorA[0:26-offsetASetting]
  81.   if offsetBSetting>0:
  82.     rotorB = rotorB[26-offsetBSetting:] + rotorB[0:26-offsetBSetting]
  83.   if offsetCSetting>0:
  84.     rotorC = rotorC[26-offsetCSetting:] + rotorC[0:26-offsetCSetting]
  85.  
  86.   ciphertext = ""
  87.  
  88.   #Converplugboard settings into a dictionary
  89.   plugboardConnections = plugboard.upper().split(" ")
  90.   plugboardDict = {}
  91.   for pair in plugboardConnections:
  92.     if len(pair)==2:
  93.       plugboardDict[pair[0]] = pair[1]
  94.       plugboardDict[pair[1]] = pair[0]
  95.  
  96.   plaintext = plaintext.upper()  
  97.   for letter in plaintext:
  98.     encryptedLetter = letter  
  99.    
  100.     if letter in alphabet:
  101.       #Rotate Rotors - This happens as soon as a key is pressed, before encrypting the letter!
  102.       rotorTrigger = False
  103.       #Third rotor rotates by 1 for every key being pressed
  104.       if rotorCLetter == rotorCNotch:
  105.         rotorTrigger = True
  106.       rotorCLetter = alphabet[(alphabet.index(rotorCLetter) + 1) % 26]
  107.       #Check if rotorB needs to rotate
  108.       if rotorTrigger:
  109.         rotorTrigger = False
  110.         if rotorBLetter == rotorBNotch:
  111.           rotorTrigger = True
  112.         rotorBLetter = alphabet[(alphabet.index(rotorBLetter) + 1) % 26]
  113.  
  114.         #Check if rotorA needs to rotate
  115.         if (rotorTrigger):
  116.           rotorTrigger = False
  117.           rotorALetter = alphabet[(alphabet.index(rotorALetter) + 1) % 26]
  118.              
  119.       else:
  120.           #Check for double step sequence!
  121.         if rotorBLetter == rotorBNotch:
  122.           rotorBLetter = alphabet[(alphabet.index(rotorBLetter) + 1) % 26]
  123.           rotorALetter = alphabet[(alphabet.index(rotorALetter) + 1) % 26]
  124.        
  125.       #Implement plugboard encryption!
  126.       if letter in plugboardDict.keys():
  127.         if plugboardDict[letter]!="":
  128.           encryptedLetter = plugboardDict[letter]
  129.    
  130.       #Rotors & Reflector Encryption
  131.       offsetA = alphabet.index(rotorALetter)
  132.       offsetB = alphabet.index(rotorBLetter)
  133.       offsetC = alphabet.index(rotorCLetter)
  134.  
  135.       # Wheel 3 Encryption
  136.       pos = alphabet.index(encryptedLetter)
  137.       let = rotorC[(pos + offsetC)%26]
  138.       pos = alphabet.index(let)
  139.       encryptedLetter = alphabet[(pos - offsetC +26)%26]
  140.      
  141.       # Wheel 2 Encryption
  142.       pos = alphabet.index(encryptedLetter)
  143.       let = rotorB[(pos + offsetB)%26]
  144.       pos = alphabet.index(let)
  145.       encryptedLetter = alphabet[(pos - offsetB +26)%26]
  146.      
  147.       # Wheel 1 Encryption
  148.       pos = alphabet.index(encryptedLetter)
  149.       let = rotorA[(pos + offsetA)%26]
  150.       pos = alphabet.index(let)
  151.       encryptedLetter = alphabet[(pos - offsetA +26)%26]
  152.      
  153.       # Reflector encryption!
  154.       if encryptedLetter in reflectorDict.keys():
  155.         if reflectorDict[encryptedLetter]!="":
  156.           encryptedLetter = reflectorDict[encryptedLetter]
  157.      
  158.       #Back through the rotors
  159.       # Wheel 1 Encryption
  160.       pos = alphabet.index(encryptedLetter)
  161.       let = alphabet[(pos + offsetA)%26]
  162.       pos = rotorA.index(let)
  163.       encryptedLetter = alphabet[(pos - offsetA +26)%26]
  164.      
  165.       # Wheel 2 Encryption
  166.       pos = alphabet.index(encryptedLetter)
  167.       let = alphabet[(pos + offsetB)%26]
  168.       pos = rotorB.index(let)
  169.       encryptedLetter = alphabet[(pos - offsetB +26)%26]
  170.      
  171.       # Wheel 3 Encryption
  172.       pos = alphabet.index(encryptedLetter)
  173.       let = alphabet[(pos + offsetC)%26]
  174.       pos = rotorC.index(let)
  175.       encryptedLetter = alphabet[(pos - offsetC +26)%26]
  176.      
  177.       #Implement plugboard encryption!
  178.       if encryptedLetter in plugboardDict.keys():
  179.         if plugboardDict[encryptedLetter]!="":
  180.           encryptedLetter = plugboardDict[encryptedLetter]
  181.  
  182.     ciphertext = ciphertext + encryptedLetter
  183.  
  184.   return ciphertext
  185.  
  186. #Main Program Starts Here
  187. print("  ##### Enigma Encoder #####")
  188. print("")
  189. plaintext = input("Enter text to encode or decode:\n")
  190. ciphertext = encode(plaintext)
  191.  
  192. print("\nEncoded text: \n " + ciphertext)
  193.  
Advertisement
RAW Paste Data Copied
Advertisement