Advertisement
Guest User

Beta Decay Caesar

a guest
Aug 8th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.76 KB | None | 0 0
  1. # Changes from version 6
  2. #
  3. # - More changes making the program more user friendly
  4.  
  5. # Import random and time module
  6. import random, time
  7.  
  8. # offset() function
  9. def offset(key):
  10.     # Set the offset to 0
  11.     offset = 0
  12.  
  13.     # Iterate through the key
  14.     for i in range(8):
  15.         # Get ASCII code of key[i]
  16.         code = ord(key[i])
  17.  
  18.         # Add ASCII code of key[i] to offset
  19.         offset += code
  20.  
  21.     # Divide offset by eight, round it down and subtract 32
  22.     offset = int(offset/8) - 32
  23.  
  24.     # Return offset
  25.     return offset
  26.  
  27. # encrypt() function
  28. def encrypt(word):
  29.     # Generate key
  30.     key = ""
  31.  
  32.     for i in range(8):
  33.         # Generate random number
  34.         rnum = random.randint(33,126)
  35.  
  36.         # Convert rnum to character
  37.         char = chr(rnum)
  38.  
  39.         # Add to key
  40.         key += char
  41.  
  42.     # Output the key
  43.     print("\nThe eight character key is: ", key)
  44.     print("Write this down for when you decrypt the message")
  45.  
  46.     # Wait for three seconds
  47.     time.sleep(3)
  48.  
  49.     # Call offset()
  50.     oset = offset(key)
  51.  
  52.     # Encrypt message
  53.     encrypted = ""
  54.  
  55.     for i in range(len(word)):
  56.         # Check if word[i] is a space
  57.         if word[i] == " ":
  58.             # Leave space unchanged
  59.             encrypted += " "
  60.  
  61.         else:
  62.             # word[i] is not a space
  63.             # Get ASCII code of word[i]
  64.             letter_code = ord(word[i])
  65.  
  66.             # Add offset to letter_code
  67.             letter_code += oset
  68.             # Is letter_code greater than 126?
  69.             if letter_code > 126:
  70.                 # Take 94 off letter_code
  71.                 letter_code -= 94
  72.             else:
  73.                 # If not, do nothing
  74.                 pass
  75.                
  76.             # Convert letter_code to a character
  77.             letter = chr(letter_code)
  78.  
  79.             # Add letter to encrypted
  80.             encrypted += letter
  81.  
  82.     # Return encrypted message and the key
  83.     return encrypted, key
  84.  
  85. def decrypt(file_name, key):
  86.     # Open and read file
  87.     file = open(file_name, "r")
  88.     word = file.read()
  89.     file.close()
  90.  
  91.     # Call offset()
  92.     oset = offset(key)
  93.  
  94.     # Decrypt message
  95.     decrypted = ""
  96.  
  97.     for i in range(len(word)):
  98.         # Check if word[i] is a space
  99.         if word[i] == " ":
  100.             # Leave space unchanged
  101.             decrypted += " "
  102.  
  103.         else:
  104.             # word[i] is not a space
  105.             # Get ASCII code of word[i]
  106.             letter_code = ord(word[i])
  107.  
  108.             # Subtract offset from letter_code
  109.             letter_code -= oset
  110.  
  111.             # Is letter_code less than 3?
  112.             if letter_code < 33:
  113.                 # Add 94 to letter_code
  114.                 letter_code += 94
  115.  
  116.             # Convert letter_code to a character
  117.             letter = chr(letter_code)
  118.  
  119.             # Add letter to decrypted
  120.             decrypted += letter
  121.  
  122.     # Return decrypted message
  123.     return decrypted
  124.  
  125. # Start
  126.  
  127. # Get user choice
  128. choice = input("Do you wish to 'encrypt' or 'decrypt' the message or 'quit' the program? ").lower()
  129.  
  130. if choice[0] == "e":
  131.     # User chose to encrypt the message
  132.     # Get the filename of the message to be encrypted
  133.     file_name = input("\nEnter the name of the file containing the message to be encrypted. ")
  134.  
  135.     # Check if user has added .txt or not
  136.     if ".txt" != file_name[-4:]:
  137.         # The last four characters in file_name != ".txt"
  138.         # Add ".txt" to file_name
  139.         file_name += ".txt"
  140.        
  141.     # Open and read file
  142.     file = open(file_name, "r")
  143.     word = file.read()
  144.     file.close()
  145.  
  146.     file_length = len(word)
  147.  
  148.     if file_length > 500:
  149.         print("File is too long (greater than 500 characters)")
  150.         print("\nPlease reduce the length of the file or split the file into")
  151.         print("multiple files before trying again.")
  152.  
  153.         input("\nPress enter to exit")
  154.         exit()
  155.        
  156.     # Check if user wishes to use extra encryption
  157.     extra = input("Do you wish to use 'extra encryption'? (y/n) ").lower()
  158.  
  159.     if extra == "y":
  160.         # Loop through word, stripping the spaces
  161.         nspaces = ''
  162.        
  163.         for i in range(len(word)):
  164.             # Check if word[i] is not a space
  165.             if word[i] != " ":
  166.                 # Add word[i] to nspaces
  167.                 nspaces += word[i]
  168.  
  169.         # Empty word
  170.         word = ''
  171.  
  172.         # Loop through nspaces, splitting it up into five letter words
  173.  
  174.         for i in range(len(nspaces)):
  175.             # Add nspaces[i] to word
  176.             word += nspaces[i]
  177.  
  178.             # Is i a multiple of 5?
  179.             if (i+1)%5 == 0:
  180.                 # Add a space
  181.                 word += " "
  182.                
  183.         # Call the encrypt() function
  184.         encrypted, key = encrypt(word)
  185.     else:
  186.         # Call the encrypt() function
  187.         encrypted, key = encrypt(word)
  188.  
  189.     # Print the encrypted word
  190.     print("\nEncrypted message")
  191.     print("=================")
  192.     print(encrypted)
  193.  
  194.     # Get the name of the file to which the ciphertext should be saved
  195.     file_name = input("\nEnter the name of the file where you wish to save the encrypted word. ")
  196.  
  197.     # Check if user has added .txt or not
  198.     if ".txt" != file_name[-4:]:
  199.         # The last four characters in file_name != ".txt"
  200.         # Add ".txt" to file_name
  201.         file_name += ".txt"
  202.  
  203.     # Open file for writing to
  204.     file = open(file_name, "w")
  205.  
  206.     # Write the ciphertext to the file
  207.     file.write(encrypted)
  208.  
  209.     file.close()
  210.  
  211.     # Check if file has saved correctly
  212.     # Run decrypt function on contents of file
  213.     savecheck = decrypt(file_name, key)
  214.    
  215.     # Check if savecheck is equal to word
  216.     if savecheck == word:
  217.         # Encryption works, save successful
  218.         print("File has saved successfully")
  219.     else:
  220.         # Either encryption does not work or write to file was unsuccessful
  221.         print("File save was unsuccessful. Please run the program again.")
  222.    
  223. elif choice[0] == "d":
  224.     # User chose to decrypt the message
  225.     # Get the filename of the message to be decrypted
  226.     file_name = input("\nEnter the name of the file containing the message to be decrypted. ")
  227.  
  228.     # Check if user has added .txt or not
  229.     if ".txt" != file_name[-4:]:
  230.         # The last four characters in file_name != ".txt"
  231.         # Add ".txt" to file_name
  232.         file_name += ".txt"
  233.    
  234.     # Get key from user
  235.     key = input("\nEnter the 8 character key used to encrypt the message. ")
  236.     # Call the decrypt() function
  237.     decrypted = decrypt(file_name, key)
  238.  
  239.     # Output the decrypted message
  240.     print("\nDecrypted message")
  241.     print("=================")
  242.     print(decrypted)
  243.  
  244. # Otherwise, user chose to exit
  245. # Get user to press enter before exiting
  246. input("\n\nPress enter to exit ")
  247.  
  248. # End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement