Advertisement
Guest User

enigma.py

a guest
Nov 12th, 2017
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. #enigma machine
  2. #---------------
  3. # Usage:
  4. # to encypt use python enigma.py string-text int-key1 int-key2 int-key3 encrypt
  5. # to decrype use python enigma.py string-text int-key1 int-key2 int-key3 decrypt
  6. #
  7. # when encrypting set keys to 0 to select random key
  8. #
  9. #
  10. #------
  11. import random
  12. import sys
  13. #global vars
  14. key1 = 0
  15. key2 = 0
  16. key3 = 0
  17. alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
  18.  
  19. def input_string(mode, T):
  20.     T = T.lower()
  21.     RT = [] # creating a list for later
  22.     LT = list(T)
  23.     print LT
  24.     for a in range(0, len(LT)): # remove spaces
  25.         if LT[a] == " ":
  26.             LT[a] = '' #---
  27.     for a in range(0, len(LT)):
  28.         if mode == "encrypt":
  29.             RT.append(encrypter(LT[a])) #encryps each letter and appends it to a new list
  30.         elif mode == "decrypt":
  31.             RT.append(decrypter(LT[a])) #encryps each letter and appends it to a new list
  32.        
  33.     return RT
  34.  
  35. def key_increment():
  36.     global key1
  37.     global key2
  38.     global key3
  39.     key1 +=1
  40.     if key1 > 25:
  41.         key1 = 0
  42.         key2 += 1
  43.         if key2 > 25:
  44.             key2 = 0
  45.             key3 += 1
  46.             if key3 >25:
  47.                 key3 = 0
  48.  
  49. def encrypter(L):
  50.     global key1
  51.     global key2
  52.     global key3
  53.     global alpha
  54.     encryption = key3 * key2 * key1 # creates a number between 0 and 15625
  55.     pos = alpha.index(L) # finds the position of the letter in the alphabet
  56.     eL = alpha[(pos + encryption)%26] # adds the result of the keys to the position of the letter while looping arround the alphabet
  57.     key_increment() #increments the key by 1
  58.     return eL # returns the now encrypted letter
  59.    
  60.  
  61. def decrypter(L): # same as encrypt
  62.     global key1
  63.     global key2
  64.     global key3
  65.     global alpha
  66.     encryption = key3 * key2 * key1
  67.     pos = alpha.index(L)
  68.     eL = alpha[(pos - encryption)%26] # simply reverses the modulo rather than adding it
  69.     key_increment()
  70.     return eL
  71.  
  72. def main(mode, T, k1, k2, k3):
  73.     global key1
  74.     global key2
  75.     global key3
  76.     enT = input_string(mode, T)
  77.     print("input is %s k1 = %s k2 = %s k3 = %s and output is %s" %(T, k1, k2, k3, enT))
  78.  
  79. if __name__ == "__main__":
  80.     try:
  81.         T = sys.argv[1] #gathering system variables
  82.         key1 = int(sys.argv[2])
  83.         key2 = int(sys.argv[3])
  84.         key3 = int(sys.argv[4])
  85.         mode = sys.argv[5]#---
  86.     except:
  87.         print("incorerct format. please read code for correct format.")
  88.         sys.exit()
  89.     if(key1 == 0 and key2 == 0 and key3 == 0): # sets keys to random values if set to 0
  90.         key1 = random.randint(1, 25)
  91.         key2 = random.randint(1, 25)
  92.         key3 = random.randint(1, 25)#---
  93.     #try:
  94.     main(mode, T, key1, key2, key3)
  95.     #except:
  96.     #   print("error in main()")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement