Python: “TypeError: Object type <class 'str'> cannot be passed to C code”

0 votes

I am trying to run the following code:

from Cryptodome.Cipher import AES

key = '0123456789abcdef'
IV='0123456789abcdef'
mode = AES.MODE_CBC
encryptor = AES.new(key, mode,IV=IV)
text = 'hello'
ciphertext = encryptor.encrypt(text)

 But I am getting the following error:

Traceback (most recent call last):

  File "F:/Python Scripts/sample.py", line 8, in <module>

    ciphertext = encryptor.encrypt(text)

  File "F:\Python Scripts\Crosspost\venv\lib\site-packages\Cryptodome\Cipher\_mode_cbc.py", line 178, in encrypt

    c_uint8_ptr(plaintext),

  File "F:\Python Scripts\Crosspost\venv\lib\site-packages\Cryptodome\Util\_raw_api.py", line 145, 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
Aug 20, 2019 in Python by Vasu
4,383 views

Hey,

Did you miss to encode the IV!

You can try something like this:

key = b'0123456789abcdef'
IV= b'0123456789abcdef'   #need to be encoded too
mode = AES.MODE_CBC
encryptor = AES.new(key, mode,IV=IV)
text = b'hello'
ciphertext = encryptor.encrypt(text)

I hope this will work.

1 answer to this question.

0 votes

You have to pass byte string as values so try encoding the data values to byte and then pass it. Try this:

from Cryptodome.Cipher import AES

key = '0123456789abcdef'
IV='0123456789abcdef'
mode = AES.MODE_CBC
encryptor = AES.new(key.encode('utf8'), mode,IV=IV.encode('utf8'))
text = 'hello'
ciphertext = encryptor.encrypt(text)
answered Aug 20, 2019 by Jishan
still happen the same error
Hey,

Did you check the other answer given above? I hope that might help.
Python3 has package named pycryptodome which causes this error.just uninstall the package

sudo pip3 uninstall pycryptodome

Related Questions In Python

0 votes
2 answers

Python error "TypeError: Can't convert 'int' object to str implicitly"

A TypeError can occur if the type ...READ MORE

answered Feb 5 in Python by lovelmark
• 160 points
1 flag 3,440 views
0 votes
1 answer

How to get base class type in Python?

Yes, There is an alternative. You can ...READ MORE

answered Feb 8, 2019 in Python by SDeb
• 13,250 points
297 views
0 votes
1 answer

Python error "TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str'"

Hey @Ashish, change the emotion_map to the ...READ MORE

answered May 30, 2019 in Python by Mir
2,643 views
0 votes
1 answer

Python error "TypeError: string indices must be integers, not str"

Hey @Dipti email_s.append(email_1["email_address"]) This is the list on ...READ MORE

answered Jul 5, 2019 in Python by Jinu
17,476 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,447 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,800 points
1,080 views
0 votes
3 answers
0 votes
1 answer

Python: For loop to iterate class object

Refer to the below code: class Try: ...READ MORE

answered May 22, 2019 in Python by Raj
1,106 views