• Crypto.AES 报错 | TypeError: Object type <class 'str'> cannot be passed to C code


    1 源代码:

        def decrypt(self, enc):
            """
            Requires hex encoded param to decrypt
            """
            enc = a2b_hex(enc)
            iv = enc[:16]
            enc = enc[16:]
            cipher = AES.new(self.key, AES.MODE_CBC, iv)
            return unpad(cipher.decrypt(enc).decode("utf-8"))

    2 报错信息:

    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

     3 报错代码:

    cipher = AES.new(self.key, AES.MODE_CBC, iv)

    4 报错原因:

    AES.new() 这个方法中的第一个参数的类型为byte型,因为我们的key值是string 型的,所以得转换一下类型。

    5 改正:

     def decrypt(self, enc):
            """
            Requires hex encoded param to decrypt
            """
            enc = a2b_hex(enc)
            iv = enc[:16]
            enc = enc[16:]
            cipher = AES.new(self.key.encode(), AES.MODE_CBC, iv)   #改动点在这里
            return unpad(cipher.decrypt(enc).decode("utf-8"))
    祈福@点亮希望
  • 相关阅读:
    css动画集合地址
    邮箱正则
    好用的工具之一 ---- Sublime Text
    组件化表单解决方案AForm 1.3 发布
    WinScp几个极大提高开发效率的小功能
    session的本质及如何实现共享?
    使用phantomjs操作DOM并对页面进行截图需要注意的几个问题
    Ubuntu 12.04 安装最新版本NodeJS
    IIS 8 nodejs + iisnode 配置
    Bagging和Boosting的介绍及对比
  • 原文地址:https://www.cnblogs.com/caizhou520/p/14680277.html
Copyright © 2020-2023  润新知