Advertisement
Guest User

test

a guest
Mar 5th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1.  
  2.  
  3. # Text Encryption
  4. # Using external files to encrypt and decrypt messages
  5.  
  6. # Procedures and functions
  7. # ===========================================
  8.  
  9. def displayMenu():
  10. # procedure to display the text encryption menu
  11. print(
  12. """
  13. Text Encryption
  14.  
  15. 0 - Exit the program
  16. 1 - Encrypt message
  17. 2 - Decrypt message
  18. """)
  19.  
  20.  
  21. def encryption():
  22. # Procedure that contains the Encryption code
  23. print("You have chosen encryption.")
  24. time.sleep(1)
  25. readfile()
  26. key()
  27. time.sleep(2)
  28. offset()
  29. time.sleep(2)
  30. convert()
  31. time.sleep(3)
  32.  
  33. print("==================================")
  34.  
  35. def decryption():
  36. # Procedure that contains the Decryption code
  37. print("You have chosen decryption.")
  38. time.sleep(2)
  39. print("Decrypt")
  40.  
  41.  
  42. def readfile():
  43. # Function that contains the code to read in a file.
  44. print("Make sure the file is in the same folder as the program.")
  45. # Ask the user to enter a filename
  46. fname = input("Enter the file name: ")
  47. # Check if the filename entered has .txt at the end. If not, add it on.
  48. if ".txt" not in fname:
  49. fname += ".txt"
  50. # Check if the file exists(returns a boolean)
  51. validfile = os.path.isfile(fname)
  52. # While loop that loops every time the user enters an invalid filename.
  53. while validfile == False:
  54. print("The file was not found. Please try again.")
  55. print("Make sure the text file is in the same folder as the program.")
  56. # Asks the user to enter a filename
  57. fname = input("Enter the file name: ")
  58. # Checks if the filename entered has a .txt at the end. If not, add it on.
  59. if ".txt" not in fname:
  60. fname += ".txt"
  61. # Check if the file exists(return a boolean)
  62. validfile = os.path.isfile(fname)
  63. # Open the file
  64. read_file = open(fname, "r")
  65. # Store the text in the file
  66. message = read_file.read()
  67. # Close the file
  68. read_file.close()
  69. # Inform the user that the text has been successfully saved.
  70. print("Text file has been saved.")
  71. print("\nThis is the message that is going to be encrypted:")
  72. time.sleep(2)
  73. print(message)
  74. time.sleep(1)
  75. global message
  76.  
  77.  
  78.  
  79. def key():
  80. # Function that generates the 8 character ASCII key
  81. # Creates a list to store the key in a list
  82. keys = []
  83. # Alert the user that the program is generating their key.
  84. print("\nGenerating numbers..." * 3)
  85. time.sleep(2)
  86. # For loop that loops 8 times to produce a different ASCII code every time.
  87. for i in range(8):
  88. # Generate a random number between 33 and 127(including 33, excluding 127)
  89. key = random.randrange(33, 127)
  90. # Turn the integer into ASCII form.
  91. key = chr(key)
  92. # Append the ASCII to the list created earlier
  93. keys.append(key)
  94. # Alert the user that this is important and they need to store it.
  95. print("\nALERT! This is the key to decrypt your message for later. Please save it somewhere. (CASE SENSITIVE)")
  96. time.sleep(2)
  97. # Print the 8 letter key to the user.
  98. print("".join(keys))
  99. input("Press enter once you have written it down: ")
  100. global keys
  101.  
  102.  
  103. def offset():
  104. # Function that generates the offset factor
  105. time.sleep(1)
  106. print("\nGenerating offset factor...")
  107. # Create a variable that will be used later.
  108. number = 0
  109. # Create a for loop that takes out each character, turns it into an ASCII code, and adds it to the variable.
  110. for character in keys:
  111. ascii = ord(character)
  112. number = number + ascii
  113. # Divide the total number by 8 to find the average
  114. number = number / 8
  115. # Round the total number down to a whole number
  116. number = int(number)
  117. # Minus 32 from the total number
  118. number = number - 32
  119. global number
  120. print("This is the offset factor:", number)
  121.  
  122.  
  123. def convert():
  124. # Function that generates the offset factor
  125. # Create a variable that will be used later.
  126. finalmessage = ""
  127. # Use a for loop to take out each character from message
  128. for character in message:
  129. # Use an if statement to check if the character is a space.
  130. if character == " ":
  131. # If the character is a space, add it to the variable created before.
  132. finalmessage = finalmessage + character
  133. # Use else if the character is not a space.
  134. else:
  135. # Create a new variable with the ASCII code of each character.
  136. ASCIIcode = ord(character)
  137. # Add the offset factor to the ASCII code.
  138. ASCIIcode = ASCIIcode + number
  139. # Check if the total number is over 126.
  140. if ASCIIcode > 126:
  141. # If the total number is over 126, then minus 94 from it.
  142. ASCIIcode = ASCIIcode - 94
  143. # Turn the ASCII code into an actual character.
  144. ASCIIcharacter = chr(ASCIIcode)
  145. # Add the character to the final encrypted message.
  146. finalmessage = finalmessage + ASCIIcharacter
  147. time.sleep(2)
  148. print("This is your encrypted message:")
  149. time.sleep(1)
  150. # Print the final encrypted message
  151. print(finalmessage)
  152.  
  153.  
  154.  
  155.  
  156. # Data structures and variables
  157. # ===================================================
  158. choice = None
  159. import os.path
  160. import random
  161. import time
  162.  
  163. # Main program and menu
  164. # =======================================
  165.  
  166. # While statement that runs most of the code in the program.
  167. while choice != "0":
  168. displayMenu()
  169. choice = input("Choice: ")
  170. print()
  171. # This if statement allows the user to quit if they enter 0.
  172. if choice == "0":
  173. print("Goodbye.")
  174. # This elif statement calls the encryption procedure
  175. elif choice == "1":
  176. encryption()
  177. # This elif statement calls the decryption procedure.
  178. elif choice == "2":
  179. decryption()
  180. # Error catching else statement
  181. else:
  182. print("\nSorry, but", choice, "isn't a valid choice.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement