0

I am trying to change an official example from https://pycryptodome.readthedocs.io/en/latest/src/examples.html

and adapt to a little encryption project I am working on:

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) ]

in my case file from example encrypted_data.bin (file_in = open("encrypted_data.bin", "rb")) will be a string variable rather than file.

How to adapt below loop to use string variable instead file_in ?

enc_session_key, nonce, tag, ciphertext = \
   [ **file_in.read(x)** for x in (private_key.size_in_bytes(), 16, 16, -1) ]
President James K. Polk
  • 40,516
  • 21
  • 95
  • 125

1 Answers1

0

Simply replace the file read commands with strings. For example:

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

encrypted_data = "my_data_str"

private_key = RSA.import_key("my_private_key_str")

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