Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- ###################################FUNCTION###################################################
- def encryption_and_decryption(chos):
- if chos=="e":
- e=random.randint(0,24)
- letters="abcdefghijklmnopqrstuvwxyz"
- encryp_dic={letters[i]:letters[(i+e)%len(letters)] for i in range(len(letters))}
- elif chos=="d":
- d=random.randint(0,24)
- letters="abcdefghijklmnopqrstuvwxyz"
- encryp_dic={letters[i]:letters[(i+d)%len(letters)] for i in range(len(letters))}
- return encryp_dic
- ###################################FUNCTION###################################################
- def ignore_not_alpha(lst):
- lst1=[]
- lst2=[]
- for i in lst:
- for j in range(len(lst)):
- a=lst[j]
- a=a.replace("\n","")
- a=a.replace("-"," ")
- lst1.append(a)
- for i in lst1:
- flag=""
- for x in i:
- if x.isalpha() or x.isspace():
- flag+=x
- flag=flag.lower()
- lst2.append(flag)
- return lst2
- ###################################FUNCTION###################################################
- def choose_e(chos):
- encrypt=(encryption_and_decryption(chos))
- plaintext=open("plaintext.txt","r")
- lst=plaintext.readlines()
- plaintext.close()
- cipher=(ignore_not_alpha(lst))
- ciphertext=open("ciphertext.txt","w")
- keytext=open("key.txt","w")
- keys=[i for i in encrypt.values()]
- lst_secret=[]
- for val in cipher:
- secret=""
- for n in range(len(val)):
- if val[n]==" ":
- secret+=val[n]
- else:
- code=val[n]
- secret+=(encrypt[code])
- lst_secret.append(secret)
- for index in lst_secret:
- ciphertext.write(index)
- ciphertext.write("\n")
- ciphertext.close()
- for i in keys:
- keytext.write(i)
- keytext.close()
- ###################################FUNCTION###################################################
- def choose_d(chos):
- choose_e(chos)
- dictionary=(encryption_and_decryption(chos))
- keytext=open("key.txt","r").read()
- value=[i for i in keytext]
- keys=[i for i in dictionary]
- decrypt=(dictionary_swap(value, keys))
- ciphertext=open("ciphertext.txt","r")
- lst1=ciphertext.readlines()
- cipher=[]
- for i in range(len(lst1)):
- val=lst1[i].replace("\n","")
- cipher.append(val)
- translation=(decrypt_file(cipher, decrypt))
- decrypted_text=open("decrypted.txt","w")
- for i in translation:
- decrypted_text.write(i)
- decrypted_text.write("\n")
- decrypted_text.close()
- def dictionary_swap(lst1, lst2):
- new_dic={}
- for i in range(len(lst1)):
- new_dic[lst1[i]] = lst2[i]
- return new_dic
- ###################################FUNCTION###################################################
- def decrypt_file(decryp, dictionary):
- decoding=[]
- for val in decryp:
- translate=""
- for x in range(len(val)):
- if val[x]==" ":
- translate+=val[x]
- else:
- code=val[x]
- translate+=(dictionary[code])
- decoding.append(translate)
- return decoding
- ###################################FUNCTION###################################################
- ###################################main#######################################################
- choice=input("please enter the letter 'e' for encrypt, or 'd' for decrypt : " )
- if choice == "e":
- encrypt=(choose_e(choice))
- elif choice=="d":
- decrypt=(choose_d(choice))
- else:
- print("we are done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement