zoukankan      html  css  js  c++  java
  • Crypto.AES 报错 | TypeError: Object type <class 'str'> cannot be passed to C code

    一、加密代码

    import base64
    import zlib
    from Crypto.Cipher import AES
    from Crypto import Random
    
    
    BLOCK_SIZE = 16
    
    
    def pad(s): return s + ((BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)).encode("utf-8")
    
    
    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 iv + cipher.encrypt(raw)
    
    
    def encode(data, key) -> bytes:
    
        """
        :param data: 序列化后的数据
        :param key: 加密的 key
        :return: 
        """
    
        # 压缩
        compress_data = zlib.compress(data)
    
        # 加密
        aes = AESCipher(key)
        encrypt_data = aes.encrypt(compress_data)
    
        # 转成Base64编码
        encode_data = base64.b64encode(encrypt_data)
        return encode_data
    
    
    if __name__ == '__main__':
        key = hashlib.md5(str(time.time()).encode('utf-8')).hexdigest()
        json_data = json.dumps({'data': 'test'})
        encode_data = encode(json_data, key)
    View Code

    二·、报错相关

    1、报错信息
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
      File "D:Python37LibCryptoCipherAES.py", line 232, in new
        return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
      File "D:Python37LibCryptoCipher\__init__.py", line 79, in _create_cipher
        return modes[mode](factory, **kwargs)
      File "D:Python37LibCryptoCipher\_mode_cbc.py", line 274, in _create_cbc_cipher
        cipher_state = factory._create_base_cipher(kwargs)
      File "D:Python37LibCryptoCipherAES.py", line 103, in _create_base_cipher
        result = start_operation(c_uint8_ptr(key),
      File "D:Python37LibCryptoUtil\_raw_api.py", line 144, 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
    
    
    2、报错代码
    cipher = AES.new(self.key, AES.MODE_CBC, iv)
    View Code

    三、报错原因

      AES.new 方法的第一个参数,需要为 bytes/bytearray/memoryview 类型,报错代码中传递的是 字符串 类型,将其转为 bytes 类型即可

    四、修改之后

    import base64
    import zlib
    from Crypto.Cipher import AES
    from Crypto import Random
    
    
    BLOCK_SIZE = 16
    
    
    def pad(s): return s + ((BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)).encode("utf-8")
    
    
    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.encode(), AES.MODE_CBC, iv)       ### 我在这里
            return iv + cipher.encrypt(raw)
    
    
    def encode(data, key) -> bytes:
    
        """
        :param data: 序列化后的数据
        :param key: 加密的 key
        :return: 
        """
    
        # 压缩
        compress_data = zlib.compress(data)
    
        # 加密
        aes = AESCipher(key)
        encrypt_data = aes.encrypt(compress_data)
    
        # 转成Base64编码
        encode_data = base64.b64encode(encrypt_data)
        return encode_data
    
    
    if __name__ == '__main__':
        key = hashlib.md5(str(time.time()).encode('utf-8')).hexdigest()
        json_data = json.dumps({'data': 'test'})
        encode_data = encode(json_data, key)
    View Code
  • 相关阅读:
    back-不忘初心,方得始终。讲讲我主场3个月的经历。题外话。
    js中event事件对象的兼容问题以及坐标属性-(clientX clientY;pageX,pageY,offsetX,offsetY)
    布局(左边的div随着右边div的高度变化而变化)
    事件委托如何实现的原理
    寄生组合式继承方法的实现以及原理总结
    封装判断一个字符的后缀名和前缀的方法
    经典的面试题如果不通过其他任何变量实现两个数值类型的变量互相更换值。
    【十次方基础教程(后台)】Dockerfile脚本完成镜像的构建
    【十次方基础教程(后台)】安装并启动RabbitMQ
    【十次方基础教程(后台)】docker下安装head插件来进行Elasticsearch的操作
  • 原文地址:https://www.cnblogs.com/hsmwlyl/p/13731863.html
Copyright © 2011-2022 走看看