[英]Python convert Private Key to RSA Key
I have a Private Key with the following format我有一个格式如下的私钥
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIE6TAbBgkqhki....
----END ENCRYPTED PRIVATE KEY-----
How can I convert this in a key with RSA format如何将其转换为具有 RSA 格式的密钥
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA28jIsb8SAhJritwO....
-----END RSA PRIVATE KEY-----
The current version of cryptography I have is 2.8.我拥有的当前密码学版本是 2.8。 Any help is really appreciated.
非常感谢任何帮助。 Thanks in advance.
提前致谢。
As described in the comment by Maarten Bodewes, a conversion of a encrypted private key in PKCS#8 format to a private key in PKCS#1 format (both PEM encoded) is possible with OpenSSL.如 Maarten Bodewes 的评论中所述,使用 OpenSSL 可以将 PKCS#8 格式的加密私钥转换为 PKCS#1 格式的私钥(均为 PEM 编码)。 But this can also be done with the Cryptography library.
但这也可以通过 Cryptography 库来完成。
The Cryptography library supports the import of (encrypted) private keys in PKCS#8 format, PEM encoded, with the method load_pem_private_key()
(since version 0.6), eg: Cryptography 库支持使用
load_pem_private_key()
方法(自 0.6 版起)导入 PKCS#8 格式、PEM 编码的(加密)私钥,例如:
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
pkcs8Encrypted = b"""-----BEGIN ENCRYPTED PRIVATE KEY-----
MIICzzBJB...
-----END ENCRYPTED PRIVATE KEY-----"""
privateKey = serialization.load_pem_private_key(
pkcs8Encrypted,
b'mypassword',
default_backend()
)
The export of private keys in PKCS#1 format, PEM encoded is possible with private_bytes()
(since version 0.2): PKCS#1 格式的私钥导出,PEM 编码可以使用
private_bytes()
(从版本 0.2 开始):
pkcs1 = privateKey.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
)
print(pkcs1.decode('utf-8')) # -----BEGIN RSA PRIVATE KEY-----...
The current version of Cryptography is 3.4.7 (Mar 2021). Cryptography 的当前版本是 3.4.7(2021 年 3 月)。 2.8 is from Oct 2019, s.
2.8 来自 2019 年 10 月,第Release history .
发布历史。 Actually, both methods should therefore be available.
实际上,因此这两种方法都应该可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.