0

Currently I can encrypt using a variable in the same file as a message such as line 10. However I need help on how to add the b"" at line 21 since i am reading in from a file.

from base64 import b64encode
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from Crypto.Random import get_random_bytes
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import glob

for item in glob.glob("*.txt"):
  #context = b"Secret Message"
  k = get_random_bytes(16)

  pk = RSA.import_key(open("publicKey.pem").read())
  file_out = open("CK.bin", "wb")
  cipher_rsa = PKCS1_OAEP.new(pk)
  CK = cipher_rsa.encrypt(k)
  file_out.write(CK)
  file_out.close()

  fileTxtIn = open(item, 'r')
  context = fileTxtIn.readlines()
  cipher = AES.new(k, AES.MODE_CBC)
  CM_bytes = cipher.encrypt(pad(context, AES.block_size))
  iv = cipher.iv
  iv_out = open("iv_file", "wb")
  iv_out.write(iv)
  CM_out = open("CM_file.enc", "wb")
  CM_out.write(CM_bytes)
  fileTxtIn.close()

Much appreicated if you can help me here, thanks.

1 Answers1

0

by using .encode()

its the equivalent to using the b"" it works only on strings so to be able to do it on a list you will have to do

new_context = []
for i in context:
    new_context.append(i.encode())

or if its a file which is true in your case you can use the read bytes parameter (rb)

u = open("test.txt", "rb").readlines()
Bredget
  • 11
  • 4