Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #enigma machine
- #---------------
- # Usage:
- # to encypt use python enigma.py string-text int-key1 int-key2 int-key3 encrypt
- # to decrype use python enigma.py string-text int-key1 int-key2 int-key3 decrypt
- #
- # when encrypting set keys to 0 to select random key
- #
- #
- #------
- import random
- import sys
- #global vars
- key1 = 0
- key2 = 0
- key3 = 0
- 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']
- def input_string(mode, T):
- T = T.lower()
- RT = [] # creating a list for later
- LT = list(T)
- print LT
- for a in range(0, len(LT)): # remove spaces
- if LT[a] == " ":
- LT[a] = '' #---
- for a in range(0, len(LT)):
- if mode == "encrypt":
- RT.append(encrypter(LT[a])) #encryps each letter and appends it to a new list
- elif mode == "decrypt":
- RT.append(decrypter(LT[a])) #encryps each letter and appends it to a new list
- return RT
- def key_increment():
- global key1
- global key2
- global key3
- key1 +=1
- if key1 > 25:
- key1 = 0
- key2 += 1
- if key2 > 25:
- key2 = 0
- key3 += 1
- if key3 >25:
- key3 = 0
- def encrypter(L):
- global key1
- global key2
- global key3
- global alpha
- encryption = key3 * key2 * key1 # creates a number between 0 and 15625
- pos = alpha.index(L) # finds the position of the letter in the alphabet
- eL = alpha[(pos + encryption)%26] # adds the result of the keys to the position of the letter while looping arround the alphabet
- key_increment() #increments the key by 1
- return eL # returns the now encrypted letter
- def decrypter(L): # same as encrypt
- global key1
- global key2
- global key3
- global alpha
- encryption = key3 * key2 * key1
- pos = alpha.index(L)
- eL = alpha[(pos - encryption)%26] # simply reverses the modulo rather than adding it
- key_increment()
- return eL
- def main(mode, T, k1, k2, k3):
- global key1
- global key2
- global key3
- enT = input_string(mode, T)
- print("input is %s k1 = %s k2 = %s k3 = %s and output is %s" %(T, k1, k2, k3, enT))
- if __name__ == "__main__":
- try:
- T = sys.argv[1] #gathering system variables
- key1 = int(sys.argv[2])
- key2 = int(sys.argv[3])
- key3 = int(sys.argv[4])
- mode = sys.argv[5]#---
- except:
- print("incorerct format. please read code for correct format.")
- sys.exit()
- if(key1 == 0 and key2 == 0 and key3 == 0): # sets keys to random values if set to 0
- key1 = random.randint(1, 25)
- key2 = random.randint(1, 25)
- key3 = random.randint(1, 25)#---
- #try:
- main(mode, T, key1, key2, key3)
- #except:
- # print("error in main()")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement