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
20,771 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)


Hope this will help!

To learn more, go for Python Master course today.

Thank!

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
key = '0123456789abcdef'
IV='0123456789abcdef'
mode = AES.MODE_CBC
#encode the key and IV
encryptor = AES.new(key.encode('utf-8'), mode,IV=IV.encode('utf-8'))
# pad the text to 16bytes
text = 'hello'
padded_text = text + "0"*(16-len(text))
#encode the text
ciphertext = encryptor.encrypt(padded_text.encode("utf-8"))
print(ciphertext)

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, 2020 in Python by lovelmark
• 160 points
1 flag 18,050 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,300 points
1,751 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
7,635 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
22,676 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,023 views
0 votes
1 answer
+5 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,790 points
3,420 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
7,665 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP