赞
踩
func Dome() { //需要加密的参数 dataArray := []interface{}{"object1", "object2"} //序列化为JSON字符串 dataBytes, _ := json.Marshal(dataArray) // 假设这是你的SM2公钥的十六进制字符串表示 publicKeyHex := "" //DecodeString返回由十六进制字符串s表示的字节 keypoints, _ := hex.DecodeString(publicKeyHex) //检查是否有效并返回PublicKey testkey, err := sm2.NewPublicKey(keypoints) if err != nil { fmt.Printf("fail to new public key %v \n", err) } //两种加密方式 //ciphertext, err := sm2.EncryptASN1(rand.Reader, testkey, dataBytes) ciphertext, err := sm2.Encrypt(rand.Reader, testkey, dataBytes, nil) if err != nil { fmt.Fprintf(os.Stderr, "Error from encryption: %s\n", err) return } fmt.Printf("Ciphertext: %x \n", ciphertext) }
func Dome() { //私钥 privateKey := `` //解密 ciphertext, _ := hex.DecodeString(`加密出来字符串`) privKey, _ := hex.DecodeString(privateKey) testkey, err := sm2.NewPrivateKey(privKey) if err != nil { fmt.Printf("fail to new private key %v \n", err) } plaintext, err := testkey.Decrypt(nil, ciphertext, nil) if err != nil { fmt.Fprintf(os.Stderr, "Error1 from decryption: %s \n", err) return } fmt.Printf("Plaintext: %s \n", string(plaintext)) }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。