test_rsa.py 1.1 KB

123456789101112131415161718192021222324252627282930
  1. import rsa as pyrsa
  2. from Crypto.PublicKey import RSA
  3. from libs import gmpy2_pkcs10aep_cipher
  4. def test_gmpy2_pkcs10aep_cipher() -> None:
  5. rsa_key_pair = pyrsa.newkeys(2048)
  6. public_key = rsa_key_pair[0].save_pkcs1()
  7. private_key = rsa_key_pair[1].save_pkcs1()
  8. public_rsa_key = RSA.import_key(public_key)
  9. public_cipher_rsa2 = gmpy2_pkcs10aep_cipher.new(public_rsa_key)
  10. private_rsa_key = RSA.import_key(private_key)
  11. private_cipher_rsa = gmpy2_pkcs10aep_cipher.new(private_rsa_key)
  12. raw_text = 'raw_text'
  13. raw_text_bytes = raw_text.encode()
  14. # RSA encryption by public key and decryption by private key
  15. encrypted_by_pub_key = public_cipher_rsa2.encrypt(message=raw_text_bytes)
  16. decrypted_by_pub_key = private_cipher_rsa.decrypt(encrypted_by_pub_key)
  17. assert decrypted_by_pub_key == raw_text_bytes
  18. # RSA encryption and decryption by private key
  19. encrypted_by_private_key = private_cipher_rsa.encrypt(message=raw_text_bytes)
  20. decrypted_by_private_key = private_cipher_rsa.decrypt(encrypted_by_private_key)
  21. assert decrypted_by_private_key == raw_text_bytes