如何像 openssl 一样在 golang 中创建 .der 文件

How to create .der file in golang same as openssl do

"openssl rsa -in prevate.pem -outform DER -pubout -out public.der"

您可能会考虑像 google/trillian, which does include a MustMarshalPublicPEMToDER(keyPEM string) []byte 函数这样的库。

// MustMarshalPublicPEMToDER reads a PEM-encoded public key and returns it in DER encoding.
// If an error occurs, it panics.
func MustMarshalPublicPEMToDER(keyPEM string) []byte {
    block, _ := pem.Decode([]byte(keyPEM))
    key, err := x509.ParsePKIXPublicKey(block.Bytes)
    if err != nil {
        panic(err)
    }

    keyDER, err := x509.MarshalPKIXPublicKey(key)
    if err != nil {
        panic(err)
    }
    return keyDER
}

如该函数的注释所示,这会读取 PEM 编码的 public 密钥。

Topaco, you would need crypto/x509#ParsePKCS8PrivateKey 所述,以便读取私人 PKCS#8 PEM 编码密钥。
元帅部分不变。