Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Changes from version 6
- #
- # - More changes making the program more user friendly
- # Import random and time module
- import random, time
- # offset() function
- def offset(key):
- # Set the offset to 0
- offset = 0
- # Iterate through the key
- for i in range(8):
- # Get ASCII code of key[i]
- code = ord(key[i])
- # Add ASCII code of key[i] to offset
- offset += code
- # Divide offset by eight, round it down and subtract 32
- offset = int(offset/8) - 32
- # Return offset
- return offset
- # encrypt() function
- def encrypt(word):
- # Generate key
- key = ""
- for i in range(8):
- # Generate random number
- rnum = random.randint(33,126)
- # Convert rnum to character
- char = chr(rnum)
- # Add to key
- key += char
- # Output the key
- print("\nThe eight character key is: ", key)
- print("Write this down for when you decrypt the message")
- # Wait for three seconds
- time.sleep(3)
- # Call offset()
- oset = offset(key)
- # Encrypt message
- encrypted = ""
- for i in range(len(word)):
- # Check if word[i] is a space
- if word[i] == " ":
- # Leave space unchanged
- encrypted += " "
- else:
- # word[i] is not a space
- # Get ASCII code of word[i]
- letter_code = ord(word[i])
- # Add offset to letter_code
- letter_code += oset
- # Is letter_code greater than 126?
- if letter_code > 126:
- # Take 94 off letter_code
- letter_code -= 94
- else:
- # If not, do nothing
- pass
- # Convert letter_code to a character
- letter = chr(letter_code)
- # Add letter to encrypted
- encrypted += letter
- # Return encrypted message and the key
- return encrypted, key
- def decrypt(file_name, key):
- # Open and read file
- file = open(file_name, "r")
- word = file.read()
- file.close()
- # Call offset()
- oset = offset(key)
- # Decrypt message
- decrypted = ""
- for i in range(len(word)):
- # Check if word[i] is a space
- if word[i] == " ":
- # Leave space unchanged
- decrypted += " "
- else:
- # word[i] is not a space
- # Get ASCII code of word[i]
- letter_code = ord(word[i])
- # Subtract offset from letter_code
- letter_code -= oset
- # Is letter_code less than 3?
- if letter_code < 33:
- # Add 94 to letter_code
- letter_code += 94
- # Convert letter_code to a character
- letter = chr(letter_code)
- # Add letter to decrypted
- decrypted += letter
- # Return decrypted message
- return decrypted
- # Start
- # Get user choice
- choice = input("Do you wish to 'encrypt' or 'decrypt' the message or 'quit' the program? ").lower()
- if choice[0] == "e":
- # User chose to encrypt the message
- # Get the filename of the message to be encrypted
- file_name = input("\nEnter the name of the file containing the message to be encrypted. ")
- # Check if user has added .txt or not
- if ".txt" != file_name[-4:]:
- # The last four characters in file_name != ".txt"
- # Add ".txt" to file_name
- file_name += ".txt"
- # Open and read file
- file = open(file_name, "r")
- word = file.read()
- file.close()
- file_length = len(word)
- if file_length > 500:
- print("File is too long (greater than 500 characters)")
- print("\nPlease reduce the length of the file or split the file into")
- print("multiple files before trying again.")
- input("\nPress enter to exit")
- exit()
- # Check if user wishes to use extra encryption
- extra = input("Do you wish to use 'extra encryption'? (y/n) ").lower()
- if extra == "y":
- # Loop through word, stripping the spaces
- nspaces = ''
- for i in range(len(word)):
- # Check if word[i] is not a space
- if word[i] != " ":
- # Add word[i] to nspaces
- nspaces += word[i]
- # Empty word
- word = ''
- # Loop through nspaces, splitting it up into five letter words
- for i in range(len(nspaces)):
- # Add nspaces[i] to word
- word += nspaces[i]
- # Is i a multiple of 5?
- if (i+1)%5 == 0:
- # Add a space
- word += " "
- # Call the encrypt() function
- encrypted, key = encrypt(word)
- else:
- # Call the encrypt() function
- encrypted, key = encrypt(word)
- # Print the encrypted word
- print("\nEncrypted message")
- print("=================")
- print(encrypted)
- # Get the name of the file to which the ciphertext should be saved
- file_name = input("\nEnter the name of the file where you wish to save the encrypted word. ")
- # Check if user has added .txt or not
- if ".txt" != file_name[-4:]:
- # The last four characters in file_name != ".txt"
- # Add ".txt" to file_name
- file_name += ".txt"
- # Open file for writing to
- file = open(file_name, "w")
- # Write the ciphertext to the file
- file.write(encrypted)
- file.close()
- # Check if file has saved correctly
- # Run decrypt function on contents of file
- savecheck = decrypt(file_name, key)
- # Check if savecheck is equal to word
- if savecheck == word:
- # Encryption works, save successful
- print("File has saved successfully")
- else:
- # Either encryption does not work or write to file was unsuccessful
- print("File save was unsuccessful. Please run the program again.")
- elif choice[0] == "d":
- # User chose to decrypt the message
- # Get the filename of the message to be decrypted
- file_name = input("\nEnter the name of the file containing the message to be decrypted. ")
- # Check if user has added .txt or not
- if ".txt" != file_name[-4:]:
- # The last four characters in file_name != ".txt"
- # Add ".txt" to file_name
- file_name += ".txt"
- # Get key from user
- key = input("\nEnter the 8 character key used to encrypt the message. ")
- # Call the decrypt() function
- decrypted = decrypt(file_name, key)
- # Output the decrypted message
- print("\nDecrypted message")
- print("=================")
- print(decrypted)
- # Otherwise, user chose to exit
- # Get user to press enter before exiting
- input("\n\nPress enter to exit ")
- # End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement