登录
首页 >  Golang >  Go问答

使用 golang 如何生成 rsa 证书,然后将私钥导出到 pfx 文件,将公钥导出到 cer 文件

来源:stackoverflow

时间:2024-04-20 19:33:40 315浏览 收藏

从现在开始,我们要努力学习啦!今天我给大家带来《使用 golang 如何生成 rsa 证书,然后将私钥导出到 pfx 文件,将公钥导出到 cer 文件》,感兴趣的朋友请继续看下去吧!下文中的内容我们主要会涉及到等等知识点,如果在阅读本文过程中有遇到不清楚的地方,欢迎留言呀!我们一起讨论,一起学习!

问题内容

我想通过 golang 生成一些证书,但遇到了问题。

当我在 php 中执行此操作时,可能会像这些代码一样:

$privkey = openssl_pkey_new([
    'digest_alg' => 'des3',
    'private_key_bits' => 1024,
    'private_key_type' => openssl_keytype_rsa

]);
$csr = openssl_csr_new([
    "countryname" => "en",
    "stateorprovincename" => "province",
    "localityname" => "city",
    "organizationname" => "org",
    "organizationalunitname" => "org",
    "commonname" => "name",
], $privkey, ['digest_alg' => 'des3']);

$x509 = openssl_csr_sign($csr, null, $privkey, $days=3650, ['digest_alg' => 'des3']);

openssl_x509_export_to_file($x509, "/path/to/pub.cer");
openssl_pkcs12_export_to_file($x509, "/path/to/pri.pfx", $privkey, "password");

现在使用 golang 我只能创建私钥,但不知道下一步该怎么做。请帮助我,非常感谢。

func main() {
    keyBytes, err := rsa.GenerateKey(rand.Reader, bitSize)
    if err != nil {
        panic(err)
    }
    if err := keyBytes.Validate(); err != nil {
        panic(err)
    }
    dn := pkix.Name{
        Country:            []string{"EN"},
        Organization:       []string{"org"},
        OrganizationalUnit: []string{"org"},
        Locality:           []string{"city"},
        Province:           []string{"province"},
        CommonName:         "name",
    }
    asn1Dn, err := asn1.Marshal(dn)
    if err != nil {
        panic(err)
    }
    template := x509.CertificateRequest{
        Raw:                asn1Dn,
        SignatureAlgorithm: x509.SHA256WithRSA,
    }

    csrBytes, err := x509.CreateCertificateRequest(rand.Reader, &template, keyBytes)
    // how to do with csrBytes next?
}

解决方案


最后我自己解决了这个问题。现在我应该为其他面临同样问题的人写下答案。

解决方案是使用 software.sslmate.com/src/go-pkcs12,它提供函数 Encode 来编码从 x509.createcertificate 创建的给定字节数据。

看下面的示例代码:

func main() {
    keyBytes, err := rsa.GenerateKey(rand.Reader, 1024)

    if err != nil {
        panic(err)
    }

    if err := keyBytes.Validate(); err != nil {
        panic(err)
    }

    template := &x509.Certificate{
        SerialNumber: big.NewInt(1),
        Subject: pkix.Name{
            Country:            []string{"EN"},
            Organization:       []string{"org"},
            OrganizationalUnit: []string{"org"},
            Locality:           []string{"city"},
            Province:           []string{"province"},
            CommonName:         "name",
        },
    }

    derBytes, err := x509.CreateCertificate(rand.Reader, template, template, &keyBytes.PublicKey, keyBytes)

    if err != nil {
        panic(err)
    }

    cert, err := x509.ParseCertificate(derBytes)

    if err != nil {
        panic(err)
    }

    pfxBytes, err := pkcs12.Encode(rand.Reader, keyBytes, cert, []*x509.Certificate{}, pkcs12.DefaultPassword)

    if err != nil {
        panic(err)
    }

    // see if pfxBytes valid
    _, _, _, err = pkcs12.DecodeChain(pfxBytes, pkcs12.DefaultPassword)
    if err != nil {
        panic(err)
    }
    if err := ioutil.WriteFile(
        "/path/to/pri.pfx",
        pfxBytes,
        os.ModePerm,
    ); err != nil {
        panic(err)
    }
}

大家可以在我的github仓库yuchanns/gobyexample中找到源代码

本篇关于《使用 golang 如何生成 rsa 证书,然后将私钥导出到 pfx 文件,将公钥导出到 cer 文件》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>