Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用go库解析出来的私钥和openssl解析出来的私钥不一样 #204

Open
Feng-Duck opened this issue Aug 21, 2024 · 0 comments
Open

Comments

@Feng-Duck
Copy link

遇到过一个go库解析pfx文件得到的私钥并转化为PEM格式,和openssl解析同一个pfx文件产生的PEM格式的私钥里的公钥不一样,并且这个私钥用gmx509.ReadPrivateKeyFromPem(priKeyPem, nil)读取失败,go库解析示例如下:

`//解析pfx证书
_, priKey, err := gmpkcs12.SM2P12Decrypt(gmPfxFile, "123456")
if err != nil {
fmt.Println("decrypt gm pfx error", err)
return
}

priPem, err := gmx509.WritePrivateKeyToPem(priKey, nil)
if err != nil {
	fmt.Println("write sm2 pri key to pem error", err)
	return
}
fmt.Println("priPem:", string(priPem))

//将私钥转换成PEM格式字符串
priKeyBytes, err := gmpkcs12.MarshalECPrivateKey(priKey)
if err != nil {
	fmt.Println("marshal ec pri key error", err)
	return
}

priKeyPem := pem.EncodeToMemory(&pem.Block{
	Type:  "EC PRIVATE KET",
	Bytes: priKeyBytes,
})
log.Println("priKeyPem:", string(priKeyPem))

_, err = gmx509.ReadPrivateKeyFromPem(priKeyPem, nil)
if err != nil {
	fmt.Println("read error")
} else {
	fmt.Println("read success")
}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant