慕桂英546537
有趣的是,没有标准功能可以做到这一点,但这里有一个自定义解决方案:type pkcs8Key struct { Version int PrivateKeyAlgorithm []asn1.ObjectIdentifier PrivateKey []byte}func rsa2pkcs8(key *rsa.PrivateKey) ([]byte, error) { var pkey pkcs8Key pkey.Version = 0 pkey.PrivateKeyAlgorithm = make([]asn1.ObjectIdentifier, 1) pkey.PrivateKeyAlgorithm[0] = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1} pkey.PrivateKey = x509.MarshalPKCS1PrivateKey(key) return asn1.Marshal(pkey)}