base64.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import base64
  2. import time
  3. #Put the driver name (C:) followed by the directory and you
  4. #can encrypt any file on a PC.
  5. def menu():
  6. time.sleep(0.5)
  7. choice = input("\n1. encrypt File\n2. encrypt message\n3. decrypt message\n4. exit\nchoice: ")
  8. if choice == "1":
  9. encryptFile()
  10. elif choice == "2":
  11. encrypt()
  12. elif choice == "3":
  13. decryptMessage()
  14. elif choice == "4":
  15. exit()
  16. else:
  17. print ("Not a valid choice.")
  18. def encryptFile():
  19. myFile = input("enter file to encrypt: ")
  20. file = open(myFile,"r")
  21. contents = file.read()
  22. contents = contents.encode()
  23. file = open(myFile, "w")
  24. encoded = base64.b64encode(contents)
  25. # the .decode() converts the bytes to str, taking off the b'...'
  26. file.write(str(encoded))
  27. print ("File is now encrypted... and the contents is unreadable")
  28. def decryptMessage():
  29. pwd = "N3VIQUJmZ2pyNDVkZDRvMzNkZmd0NzBkZzlLOWRmcjJ0NWhCdmRm"
  30. key = base64.b64decode(pwd) #the decoded version of this is the key.
  31. value = input("Enter the decryption key: ").encode()
  32. if value == key:
  33. time.sleep(1)
  34. message = input("Enter the message to decode: ")
  35. decoded = base64.b64decode(message)
  36. print (decoded)
  37. menu()
  38. else:
  39. print("Decryption key is wrong.")
  40. menu()
  41. def encrypt():
  42. password = input("Enter a message: ").encode()
  43. encoded = base64.b64encode(password)
  44. print (encoded.decode())
  45. menu()
  46. def hashing(password):
  47. hash1 = hashlib.md5(str.encode(password)).hexdigest()
  48. print ("your hashed password is:", hash1,"\n")
  49. menu()