当前位置:   article > 正文

基于Go调用国密SM2算法_golang sm2

golang sm2
// 一下为tool.go,辅助函数
/*
	保存系统参数
 */
func saveOneValue(filename string,save *big.Int)  {
	outputFile, outputError := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)//os.O_APPEND追加
	if outputError != nil {
		fmt.Println(outputError)
		return
	}
	defer outputFile.Close()
	outputWriter := bufio.NewWriter(outputFile)
	outputWriter.WriteString(save.String()+"\n")
	// 一定得记得将缓冲区内容刷新到磁盘文件
	outputWriter.Flush()
}

/*
	保存各个成员的参数情况——用于测试
 */
func saveValue(filename string,save []*big.Int)  {
	//写文件 保存p值
	k := len(save)

	outputFile, outputError := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)//os.O_APPEND追加
	if outputError != nil {
		fmt.Println(outputError)
		return
	}
	defer outputFile.Close()
	outputWriter := bufio.NewWriter(outputFile)
	for i := 0; i < k; i++ {
		outputWriter.WriteString(save[i].String()+"\n")
	}
	// 一定得记得将缓冲区内容刷新到磁盘文件
	outputWriter.Flush()
}

/*
	读取各个成员的参数情况——用于测试
 */
func ReadValue(filename string) (re []*big.Int) {
	temp := new(big.Int)

	inputFile, inputError := os.Open(filename)
	if inputError != nil {
		fmt.Println(inputError)
		return nil
	}
	defer inputFile.Close()
	inputReader := bufio.NewReader(inputFile)
	for {
		inputString, readerError := inputReader.ReadString('\n')
		temp.SetString(inputString,10)
		re = append(re,new(big.Int).Set(temp))
		if readerError == io.EOF {
			break
		}
	}
	return re
}

/*
	读取系统参数情况
 */
func ReadOneValue(filename string) (*big.Int) {
	re := new(big.Int)
	inputFile, inputError := os.Open(filename)
	if inputError != nil {
		fmt.Println(inputError)
		return nil
	}
	defer inputFile.Close()
	inputReader := bufio.NewReader(inputFile)
	for {
		inputString, readerError := inputReader.ReadString('\n')
		re.SetString(inputString,10)

		if readerError == io.EOF {
			break
		}
	}
	return re
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84

以下代码

package tool

import (
	"crypto/rand"
	"github.com/pkg/errors"
	"github.com/tjfoc/gmsm/sm2"
	"log"
	"math/big"
)

/*
	用于生成sm2密钥对
	密钥对将会存储在sm2Value
 */
func GetSm2Keys()  {
	priv, err := sm2.GenerateKey(rand.Reader) // 生成密钥对
	if err != nil {
		log.Fatal(err)
	}
	pub := &priv.PublicKey

	var values []*big.Int
	values = append(values, priv.D,pub.X,pub.Y)
	saveValue("sm2Value.txt",values)
}

/*
	使用对方的公钥加密
	content是需要加密的内容
 */
func CreateSm2Encrypt(msg []byte) ([]byte,*sm2.PublicKey,error) {
	//读取密钥对

	re := ReadValue("sm2Value.txt")

	if len(re)<3 {
		return nil,nil,errors.New("文件内容损坏!")
	}

	c := sm2.P256Sm2()
	priv := new(sm2.PrivateKey)
	priv.PublicKey.Curve = c
	priv.D = new(big.Int).Set(re[0])
	priv.PublicKey.X, priv.PublicKey.Y = new(big.Int).Set(re[1]),new(big.Int).Set(re[2])

	pub := &priv.PublicKey
	ciphertxt, err := pub.EncryptAsn1(msg,rand.Reader) //sm2加密
	if err != nil {
		return nil, nil, err
	}

	return ciphertxt,pub,nil
}

/*
	私钥进行解密操作
	需要使用匹配的私钥进行解密
*/
func Sm2Decrypt(ciphertxt []byte) (string,error) {
	//读取密钥对
	re := ReadValue("sm2Value.txt")

	if len(re)<3 {
		return "", errors.New("文件内容损坏!")
	}

	c := sm2.P256Sm2()
	priv := new(sm2.PrivateKey)
	priv.PublicKey.Curve = c
	priv.D = new(big.Int).Set(re[0])
	priv.PublicKey.X, priv.PublicKey.Y = new(big.Int).Set(re[1]),new(big.Int).Set(re[2])

	plaintxt,err :=  priv.DecryptAsn1(ciphertxt)  //sm2解密
	if err != nil {
		return "", err
	}

	return string(plaintxt),nil
}

/*
	使用私钥创建签名
 */
func CreateSm2Sig(msg []byte) ([]byte,*sm2.PublicKey,error) {
	//读取密钥对
	re := ReadValue("sm2Value.txt")

	if len(re)<3 {
		return nil,nil, errors.New("文件内容损坏!")
	}

	c := sm2.P256Sm2()
	priv := new(sm2.PrivateKey)
	priv.PublicKey.Curve = c
	priv.D = new(big.Int).Set(re[0])
	priv.PublicKey.X, priv.PublicKey.Y = new(big.Int).Set(re[1]),new(big.Int).Set(re[2])

	sign,err := priv.Sign(rand.Reader, msg, nil)  //sm2签名
	if err != nil {
		return nil,nil, err
	}
	return sign,&priv.PublicKey,err
}

/*
	验证签名是否正常
 */
func VerSm2Sig(pub *sm2.PublicKey,msg []byte,sign []byte) bool {
	isok := pub.Verify(msg, sign)
	if !isok {
		return false
	}
	return true
}



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/483324
推荐阅读
相关标签
  

闽ICP备14008679号