0

I am reading the docs for pycryptodome and got confused at over how the is the data encrypted and transmitted to the receiver and then decrypted over there

What I understood is

over at the sender side, the public key of the sender is used to encrypt a piece of data which is then write to a .bin file

from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES, PKCS1_OAEP

data = "I met aliens in UFO. Here is the map.".encode("utf-8")
file_out = open("encrypted_data.bin", "wb")

recipient_key = RSA.import_key(open("receiver.pem").read())
session_key = get_random_bytes(16)

# Encrypt the session key with the public RSA key
cipher_rsa = PKCS1_OAEP.new(recipient_key)
enc_session_key = cipher_rsa.encrypt(session_key)

# Encrypt the data with the AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX)
ciphertext, tag = cipher_aes.encrypt_and_digest(data)
[ file_out.write(x) for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ]
file_out.close()

over at the receiver (the destination supposed to receive the data) the .bin file is loaded and decrypted using the private key of the receiver

from Crypto.PublicKey import RSA
from Crypto.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 private 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_aes.decrypt_and_verify(ciphertext, tag)
print(data.decode("utf-8"))

Now as I understand each one of the sender and the receiver have two .pem files one is public key and the other is private

what I don't understand is: Why do we have a session_key and how is the data decrypted with the private key of the receiver?

E_net4
  • 27,810
  • 13
  • 101
  • 139
manuel
  • 23
  • 6
  • 1
    Look at hybrid encryption, e.g. [Hybrid cryptosystem](https://en.wikipedia.org/wiki/Hybrid_cryptosystem), then you will probably be able to answer most of your questions yourself. – Topaco Feb 24 '22 at 18:47
  • 2
    *...over at the sender side, the public key of the sender is used to encrypt a piece of data...* No, the public key of the **recipient** is used, not the sender. Which is what your code actually does. – President James K. Polk Feb 24 '22 at 20:21
  • I got to say I do am learning how to use Pycryptodome and I must say the documentation is not as helpful as it should be. For example one would intuit that the RSA class would also handle the encryption and decryption. However, there's no mention of it in the documentation for the RSA class not even pointing readers to what ever PKCS1_OAEP is supposed to be (By the way what a terrible y name PKCS1_OAEP is). – Andrew S Sep 09 '22 at 04:21

0 Answers0