1

I'm using pycryptodomex for RSA encryption, decryption. Im following this article

from Cryptodome.PublicKey import RSA
from Cryptodome.Cipher import AES, PKCS1_OAEP

file_in = open("encrypted_data.bin", "rb")

private_key = RSA.import_key(open("private.pem").read())

enc_session_key, nonce, tag, ciphertext = \
   [ file_in.read(x) for x in (private_key.size_in_bytes(), 16, 16, -1) ]

# Decrypt the session key with the public RSA key
cipher_rsa = PKCS1_OAEP.new(private_key)
session_key = cipher_rsa.decrypt(enc_session_key)

# Decrypt the data with the AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
data = cipher.decrypt_and_verify(ciphertext, tag)

It is showing following error

ValueError Traceback (most recent call last) in 11 # Decrypt the session key with the public RSA key 12 cipher_rsa = PKCS1_OAEP.new(private_key) ---> 13 session_key = cipher_rsa.decrypt(enc_session_key) 14 15 # Decrypt the data with the AES session key

C:\ProgramData\Anaconda3\lib\site-packages\Cryptodome\Cipher\PKCS1_OAEP.py in decrypt(self, ciphertext) 165 # Step 1b and 1c 166 if len(ciphertext) != k or k 167 raise ValueError("Ciphertext with incorrect length.") 168 # Step 2a (O2SIP) 169 ct_int = bytes_to_long(ciphertext)

ValueError: Ciphertext with incorrect length.

Is there any problem in Code ?

0 Answers0