我一直得到" TypeError:只有字节字符串可以传递给C代码"当试图运行这个python ecnrypt

时间:2018-02-20 00:45:05

标签: python encryption

尝试运行此加密代码时,我一直收到TypeError

from Cryptodome.Cipher import AES 
import base64
import os


def encryption(privateInfo):
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)
print ('encryption key:', secret)

cipher = AES.new(secret,AES.MODE_CFB)

encoded = EncodeAES(cipher, privateInfo)
print( 'Encrypted string: ', encoded)

起初它告诉我,我需要指定线路的模式 ' cipher = AES.new(secret)'。一旦我这样做,它给了我下面的错误。

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Eddie\source\repos\pycrypto_1\pycrypto_1\pycrypto_1.py", line 19, in encryption
    encoded = EncodeAES(cipher, privateInfo)
  File "C:\Users\Eddie\source\repos\pycrypto_1\pycrypto_1\pycrypto_1.py", line 12, in <lambda>
    EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s))
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\Cryptodome\Cipher\_mode_cfb.py", line 156, in encrypt
    expect_byte_string(plaintext)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\Cryptodome\Util\_raw_api.py", line 196, in expect_byte_string
    raise TypeError("Only byte strings can be passed to C code")
TypeError: Only byte strings can be passed to C code

我会说代码确实成功打印了加密密钥。它只会在到达应该打印出加密消息的代码部分时给出错误。

0 个答案:

没有答案