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

提供几个方法 解析密钥,导出Hex 等格式,对标java中org.bouncycastle.crypto #207

Open
jixiuf opened this issue Sep 11, 2024 · 2 comments

Comments

@jixiuf
Copy link

jixiuf commented Sep 11, 2024


// 国密sm2  非对称加密算法 (对标rsa使用场景)
//
//	ParsePublicKey 公钥字符串还原为 sm2.PublicKey 对象(与java中org.bouncycastle.crypto生成的公私钥完全互通使用)
func ParsePublicKey(publicKeyStr string) (*sm2.PublicKey, error) {
	publicKeyBytes, err := hex.DecodeString(publicKeyStr)
	if err != nil {
		return nil, err
	}
	// 提取 x 和 y 坐标字节切片
	curve := sm2.P256Sm2().Params()
	byteLen := (curve.BitSize + 7) / 8
	xBytes := publicKeyBytes[1 : byteLen+1]
	yBytes := publicKeyBytes[byteLen+1 : 2*byteLen+1]
	// 将字节切片转换为大整数
	x := new(big.Int).SetBytes(xBytes)
	y := new(big.Int).SetBytes(yBytes)
	// 创建 sm2.PublicKey 对象
	publicKey := &sm2.PublicKey{
		Curve: curve,
		X:     x,
		Y:     y,
	}
	return publicKey, nil
}

// 国密 非对称加密算法
//
//	ParsePublicKey 公钥字符串还原为 sm2.PublicKey 对象(与java中org.bouncycastle.crypto生成的公私钥完全互通使用)
func ParsePublicKeyByXY(xHex, yHex string) (*sm2.PublicKey, error) {
	xBytes, err := hex.DecodeString(xHex)
	if err != nil {
		return nil, err
	}
	yBytes, err := hex.DecodeString(yHex)
	if err != nil {
		return nil, err
	}
	// 提取 x 和 y 坐标字节切片
	curve := sm2.P256Sm2().Params()
	// byteLen := (curve.BitSize + 7) / 8
	// 将字节切片转换为大整数
	x := new(big.Int).SetBytes(xBytes)
	y := new(big.Int).SetBytes(yBytes)
	// 创建 sm2.PublicKey 对象
	publicKey := &sm2.PublicKey{
		Curve: curve,
		X:     x,
		Y:     y,
	}
	return publicKey, nil
}

func PublicKeyToHex(pk *sm2.PublicKey) (pubKeyString string) {
	bs := append([]byte{byte(4)}, pk.X.Bytes()...)
	bs = append(bs, pk.Y.Bytes()...)

	return hex.EncodeToString(bs)
}
func PublicKeyToXYHex(pk *sm2.PublicKey) (x, y string) {
	x = hex.EncodeToString(pk.X.Bytes())
	y = hex.EncodeToString(pk.Y.Bytes())
	return x, y
}

// 将私钥字符串反序列化转为私钥对象:
// ParsePrivateKey 私钥还原为 sm2.PrivateKey对象(与java中org.bouncycastle.crypto生成的公私钥完全互通使用)
func ParsePrivateKey(privateKeyStr string, publicKey *sm2.PublicKey) (*sm2.PrivateKey, error) {
	privateKeyBytes, err := hex.DecodeString(privateKeyStr)
	if err != nil {
		return nil, err
	}
	// 将字节切片转换为大整数
	d := new(big.Int).SetBytes(privateKeyBytes)
	// 创建 sm2.PrivateKey 对象
	privateKey := &sm2.PrivateKey{
		PublicKey: *publicKey,
		D:         d,
	}
	return privateKey, nil
}
func PrivateKeyToHex(pk *sm2.PrivateKey) (d string) {
	d = hex.EncodeToString(pk.D.Bytes())
	return d
}

@DeyiXu
Copy link

DeyiXu commented Dec 24, 2024

mark‌

@DeyiXu
Copy link

DeyiXu commented Dec 24, 2024

使用github.com/tjfoc/gmsm/x509下面的方法可满足

	publicKeyString := ""
	privateKeyString := ""
	publicKey, err := x509.ReadPublicKeyFromHex(publicKeyString)
	if err != nil {
		panic(err)
	}
	privateKey, err := x509.ReadPrivateKeyFromHex(privateKeyString)
	if err != nil {
		panic(err)
	}
	publicKeyHex, err := x509.WritePublicKeyToHex(publicKey)
	if err != nil {
		panic(err)
	}
	privateKeyHex, err := x509.WritePrivateKeyToHex(privateKey)
	if err != nil {
		panic(err)
	}

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

2 participants