如何在读取混合加密的文本文件时添加 b " "



目前,我可以使用与消息(如第10行(相同文件中的变量进行加密。然而,我需要关于如何添加"b"的帮助"在第21行,因为我正在从一个文件中读取。

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

非常感谢你能在这里帮我,谢谢。

通过使用.encode((

其等效于使用b〃"它只适用于字符串,因此可以在列表中执行你必须做

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

或者如果它是一个文件,在你的情况下是真的,你可以使用读取字节参数(rb(

u = open("test.txt", "rb").readlines()

最新更新