python将pkcs8的私钥转换为pkcs1
场景是与支付宝对接,手里只有java sdk使用的pkcs8的私钥,但需要使用python sdk开发一些工具,私钥不兼容,所以需要进行转换。
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
private_key_base64 = '[pkcs8私钥]'
private_key_bytes = base64.b64decode(private_key_base64)
private_key_pem = f"-----BEGIN PRIVATE KEY-----\n{base64.b64encode(private_key_bytes).decode('utf-8')}\n-----END PRIVATE KEY-----"
private_key = serialization.load_pem_private_key(
private_key_pem.encode('utf-8'),
password=None
)
private_key_pkcs1_pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
)
print(private_key_pkcs1_pem.decode('utf-8'))
联系我:ryan255@163.com
github:https://github.com/ryan255
github:https://github.com/ryan255