gmsm icon indicating copy to clipboard operation
gmsm copied to clipboard

缺失方法导入公钥然后使用公钥加密

Open jiftle opened this issue 9 months ago • 0 comments

附代码

func SM2_GenerateKeyPair() (publicKey string, privateKey string, err error) {
	privKey, err := sm2.GenerateKey(nil) // 生成密钥对
	if err != nil {
		return
	}
	privateKey = x509.WritePrivateKeyToHex(privKey)
	publicKey = x509.WritePublicKeyToHex(&privKey.PublicKey)
	return
}

func SM2_PublicKey_Encrypt(plain string, publicKey string) (cipher string, err error) {
	pub, err := x509.ReadPublicKeyFromHex(publicKey)
	if err != nil {
		return
	}
	bytPlan, err := hex.DecodeString(plain)
	if err != nil {
		return
	}
	d, err := sm2.Encrypt(pub, bytPlan, rand.Reader, sm2.C1C3C2)
	if err != nil {
		return
	}
	cipher = hex.EncodeToString(d)
	return
}

func SM2_PrivateKey_Decrypt(cipher string, privKey string) (plain string, err error) {
	// g.Log().Infof(context.TODO(), "cipher=%v, privKey=%v", cipher, privKey)
	priv, err := x509.ReadPrivateKeyFromHex(privKey)
	if err != nil {
		return
	}
	bytCipher, err := hex.DecodeString(cipher)
	if err != nil {
		return
	}
	d, err := sm2.Decrypt(priv, bytCipher, sm2.C1C3C2)
	if err != nil {
		return
	}
	plain = hex.EncodeToString(d)
	// g.Log().Infof(context.TODO(), "cipher=%v, privKey=%v, plain=%v", cipher, privKey, plain)
	return
}

jiftle avatar Sep 15 '23 11:09 jiftle