zoukankan      html  css  js  c++  java
  • 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"))
    祈福@点亮希望
  • 相关阅读:
    解决做好一个机器学习项目的3个问题
    VMwareworkstations14 安装arch
    python3学习笔记——数字、字符串、列表、字典、元组
    python2和python3编码
    重定向和伪静态的原理、语法、实践
    HTTP中的重定向和请求转发的区别
    linux+Apache开启伪静态配置
    Centos7下Yum安装PHP5.5,5.6,7.0
    windows10图形化连接CentOS7
    python学习笔记--类(一)
  • 原文地址:https://www.cnblogs.com/caizhou520/p/14680277.html
Copyright © 2011-2022 走看看