easyRSAlatest.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import rsa
  2. import time
  3. import os.path
  4. def LICENSE():
  5. print('''
  6. easyRSA - It just encrypt and decrypt.
  7. Copyright (C) 2021 stak
  8. This program is free software: you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation, either version 3 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. ''')
  19. def console_picture():
  20. print(' ____ ____ _ ')
  21. time.sleep(0.1)
  22. print(' ___ __ _ ___ _ _| _ \/ ___| / \ ')
  23. time.sleep(0.1)
  24. print(' / _ \/ _` / __| | | | |_) \___ \ / _ \ ')
  25. time.sleep(0.1)
  26. print('| __/ (_| \__ \ |_| | _ < ___) / ___ \ ')
  27. time.sleep(0.1)
  28. print(' \___|\__,_|___/\__, |_| \_\____/_/ \_\ ')
  29. time.sleep(0.1)
  30. print(' |___/ ')
  31. def gen_rsa(bit=2048):
  32. global pubkey, privkey
  33. if __name__ == '__main__':
  34. bit = input('Enter number of bits for generate RSA keys without spaces(default 2048): ')
  35. if bit == '':
  36. bit = 2048
  37. bit = int(bit)
  38. (pubkey, privkey) = rsa.newkeys(bit)
  39. pubkey_pem = pubkey.save_pkcs1()
  40. privkey_pem = privkey.save_pkcs1()
  41. if os.path.isfile('private.pem') == False:
  42. file_out = open("privkey.pem", "wb")
  43. file_out.write(privkey_pem)
  44. file_out.close()
  45. if os.path.isfile('public.pem') == False:
  46. file_out = open("public.pem", "wb")
  47. file_out.write(pubkey_pem)
  48. file_out.close()
  49. else:
  50. print('File(s) already exists!')
  51. def encryption(word):
  52. global encrypted_message
  53. word = word.encode('utf-8')
  54. encrypted_message = rsa.encrypt(word, pubkey)
  55. return encrypted_message
  56. def decryption(encrypted_message):
  57. unencrypted_message = rsa.decrypt(encrypted_message, privkey)
  58. global decode_message
  59. decode_message = unencrypted_message.decode('utf-8')
  60. return decode_message
  61. def gen_rsa_q():
  62. userinput = input('Generate RSA keys?[y,n]: ')
  63. if userinput == 'y':
  64. gen_rsa()
  65. else:
  66. print('RSA keys have not been generated!')
  67. def control():
  68. exit_words = ['exit', 'stop', 'break', '/exit', '/stop', '/break','-exit', '-stop', '-break`']
  69. while True:
  70. instruction = input('Enter instruction: ')
  71. if instruction == '-start':
  72. gen_rsa()
  73. message = input('Enter word: ')
  74. encryption(message)
  75. print(f'encrypted message = {encrypted_message}')
  76. if instruction == '-e':
  77. try:
  78. word = input('Enter word: ')
  79. encryption(word)
  80. print(f'encrypted message = {encrypted_message}')
  81. except NameError:
  82. print("You didn't generate RSA keys")
  83. gen_rsa_q()
  84. else:
  85. continue
  86. if instruction == '-d':
  87. try:
  88. decryption(encrypted_message=encrypted_message)
  89. print(f'decode_message = {decode_message}')
  90. except NameError:
  91. print("You didn't encrypt any words, enter -e to encrypt")
  92. else:
  93. continue
  94. if instruction == '-g':
  95. gen_rsa()
  96. if instruction in exit_words:
  97. console_picture()
  98. print('Bye!')
  99. break
  100. def main():
  101. LICENSE()
  102. console_picture()
  103. control()
  104. if __name__ == "__main__":
  105. main()