简体   繁体   English

Object型<class 'str'>无法传递给 C 代码 - 虚拟环境</class>

[英]Object type <class 'str'> cannot be passed to C code - virtual environment

I am using Mac Anaconda.我正在使用 Mac Anaconda。 And I try to use the AES of Crypto.我尝试使用加密的 AES。 However, I face a strange problem.但是,我面临一个奇怪的问题。

I just want to excute a simple line of code:我只想执行一行简单的代码:

 obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')

if I run the code without the virtual environment as below it is OK.如果我在没有虚拟环境的情况下运行代码,如下所示。

$ 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')

if I run with the virtual environment "testaes" then I got the error:如果我使用虚拟环境“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

You can see that at both time I use the the same Anaconda Python 3.6.4 and GCC4.2.1.您可以看到,在这两次我使用相同的 Anaconda Python 3.6.4 和 GCC4.2.1。 How to solve this?如何解决这个问题?

In Python 3, encode it into a bytearray :在 Python 3 中,将其编码为bytearray

obj = AES.new('This is a key123'.encode("utf8"), AES.MODE_CBC, 'This is an IV456'.encode("utf8"))

If you store these in variables and want to use them as (Python) strings again, just use:如果您将这些存储在变量中并想再次将它们用作(Python)字符串,只需使用:

key_as_bytearray.decode("utf8")

Check out this answer for further information.查看此答案以获取更多信息。

from Crypto.Cipher import AES
import base64

def encrypt(text):  
    private_key = "xxx".encode('utf-8')   
    iv = "yyy".encode('utf-8')
    cipher = AES.new(private_key, AES.MODE_CBC, iv)

    pad = lambda s: s + (16 - len(s) % 16) * chr(16 - len(s) % 16)
    encrypted =  base64.b64encode( cipher.encrypt(pad(text)))
    return encrypted.decode("utf-8")

This was working fine in Google Collab but It was not showing this above error in Python3 Terminal in Linux.这在 Google Collab 中运行良好,但在 Linux 的 Python3 终端中没有显示上述错误。 So What I did is -所以我所做的是——

from Crypto.Cipher import AES
from base64 import b64encode
from Crypto.Util.Padding import pad, unpad

def encrypt(text):
    private_key = bytes("xxx", 'utf-8')
    iv = bytes("yyy", 'utf-8')
    cipher = AES.new(private_key, AES.MODE_CBC, iv)

    encrypted = cipher.encrypt(pad(text.encode("UTF-8"), AES.block_size))
    return b64encode(encrypted).decode('utf-8')      

Just encode the key and you will get no error:只需对密钥进行编码,就不会出现错误:

from Crypto.Cipher import AES
import base64

key = ''

cipher_text = ''
cipher_text = base64.b64decode(cipher_text)


decipher = AES.new(key.encode(), AES.MODE_ECB)

print(decipher.decrypt(cipher_text)) 

Python has package named pycryptodome which causes this error.just uninstall the package Python 有一个名为 pycryptodome 的包,这会导致此错误。只需卸载该包

sudo pip3 uninstall pycryptodome须藤 pip3 卸载 pycryptodome

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:对象类型<class 'str'>不能传递给 C 代码 - TypeError: Object type <class 'str'> cannot be passed to C code PyCryptoDome版本3.6.6引发TypeError:对象类型 <class 'str'> 无法传递给C代码 - PyCryptoDome Version 3.6.6 raises an TypeError: Object type <class 'str'> cannot be passed to C code python 中的 DES 加密错误(TypeError: Object 类型<class 'str'>不能传给 C 码)</class> - DES Encryption in python error(TypeError: Object type <class 'str'> cannot be passed to C code) “类型错误:Object 类型<class 'str'>加密图像时无法传递给 C 代码</class> - "TypeError: Object type <class 'str'> cannot be passed to C code when encrypting an image Object型<class 'tuple'>在 django 中加密文件时无法传递给 C 代码</class> - Object type <class 'tuple'> cannot be passed to C code when encrypting file in django 类型错误:无法连接类型为 &#39; 的对象<class 'str'> &#39;; 只有 Series 和 DataFrame 对象有效 - TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid 打印具有类型的对象中的特定值<class 'str'> - Printing specific values in an object with type <class 'str'> “str”类型对象的未知格式代码“g” - Unknown format code 'g' for object of type 'str' 类型“ str”的对象的未知格式代码“ b” - Unknown format code 'b' for object of type 'str' 无法安装到虚拟环境 - Cannot Install into Virtual Environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM