我使用的是Mac Anaconda。我试着使用加密的AES。然而,我面临着一个奇怪的问题。
我只想执行一行简单的代码:
obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
如果我像下面这样在没有虚拟环境的情况下运行代码,它是可以的。
$ python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more
information.
>>> from Crypto.Cipher import AES
>>> obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
如果我使用虚拟环境"testaes“运行,那么我会得到错误:
(testaes)$ python
Python 3.6.4 |Anaconda, Inc.| (default, Mar 12 2018, 20:05:31)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from Crypto.Cipher import AES
>>> obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Applications/anaconda3/envs/testaes/lib/python3.6/site-packages/Crypto/Cipher/AES.py", line 200, in new
return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
File "/Applications/anaconda3/envs/testaes/lib/python3.6/site-packages/Crypto/Cipher/__init__.py", line 55, in _create_cipher
return modes[mode](factory, **kwargs)
File "/Applications/anaconda3/envs/testaes/lib/python3.6/site-packages/Crypto/Cipher/_mode_cbc.py", line 234, in _create_cbc_cipher
cipher_state = factory._create_base_cipher(kwargs)
File "/Applications/anaconda3/envs/testaes/lib/python3.6/site-packages/Crypto/Cipher/AES.py", line 100, in _create_base_cipher
result = start_operation(c_uint8_ptr(key),
File "/Applications/anaconda3/envs/testaes/lib/python3.6/site-packages/Crypto/Util/_raw_api.py", line 109, 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
您可以看到,在这两个时间我使用相同的Anaconda Python3.6.4和GCC4.2.1。如何解决这个问题?
发布于 2018-05-12 04:56:38
在Python3中,将其编码为bytearray
obj = AES.new('This is a key123'.encode("utf8"), AES.MODE_CBC, 'This is an IV456'.encode("utf8"))
如果您将它们存储在变量中,并希望再次将它们用作(Python)字符串,则只需使用:
key_as_bytearray.decode("utf8")
有关更多信息,请查看this answer。
发布于 2020-05-14 11:41:13
Python有一个名为pycryptodome的包,这会导致此error.just卸载该包
sudo pip3卸载pycryptodome
https://stackoverflow.com/questions/50302827
复制相似问题