本帖最后由 mz135135 于 2020-7-20 17:55 编辑
你的需求
[Asm] 纯文本查看 复制代码 package main
import (
"github.com/tjfoc/gmsm/sm2"
"fmt"
"encoding/hex"
)
func main() {
//生成私钥
privateKey, e := sm2.GenerateKey()
if e!=nil{
fmt.Println("sm2 encrypt faild!")
}
//从私钥中获取公钥
pubkey := &privateKey.PublicKey
msg:= []byte("i am wek && i am The_Reader too 。")
//用公钥加密msg
bytes, i := pubkey.Encrypt(msg)
if i !=nil{
fmt.Println("使用私钥加密失败!")
}
fmt.Println("the encrypt msg = ",hex.EncodeToString(bytes))
//用私钥解密msg
decrypt, i2 := privateKey.Decrypt(bytes)
if i2 != nil{
fmt.Println("使用私钥解密失败!")
}
fmt.Println( "the msg = ", string(decrypt))
} |