python加密模块中的错误:_RSAPrivateKey'对象没有属性'sign [英] Error in python cryptography module: _RSAPrivateKey' object has no attribute 'sign

查看:163
本文介绍了python加密模块中的错误:_RSAPrivateKey'对象没有属性'sign的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Python代码中,我正在使用密码学模块.我在磁盘上有一个私钥.因此,从文档中,我使用了此示例加载该密钥.然后使用该密钥对消息进行签名.但是运行程序会引发AttributeError:'_ RSAPrivateKey'对象没有属性'sign'

In my Python code, I'm using cryptography module. I have a private key on disk. So, from documentation, I used this example to load that key. Then use that key to sign a message. But running the program throws AttributeError: '_RSAPrivateKey' object has no attribute 'sign'

我查看了 load_pem_private_key().该代码需要对抽象基类有所了解.

I looked in to source code of serialization module and check return type of load_pem_private_key(). The code requires some understanding of Abstract Base Classes.

在此处寻求帮助以调试此问题.

Seeking help here to debug this issue.

这是我的代码

  1 from cryptography.hazmat.backends import default_backend
  2 from cryptography.hazmat.primitives import hashes
  3 from cryptography.hazmat.primitives import serialization
  4 from cryptography.hazmat.primitives.asymmetric import padding
  5 from cryptography.hazmat.primitives.asymmetric import utils
  6 
  7 from base64 import b64encode
  8 
  9 def test_new_crypto():
 10     privkey = '/path/to/privkey'
 11     with open(privkey, "rb") as kf:
 12         private_key = serialization.load_pem_private_key(
 13                 kf.read(),
 14                 password=None,
 15                 backend=default_backend()
 16                 )
 17 
 18     message = b"A message I want to sign"
 19     signature = private_key.sign(  #### Error is here
 20             message,
 21             padding.PSS(
 22                 mgf=padding.MGF1(hashes.SHA256()),
 23                 salt_length=padding.PSS.MAX_LENGTH
 24                 ),
 25             hashes.SHA256()
 26             )
 27 
 28     return b64encode(signature)
 29 
 30 if __name__ == "__main__":
 31     print(test_new_crypto())

推荐答案

You mention you are running an outdated version.

从1.7.1版升级到2.6.1版可以解决此问题.

Upgrading from version 1.7.1 to 2.6.1 resolves the issue.

这篇关于python加密模块中的错误:_RSAPrivateKey'对象没有属性'sign的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆