RSA.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import sys
  2. import os
  3. from os import system,name
  4. import time
  5. if name == 'nt':
  6. from Cryptodome.PublicKey import RSA
  7. from Cryptodome.Random import get_random_bytes
  8. from Cryptodome.Cipher import AES, PKCS1_OAEP
  9. else:
  10. from Crypto.PublicKey import RSA
  11. from Crypto.Random import get_random_bytes
  12. from Crypto.Cipher import AES, PKCS1_OAEP
  13. #Just for clear terminal
  14. def clear():
  15. system('cls') if name == 'nt' else system('clear')
  16. def main():
  17. clear()
  18. print("1. Generate keys")
  19. print("2. Encrypt File")
  20. print("3. Decrypt File")
  21. print("4. Exit")
  22. main_input = input(">>> ")
  23. if main_input == "1":
  24. enc_key = RSA.generate(4096) #Generating RSA key with len = 4096.
  25. privkeyname = input("Enter your private key name: ") #Entering name for your private key
  26. pubkeyname = input("Enter your public key name: ") #Entering name for your public key
  27. private_key = enc_key.export_key() #Exporting encrypted prime numbers from key
  28. prkey_out = open("Keys/"+ privkeyname + ".pem", "wb")
  29. prkey_out.write(private_key) #Writing private key to file
  30. prkey_out.close() #Closing your private key so you can instantly use him, without closing a program
  31. public_key = enc_key.publickey().export_key() #Exporting encrypted big number from private key based on first generated key
  32. pbkey_out = open("Keys/" + pubkeyname + ".pem", "wb")
  33. pbkey_out.write(public_key) #Writing public key to file
  34. pbkey_out.close() #Closing your public key so you can instantly use him, without closing a program
  35. print("Done! You can find your keys in 'Keys' folder")
  36. print("Press [ENTER] to return to main menu")
  37. asdasddsad = input("")
  38. main()
  39. elif main_input == "2":
  40. file_ = input("Input your file to encrypt: ") #Entering your file_ to encrypt
  41. pubkey = input("Input your public key: ") #Entering your pubkey to encrypt file_ with him
  42. f = open(file_, "rb") #Opening your file_
  43. data = f.read(); f.close() #Reading data from your file_, closing it
  44. file_out = open(str(file_)+".bin", "wb") #Writing output file
  45. session_key = get_random_bytes(16) #Getting random bytes
  46. recipient_key = RSA.import_key(open(pubkey).read()) #Creating recipient key from pubkey
  47. cipher_rsa = PKCS1_OAEP.new(recipient_key) #creating cipher rsa from recipient key
  48. enc_session_key = cipher_rsa.encrypt(session_key) #Creating enc session key from random bytes
  49. cipher_aes = AES.new(session_key, AES.MODE_EAX) #Strengthen it all with AES encryption
  50. ciphertext, tag = cipher_aes.encrypt_and_digest(data) #Finally, encrypting the data from file_
  51. [ file_out.write(x) for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ] #Writing all encrypted data to the new, encrypted file_
  52. f.close()
  53. file_out.close()
  54. print("File encrypted succesfully!")
  55. print("Press [ENTER] to return to main menu")
  56. asdasddsad = input("") #Just for hold the terminal, nevermind
  57. main()
  58. elif main_input == "3":
  59. privkey = input("Input your private key: ") #Entering your private key to decrypt
  60. enc_file_ = input("Input your encrypted file: ") #Entering your encrypted file_ to decrypt
  61. file_in = open(enc_file_, "rb")
  62. file_out = open(str(enc_file_[:-4]), "wb") #Opening your encrypted file_
  63. private_key = RSA.import_key(open(privkey).read()) #Reading your private key
  64. enc_session_key, nonce, tag, ciphertext = \
  65. [ file_in.read(x) for x in (private_key.size_in_bytes(), 16, 16, -1) ] #Creating enc session key from private key
  66. cipher_rsa = PKCS1_OAEP.new(private_key)
  67. session_key = cipher_rsa.decrypt(enc_session_key) #Decrypting your data from encrypted file_
  68. cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
  69. data = cipher_aes.decrypt_and_verify(ciphertext, tag) #Verifying that is all right
  70. file_out.write(data) #Writing decrypted data to your file_
  71. file_in.close()
  72. file_out.close()
  73. print("File decrypted succesfully!")
  74. os.remove(enc_file_) #Removing encrypted file_ from your system, you don't need it anymore
  75. print("Press [ENTER] to return to main menu")
  76. asdasdasd = input("")
  77. main()
  78. elif main_input == "4":
  79. exit(0)
  80. else:
  81. print("Error input, repeating..")
  82. time.sleep(1)
  83. main()
  84. if __name__ == "__main__":
  85. try:
  86. main()
  87. except KeyboardInterrupt:
  88. clear()
  89. print("Bye")
  90. exit(0)