Guest User

Untitled

a guest
Feb 1st, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. import random
  2. import time
  3. import os
  4. import sys
  5.  
  6.  
  7. invalid_input = True
  8. filename = None
  9. key = None
  10.  
  11. def menu():
  12. print("Welcome to the Encryption / Decryption program!\n-------------------------------------------")
  13. print("Please select a number (1-3) corresponding to the instruction you would like")
  14. menuInput = input("[1] Encrypt a message\n[2] Decrypt a message\n[3] Exit the program\n-------------------------------------------\n")
  15. if menuInput == '1':
  16. encrypt()
  17. elif menuInput == '2':
  18. decrypt()
  19. elif menuInput == '3':
  20. end = input("Are you sure you want to exit the program?\n[1] Yes\n[2] No\n")
  21. if end == '1':
  22. exit()
  23. elif end == '2':
  24. menu()
  25. else:
  26. print("Invalid selection. Returning to main menu.")
  27. time.sleep(2)
  28. menu()
  29. else:
  30. print("Invalid Selection")
  31. menu()
  32.  
  33. def encrypt():
  34. global filename
  35. global key
  36. print("You have chosen to Encrypt a message!")
  37. invalid_input = False
  38. encrypt = input("Please type in the name of the file containing the non-encrypted message\n")
  39. encrypt = encrypt.upper()
  40. message = open(encrypt+".txt","r")
  41. message = message.read()
  42.  
  43. if encrypt == "SAMPLE":
  44. key = ''
  45. for i in range(0,8):
  46. random_number = (random.randint(33,126))
  47. key+=str(chr(random_number))
  48. print(key)
  49. print("You will need this key to decrypt your file!")
  50. time.sleep(1.5)
  51. print("Encrypting your file now...")
  52. time.sleep(2.5)
  53.  
  54. calculate = []
  55.  
  56. for x in key:
  57. x = ord(x)
  58. calculate.append(x)
  59. numbers = sum(calculate)
  60. divided = numbers / 8
  61. rounded = round(divided-0.5)
  62. finalNumber = rounded - 32
  63.  
  64. time.sleep(1)
  65. encrypted = ''
  66. spaces = ''
  67.  
  68. for letter in message:
  69. valLetter = ord(letter)
  70. if valLetter == 32:
  71. encrypted += chr(valLetter)
  72. else:
  73. added = finalNumber + valLetter
  74.  
  75. if added > 126:
  76. subtracted = added - 94
  77. encrypted += chr(subtracted)
  78. else:
  79. encrypted += chr(valLetter)
  80. print("Here is your encrypted message")
  81. time.sleep(1)
  82. print(encrypted)
  83.  
  84.  
  85. filename = input("Please enter the name you wish the file to be called: ")
  86.  
  87. cipher_filename = filename + ".txt"
  88. f = open(cipher_filename,"w")
  89.  
  90. f.write(encrypted)
  91.  
  92. print("Thank you, you will be redirected to the main menu shortly where you can decrypt your message!")
  93. time.sleep(1)
  94.  
  95. else:
  96. print("Invalid Filename!")
  97.  
  98. def decrypt():
  99. print("You have chosen to decrypt a message!")
  100. invalid_input = False
  101. newFile = input("What is the name of your file you stored your encrypted message in?\n")
  102.  
  103. decrypt = open(newFile+".txt","r")
  104. msg = ''
  105.  
  106. for words in decrypt:
  107. msg += str(words)
  108.  
  109. if newFile == filename:
  110. print("Correct!")
  111. keyAuth = input("Please enter the key we provided you with earlier!")
  112.  
  113. if keyAuth == key:
  114. print("Success! Decrypting your message now.")
  115.  
  116. calculate = []
  117.  
  118. for x in key:
  119. x = ord(x)
  120. calculate.append(x)
  121. numbers = sum(calculate)
  122. divided = numbers / 8
  123. rounded = round(divided-0.5)
  124. offset = rounded - 32
  125.  
  126. time.sleep(1)
  127. decrypted = ''
  128. spaces = ''
  129.  
  130. for letter in msg:
  131. valLetter = ord(letter)
  132. if valLetter == 32:
  133. decrypted += chr(valLetter)
  134. else:
  135. subtracted = valLetter - offset
  136.  
  137. if subtracted < 33:
  138. newVal = subtracted + 94
  139. decrypted += chr(newVal)
  140. else:
  141. decrypted += chr(subtracted)
  142. print("Here is your decrypted message")
  143. print(decrypted)
  144.  
  145. else:
  146. print("Invalid Key.")
  147.  
  148. else:
  149. print("Invalid Input, try again!")
  150.  
  151. while invalid_input:
  152. menu()
Advertisement
Add Comment
Please, Sign In to add comment