简体   繁体   中英

PyCryptoDome Version 3.6.6 raises an TypeError: Object type <class 'str'> cannot be passed to C code

A couple o' years ago i created a cryptograph module in python with PyCryptoDome Version 2.6.1.

Now with the new PyCryptoDome Version 3.6.6 and Python 3.6 i got a TypeError with the same code. The error is thrown in the "_init_crypter" - method:

  File "/usr/local/lib/python3.6/dist-packages/Crypto/Cipher/AES.py", line 206, in new
    return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/Crypto/Cipher/__init__.py", line 79, in _create_cipher
    return modes[mode](factory, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/Crypto/Cipher/_mode_cbc.py", line 253, in _create_cbc_cipher
    return CbcMode(cipher_state, iv)
  File "/usr/local/lib/python3.6/dist-packages/Crypto/Cipher/_mode_cbc.py", line 96, in __init__
    c_uint8_ptr(iv),
  File "/usr/local/lib/python3.6/dist-packages/Crypto/Util/_raw_api.py", line 196, 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

How can i fix this!

My Code for both versions:

import os
from base64 import b64encode, b64decode
import hashlib
from Crypto.Cipher import Blowfish, AES


class Crypt:

    fileextension = ".enc"

    def __init__(self, crypter, key="", encoding="utf-8"):
        self._mode = -1
        self._encoding = encoding
        self._crypter = Blowfish
        self._blocksize = 8
        self._key_32byte = ""
        self._cryptographer = None

        self.set_crypter(crypter)
        if key:
            self.set_key(key)

    def _init_crypter(self):

        # self._key_32byte          <- set in set_key-method
        # self._mode = AES.MODE_CBC <- set in set_crypter-method
        # self._blocksize = 16      <- set in set_crypter-method

        self._cryptographer = self._crypter.new(
            self._key_32byte,
            self._mode,
            IV=self._blocksize * '\x00'
        )

    # and further code ...

The key and IV parameter must be a binary type ( bytes ) and not a text type ( str ).

If you're loading your key from a file, be sure to open it in binary mode:

with open('keyfile', 'rb') as f:
    ...

If your key is a literal in code, make sure it is a binary literal:

key = b'badbadbadkey'

And last, to construct your IV parameter, it must be a sequence of bytes .

For example:

>>> Blowfish.new(b'badbadbadkey', 2, IV=8 * b'\x00')
<Crypto.Cipher._mode_cbc.CbcMode object at 0x7f434187b8d0>

Ok - after hours i got it! Only one letter solves the problem: '\\x00' must be b '\\x00':

def _init_crypter(self):
    self._cryptographer = self._crypter.new(
        self._key_32byte,
        self._mode,
        IV=self._blocksize * b'\x00'
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM