Search code examples
python-3.xpycryptodome

Getting Exception: Object type <class 'str'> cannot be passed to C code


I install pip install pycryptodome on Python 3.7.2. I'm getting above exception for obj = AES.new(key, AES.MODE_CBC, iv) line. my code is:

from Crypto import Random
from Crypto.Cipher import AES
import random

def get_encryption():
    try:
        str = "This is input string"

        key = b'abcdefghijklmnop'  
        iv = Random.new().read(AES.block_size)

        obj = AES.new(key, AES.MODE_CBC, iv)
        encrypted = obj.encrypt(str)
        print(encrypted)
    except Exception as e:
        print(e)

I tried to all the way but not getting how to solve it.


Solution

  • After tried all the way I got solution. I converted key string into bytes. code is:

    from Crypto import Random
    from Crypto.Cipher import AES
    import random
    
    def get_encryption():
        try:
            strmsg = "This is input string"
    
            key = 'abcdefghijklmnop'  
            key1 = str.encode(key)
    
            iv = Random.new().read(AES.block_size)
    
            obj = AES.new(key1, AES.MODE_CBC, iv)
            encrypted = obj.encrypt(str.encode(strmsg))
            print(encrypted)
        except Exception as e:
            print(e)