您可以在加密模块的帮助下加载 DER 密钥并将其转储为受密码保护的 PEM 密钥,如下所示:
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
private_key = serialization.load_der_private_key(
der_data, # assuming that "der_data" variable contains your DER key
password=None,
backend=default_backend()
)
pem_encrypted = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.BestAvailableEncryption(b'mypassword')
)
print(pem_encrypted.decode()) # -----BEGIN ENCRYPTED PRIVATE KEY-----...