Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I am using Mac Anaconda. And I try to use the AES of Crypto. 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)$ 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. How to solve this?

like image 605
geen Frog Avatar asked May 12 '18 04:05

geen Frog


1 Answers

In Python 3, encode it into a 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:

key_as_bytearray.decode("utf8") 

Check out this answer for further information.

like image 194
Arthur Dent Avatar answered Sep 21 '22 04:09

Arthur Dent