将 python x509 签名请求对象 (x509.CertificateSigningRequest) 对象转换为字节

Posted

技术标签:

【中文标题】将 python x509 签名请求对象 (x509.CertificateSigningRequest) 对象转换为字节【英文标题】:Convert python x509 Signing Request object (x509.CertificateSigningRequest) object to bytes 【发布时间】:2021-08-28 11:57:50 【问题描述】:

我需要将 x509.CertificateSigningRequest 对象转换为字节

这里是返回 x509.CertificateSigningRequest 对象的方法

def csr(private_key: rsa.RSAPrivateKey) -> x509.CertificateSigningRequest:
    b = x509.CertificateSigningRequestBuilder()
    req = b.subject_name(x509.Name([
        x509.NameAttribute(NameOID.COUNTRY_NAME, u"US"),
        x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, u"CA"),
        x509.NameAttribute(NameOID.LOCALITY_NAME, u"San Francisco"),
        x509.NameAttribute(NameOID.ORGANIZATION_NAME, u"Command1"),
        x509.NameAttribute(NameOID.COMMON_NAME, u"Commandment"),
    ])).sign(private_key, hashes.SHA256(), default_backend())

    return req 

有人知道怎么做吗? 提前致谢

【问题讨论】:

【参考方案1】:

只是在做

req_bytes=req.public_bytes(encoding=serializaion.Encoding.PEM)

解决问题

【讨论】:

以上是关于将 python x509 签名请求对象 (x509.CertificateSigningRequest) 对象转换为字节的主要内容,如果未能解决你的问题,请参考以下文章