0

我有一个 base64 编码的证书。我可以像这样使用 Java 获取公钥:

    private static final String CERTIFICATE = "MIIGXDCCBUSgAwIBAgIMNrcrYQDXRuN4uLHeMA0GCSqGSIb3DQEBC........";
    X509Certificate cert = (X509Certificate) CertificateFactory.getInstance("X.509")
            .generateCertificate(new ByteArrayInputStream(Base64.getDecoder().decode(CERTIFICATE)));
    PublicKey publicKey = cert.getPublicKey();

但现在我尝试使用 Python 3 做同样的事情。我找不到任何示例如何从 base64 编码字符串中获取公钥。有人可以帮助或指向我一些示例代码来获取我以后可以用来加密 JWT 的公钥。

谢谢大卫

4

1 回答 1

0
from cryptography.x509 import load_pem_x509_certificate
from cryptography.hazmat.backends import default_backend

cert_str = '-----BEGIN CERTIFICATE----- MUST HAVE THE BEGIN AND END CERTIFICATE -----END CERTIFICATE-----';

cert_obj = load_pem_x509_certificate(str.encode(cert_str), default_backend())
public_key = cert_obj.public_key();
于 2020-11-05T01:05:56.970 回答