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 in Python by Vasu
304 views

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 by Jishan

Related Questions In Python

0 votes
1 answer

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

Hey Yamini, you cannot concatenate a string ...READ MORE

answered May 31 in Python by Gargi
479 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 in Python by SDeb
• 13,190 points
26 views
0 votes
2 answers
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 in Python by Mir
509 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 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 349 views
+4 votes
6 answers
0 votes
3 answers
0 votes
1 answer