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

10,421

just update unpad to be unpad = lambda s : s[0:-ord(s[-1:])] the main issue that ord() expects string of length one if you try to print value of s[-1] it prints 10 which not one char but s[-1:] printed value is b'\n' which is one char

also encode key to be bytes bytes(key, 'utf-8') and pad

pad = lambda s: bytes(s + (BS - len(s) % BS) * chr(BS - len(s) % BS), 'utf-8')

to make sure all inputs are bytes

from hashlib import sha256
import base64
from Crypto import Random
from Crypto.Cipher import AES

BS = 16
pad = lambda s: bytes(s + (BS - len(s) % BS) * chr(BS - len(s) % BS), 'utf-8')
unpad = lambda s : s[0:-ord(s[-1:])]

class AESCipher:

    def __init__( self, key ):
        self.key = bytes(key, 'utf-8')

    def encrypt( self, raw ):
        raw = pad(raw)
        iv = Random.new().read( AES.block_size )
        cipher = AES.new(self.key, AES.MODE_CBC, iv )
        return base64.b64encode( iv + cipher.encrypt( raw ) )

    def decrypt( self, enc ):
        enc = base64.b64decode(enc)
        iv = enc[:16]
        cipher = AES.new(self.key, AES.MODE_CBC, iv )
        return unpad(cipher.decrypt( enc[16:] )).decode('utf8')

cipher = AESCipher('mysecretpassword')
encrypted = cipher.encrypt('Secret')
decrypted = cipher.decrypt(encrypted)

print(encrypted)
print(decrypted)
Share:
10,421
Jozf
Author by

Jozf

Updated on June 04, 2022

Comments

  • Jozf
    Jozf almost 2 years

    I am trying to implement aes encryption and decryption in python. When I execute code, it returns error. I have installed anaconda on my machine. I am running scripts in jupyter notebook.

    !pip install pycryptodome
    
    import base64
    from Crypto import Random
    from Crypto.Cipher import AES
    
    BS = 16
    pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
    unpad = lambda s : s[0:-ord(s[-1])]
    
    class AESCipher:
    
        def __init__( self, key ):
            self.key = key
    
        def encrypt( self, raw ):
            raw = pad(raw)
            iv = Random.new().read( AES.block_size )
            cipher = AES.new( self.key, AES.MODE_CBC, iv )
            return base64.b64encode( iv + cipher.encrypt( raw ) )
    
        def decrypt( self, enc ):
            enc = base64.b64decode(enc)
            iv = enc[:16]
            cipher = AES.new(self.key, AES.MODE_CBC, iv )
            return unpad(cipher.decrypt( enc[16:] ))
    
    cipher = AESCipher('mysecretpassword')
    encrypted = cipher.encrypt('Secret')
    decrypted = cipher.decrypt(encrypted)
    print(encrypted)
    print(decrypted)
    

    How to solve this ?

    • Attie
      Attie over 5 years
      please fix your indentation. what is the error? (which line, etc...)
    • Jozf
      Jozf over 5 years
      raw=pad(raw). TypeError: Object type <class 'str'> cannot be passed to C code
    • t.m.adam
      t.m.adam over 5 years
      Your code is written for Python2, in Python3 str and bytes are differend objects. You'll have to replace all your strings with bytes (see str.encode).
    • Jozf
      Jozf over 5 years
      I made the following changes to the above code. pwd=b"mysecretpassword" cipher = AESCipher(pwd) msg=b"Secret" encrypted = cipher.encrypt(msg) print(encrypted) print(decrypted) It returns TypeError: can't concat str to bytes
  • Jozf
    Jozf over 5 years
    It again returns the same error. TypeError: Object type <class 'str'> cannot be passed to C code
  • Ahmed Yousif
    Ahmed Yousif over 5 years
    @Jozf I have updated my answer I think it is working fine now I tried it on jjupyter notebook
  • Jozf
    Jozf over 5 years
    Thank you. But in the output, there is b before encrypted and decrypted data. Did you see that ? b'6tx+0XpByP2w2cluCcDgYLnx3Zu65vbM8+Db2su4yKg=' b'Secret' How can we remove that ?
  • Ahmed Yousif
    Ahmed Yousif over 5 years
    @Jozf just decode the result .decode('utf8') I updated it in answer