Python 3.x 类型错误:can';t concat字节到str.Pycrypto Aes加密

Python 3.x 类型错误:can';t concat字节到str.Pycrypto Aes加密,python-3.x,lambda,aes,concat,pycrypto,Python 3.x,Lambda,Aes,Concat,Pycrypto,尝试使用pycryptodome 3.4.2使用python 3使用aes加密对文本进行加密/解密 当然,我在互联网上找到了这种方法,并试图根据我的需要对其进行更改,但我得到的只是错误 代码如下: def aes(): #aes print('1.Шифруем') print('2.Дешифруем') c = input('Ваш выбор:') if int(c) == 1: #shifr os.system('c

尝试使用pycryptodome 3.4.2使用python 3使用aes加密对文本进行加密/解密

当然,我在互联网上找到了这种方法,并试图根据我的需要对其进行更改,但我得到的只是错误

代码如下:

def aes():
    #aes
    print('1.Шифруем')
    print('2.Дешифруем')
    c = input('Ваш выбор:')
    if int(c) == 1:
        #shifr
        os.system('clear')
        print('Шифруем значит')
        print('Введите текст, который хотите зашифровать')
        text = input()
        with open('Aes/plaintext.txt', 'wb') as f:
            f.write(text.encode('utf-8'))
        BLOCK_SIZE = 16
        PADDING = '{'
        pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
        EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
        secret = os.urandom(BLOCK_SIZE)
        with open('Aes/secret.bin', 'wb') as f:
            f.write(secret)
        cipher = AES.new(secret, AES.MODE_CFB)
        with open('Aes/plaintext.txt', 'rb') as f:
            text = f.read()
        encoded = EncodeAES(cipher, text)
        with open('Aes/ciphertext.bin', 'wb') as f:
            f.write(encoded)
        print (encoded)
    if int(c) == 2:
        os.system('clear')
        print('Дешифруем значит')
        PADDING = '{'
        with open('Aes/ciphertext.bin', 'rb') as f:
            encoded = f.read()
        with open('Aes/secret.bin', 'rb') as keyfile:
            secret = keyfile.read()
        DecodeAES = lambda c, e:    c.decrypt(base64.b64decode(e)).rstrip(PADDING)
        cipher = AES.new(secret, AES.MODE_CFB)
        decoded = DecodeAES(cipher, encoded)
        with open('Aes/plaintext.txt', 'w') as f:
            f.write(str(decoded))
        print(decoded)    
但是当我试图解密一些文本时,我得到了以下错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 88, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "/home/tukanoid/Desktop/Enc_dec2/Enc_dec.py", line 475, in <module>
aes()
File "/home/tukanoid/Desktop/Enc_dec2/Enc_dec.py", line 178, in aes
encoded = EncodeAES(cipher, text)
File "/home/tukanoid/Desktop/Enc_dec2/Enc_dec.py", line 166, in <lambda>
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
File "/home/tukanoid/Desktop/Enc_dec2/Enc_dec.py", line 162, in <lambda>
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
TypeError: can't concat bytes to str
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/usr/lib/python3/dist packages/spyderlib/widgets/externalshell/sitecustomize.py”,第699行,在runfile中
execfile(文件名、命名空间)
文件“/usr/lib/python3/dist packages/spyderlib/widgets/externalshell/sitecustomize.py”,第88行,在execfile中
exec(编译(打开(文件名'rb').read(),文件名'exec'),命名空间)
文件“/home/tukanoid/Desktop/Enc_dec2/Enc_dec.py”,第475行,在
aes()
文件“/home/tukanoid/Desktop/Enc_dec2/Enc_dec.py”,第178行,aes格式
encoded=EncodeAES(密码、文本)
文件“/home/tukanoid/Desktop/Enc_dec2/Enc_dec.py”,第166行,在
EncodeAES=lambda c,s:base64.b64encode(c.encrypt(pad(s)))
文件“/home/tukanoid/Desktop/Enc_dec2/Enc_dec.py”,第162行,在
pad=λs:s+(块大小-透镜%s块大小)*填充
TypeError:无法将字节连接到str

实际上,我不知道lambda做了什么,但我认为我得到这个错误是因为它。谢谢。

您正在将文本文件读取为
rb
,在python 3中,它返回一个
字节
对象

在函数中,您使用
str
对象填充,因此会出现错误。Python3清楚地将二进制数据与文本数据分开,这是Python2没有做到的

    PADDING = '{'
    pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
通过
open('Aes/plaintext.txt','rb')
更改
open('Aes/plaintext.txt','r')
,您将在两侧获得ascii码,这将起作用


(或者将
PADDING
更改为
bytes('{',encoding=“ascii”)
或者正如James刚才所说的那样
b“{”
)。

坚持字节似乎适合aes加密,这是一种基于字节的操作。
PADDING=b'{
也应该可以工作。Tnx,但现在我有一个错误:EncodeAES=lambda c,s:base64.b64encode(c.encrypt)(pad(s)))expect_byte_string(纯文本)raise TypeError(“只能将字节字符串传递给C代码”)TypeError:只能将字节字符串传递给C代码然后替代解决方案:将填充更改为字节(“{',encoding=“ascii”),并将文件作为
“rb”
打开。告诉我它是如何为您工作的。可能的重复,您需要在任何地方使用字节。