繁体   English   中英

使用密码学库在 Python 中生成椭圆曲线私钥

[英]Generating Elliptic Curve Private Key in Python with the cryptography library

我正在尝试使用密码学中的危险品在 python 中生成一个公共/私人椭圆曲线密钥对。 以下是我拥有的当前代码。 当我运行时,它生成错误“NoneType”对象没有属性“generate_elliptic_curve_private_key”

ecurve = asymmetric.ec.EllipticCurve
ecurve.name = 'secp256r1'
ecurve.key_size = 128
ec_backend = cryptography.hazmat.backends.interfaces.EllipticCurveBackend.generate_elliptic_curve_private_key(cryptography.hazmat.backends.interfaces.EllipticCurveBackend, ecurve)
key = asymmetric.ec.generate_private_key(curve=ecurve, backend=ec_backend)  

这是文档https://cryptography.io/en/latest/hazmat/primitives/asymmetric/ec/#

我没有看到generate_elliptic_curve_private_key方法在哪里可用。

以下是生成SECP256R1并将公钥序列化为 PEM 格式的示例:

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import ec

private_key = ec.generate_private_key(ec.SECP256R1(), default_backend())
public_key = private_key.public_key()
# serializing into PEM
rsa_pem = public_key.public_bytes(encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo)

打印密钥

In [14]: print(rsa_pem.decode())
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEilwhueTwySfEbXd9y/inZVsYVG6z
/UJyVbN+cYgtIFd0vLdaP27cME8RGE/enMEcX7/jkb13j2DPnXt2R6teZw==
-----END PUBLIC KEY-----

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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