Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. import random
  2. ###################################FUNCTION###################################################
  3. def encryption_and_decryption(chos):
  4. if chos=="e":
  5. e=random.randint(0,24)
  6. letters="abcdefghijklmnopqrstuvwxyz"
  7. encryp_dic={letters[i]:letters[(i+e)%len(letters)] for i in range(len(letters))}
  8. elif chos=="d":
  9. d=random.randint(0,24)
  10. letters="abcdefghijklmnopqrstuvwxyz"
  11. encryp_dic={letters[i]:letters[(i+d)%len(letters)] for i in range(len(letters))}
  12. return encryp_dic
  13. ###################################FUNCTION###################################################
  14. def ignore_not_alpha(lst):
  15. lst1=[]
  16. lst2=[]
  17. for i in lst:
  18. for j in range(len(lst)):
  19. a=lst[j]
  20. a=a.replace("\n","")
  21. a=a.replace("-"," ")
  22. lst1.append(a)
  23. for i in lst1:
  24. flag=""
  25. for x in i:
  26. if x.isalpha() or x.isspace():
  27. flag+=x
  28. flag=flag.lower()
  29. lst2.append(flag)
  30. return lst2
  31. ###################################FUNCTION###################################################
  32. def choose_e(chos):
  33. encrypt=(encryption_and_decryption(chos))
  34. plaintext=open("plaintext.txt","r")
  35. lst=plaintext.readlines()
  36. plaintext.close()
  37. cipher=(ignore_not_alpha(lst))
  38. ciphertext=open("ciphertext.txt","w")
  39. keytext=open("key.txt","w")
  40. keys=[i for i in encrypt.values()]
  41. lst_secret=[]
  42. for val in cipher:
  43. secret=""
  44. for n in range(len(val)):
  45. if val[n]==" ":
  46. secret+=val[n]
  47. else:
  48. code=val[n]
  49. secret+=(encrypt[code])
  50. lst_secret.append(secret)
  51.  
  52. for index in lst_secret:
  53. ciphertext.write(index)
  54. ciphertext.write("\n")
  55. ciphertext.close()
  56. for i in keys:
  57. keytext.write(i)
  58. keytext.close()
  59. ###################################FUNCTION###################################################
  60. def choose_d(chos):
  61. choose_e(chos)
  62. dictionary=(encryption_and_decryption(chos))
  63. keytext=open("key.txt","r").read()
  64. value=[i for i in keytext]
  65. keys=[i for i in dictionary]
  66. decrypt=(dictionary_swap(value, keys))
  67. ciphertext=open("ciphertext.txt","r")
  68. lst1=ciphertext.readlines()
  69. cipher=[]
  70. for i in range(len(lst1)):
  71. val=lst1[i].replace("\n","")
  72. cipher.append(val)
  73. translation=(decrypt_file(cipher, decrypt))
  74. decrypted_text=open("decrypted.txt","w")
  75. for i in translation:
  76. decrypted_text.write(i)
  77. decrypted_text.write("\n")
  78. decrypted_text.close()
  79.  
  80. def dictionary_swap(lst1, lst2):
  81. new_dic={}
  82. for i in range(len(lst1)):
  83. new_dic[lst1[i]] = lst2[i]
  84. return new_dic
  85. ###################################FUNCTION###################################################
  86. def decrypt_file(decryp, dictionary):
  87. decoding=[]
  88. for val in decryp:
  89. translate=""
  90. for x in range(len(val)):
  91. if val[x]==" ":
  92. translate+=val[x]
  93. else:
  94. code=val[x]
  95. translate+=(dictionary[code])
  96. decoding.append(translate)
  97. return decoding
  98. ###################################FUNCTION###################################################
  99. ###################################main#######################################################
  100. choice=input("please enter the letter 'e' for encrypt, or 'd' for decrypt : " )
  101. if choice == "e":
  102. encrypt=(choose_e(choice))
  103. elif choice=="d":
  104. decrypt=(choose_d(choice))
  105. else:
  106. print("we are done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement