使用 go-cryptobin 转换 RSA 的 PEM 证书编码格式
下载
go get -u github.com/deatil/go-cryptobin
引入
import (
"github.com/deatil/go-cryptobin/cryptobin/rsa"
)
私钥公钥证书编码格式转换
import (
"github.com/deatil/go-cryptobin/cryptobin/rsa"
)
func main() {
// 私钥编码转换
// PrivateKey change type
var priKeyPem []byte = []byte("")
var priKeyXML []byte = []byte("")
// 私钥密码
// privatekey password
var psssword string = ""
var newPrivateKey string = rsa.New().
// FromPrivateKey(priKeyPem).
// FromPrivateKeyWithPassword(priKeyPem, psssword).
// FromPKCS1PrivateKey(priKeyPem).
FromPKCS1PrivateKeyWithPassword(priKeyPem, psssword).
// FromPKCS8PrivateKey(priKeyPem).
// FromPKCS8PrivateKeyWithPassword(priKeyPem, psssword).
// FromXMLPrivateKey(priKeyXML).
// CreatePrivateKey().
// CreatePrivateKeyWithPassword(psssword, "AES256CBC").
// CreatePKCS1PrivateKey().
// CreatePKCS1PrivateKeyWithPassword(psssword, "AES256CBC").
CreatePKCS8PrivateKey(). // 转为 PKCS8 编码
// CreatePKCS8PrivateKeyWithPassword(psssword, "AES256CBC", "SHA256").
// CreateXMLPrivateKey().
ToKeyString()
// 公钥编码转换
// PublicKey change type
var pubKeyPem []byte = []byte("")
var pubKeyXML []byte = []byte("")
var newPublicKey string = rsa.New().
FromPublicKey(pubKeyPem).
// FromPKCS1PublicKey(pubKeyPem).
// FromPKCS8PublicKey(pubKeyPem).
// FromXMLPublicKey(pubKeyXML).
// CreatePKCS1PublicKey().
CreatePKCS8PublicKey(). // 转为 PKCS8 编码
// CreateXMLPublicKey().
ToKeyString()
}
更多文档信息可以查看 go-cryptobin docs
本作品采用《CC 协议》,转载必须注明作者和本文链接