Python 报错解决 TypeError: Object type cannot be passed to C code

此文首发于我的个人博客:zhang0peter的个人博客


下午在使用Crypto的时候报错如下:

Traceback (most recent call last):
  File "/usr/lib64/python3.6/multiprocessing/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "/usr/lib64/python3.6/multiprocessing/pool.py", line 44, in mapstar
    return list(map(*args))
  File "FastFinally.py", line 86, in generate2
    next(yield_32),next(yield_32)
  File "FastFinally.py", line 55, in AES_32_YIELD
    yield int(aes.encrypt(data).hex(),16)
  File "/usr/local/lib64/python3.6/site-packages/pycryptodome-3.9a0-py3.6-linux-x86_64.egg/Crypto/Cipher/_mode_ctr.py", line 201, in encrypt
    c_uint8_ptr(plaintext),
  File "/usr/local/lib64/python3.6/site-packages/pycryptodome-3.9a0-py3.6-linux-x86_64.egg/Crypto/Util/_raw_api.py", line 235, in c_uint8_ptr
    raise TypeError("Object type %s cannot be passed to C code" % type(data))
TypeError: Object type <class 'str'> cannot be passed to C code
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "FastFinally.py", line 97, in <module>
    pool.map(generate2,l)
  File "/usr/lib64/python3.6/multiprocessing/pool.py", line 266, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File "/usr/lib64/python3.6/multiprocessing/pool.py", line 644, in get
    raise self._value
TypeError: Object type <class 'str'> cannot be passed to C code

我的代码如下:

from Crypto.Cipher import AES
from Crypto.Util import Counter
import os
key=os.urandom(16)
aes = AES.new(key, AES.MODE_CTR, counter=Counter.new(128))
data = "0" * 8
temp=aes.encrypt(data)

这部分代码在Windows上跑没问题,但是到Linux上跑就出错了。

把代码修改如下:

temp=aes.encrypt(data.encode('utf-8'))

只能说坑真多