日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

密码技术--椭圆曲线算法EDCSA数字签名及Go语言应用

發布時間:2025/3/21 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 密码技术--椭圆曲线算法EDCSA数字签名及Go语言应用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.生成秘鑰對并寫入磁盤文件

1.使用ecdsa生成秘鑰對

2.將私鑰寫入磁盤

  • 使用x509進行反序列化
  • 將得到的切片字符串放到pem.Block結構體中
  • 使用pem編碼

3.將公鑰寫入磁盤

  • 從私鑰中得到公鑰
  • 使用x509進行序列化
  • 將得到的切片字符串放入pem.Block結構體中
  • 使用pem編碼

2.使用私鑰進行數字簽名

1.打開私鑰文件,將內容讀出來

2.使用pem進行數據解碼

3.使用x509對數據還原

4.對原始數據進行哈希運算

5.進行數字簽名
func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err error)
6.返回值為指針,因此需要將該地址指向內存中的數據進行序列話

3.使用公鑰驗證數字簽名

1.打開公鑰文件,讀出數據

2.使用pem進行解碼

3.使用x509進行公鑰數據還原
func ParsePKIXPublicKey(derBytes []byte) (pub interface{}, err error)

4.由于上一步返回的是一個接口類型,因此需要進行類型斷言,將接口類型轉換為公鑰

5.對原始數據進行哈希運算

6.驗簽

4.go中應用

package mainimport ("crypto/ecdsa""crypto/elliptic""crypto/rand""crypto/x509""encoding/pem""os""crypto/sha256""math/big" )func GenerateEcdsaKey () {//1.使用ecdsa生成秘鑰對privateKey, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)if err != nil {panic(err)}//2.將私鑰寫入磁盤//* 使用x509進行反序列化ecPrivateKey, err := x509.MarshalECPrivateKey(privateKey)if err != nil {panic(err)}//* 將得到的切片字符串放到pem.Block結構體中block := pem.Block{Type: "ecdsa private key",Headers: nil,Bytes: ecPrivateKey,}//* 使用pem編碼file, err := os.Create("ecPrivate.pem")if err != nil {panic(err)}defer file.Close()err = pem.Encode(file, &block)if err != nil {panic(err)}//3.將公鑰寫入磁盤//* 從私鑰中得到公鑰publicKey := privateKey.PublicKey//* 使用x509進行序列化ecPublicKey, err := x509.MarshalPKIXPublicKey(&publicKey)if err != nil {panic(err)}//* 將得到的切片字符串放入pem.Block結構體中block = pem.Block{Type: "ecdsa public key",Headers: nil,Bytes: ecPublicKey,}//* 使用pem編碼file, err = os.Create("ecPublic.pem")if err != nil {panic(err)}defer file.Close()pem.Encode(file, &block) }//簽名 func SignECDSA (plainText []byte, priFileName string) (rText, sText []byte) {//1.打開私鑰文件,將內容讀出來file, err := os.Open(priFileName)if err != nil {panic(err)}defer file.Close()fileInfo, err := file.Stat()if err != nil {panic(err)}buf := make([]byte, fileInfo.Size())_, err = file.Read(buf)if err != nil {panic(err)}//2.使用pem進行數據解碼block, _ := pem.Decode(buf)//3.使用x509對數據還原privateKey, err := x509.ParseECPrivateKey(block.Bytes)if err != nil {panic(err)}//4.對原始數據進行哈希運算hashText := sha256.Sum256(plainText)//5.進行數字簽名var r, s *big.Int //注意這里r, s, err = ecdsa.Sign(rand.Reader, privateKey, hashText[:])if err != nil {panic(err)}//6.返回值為指針,因此需要將該地址指向內存中的數據進行序列話rText, err = r.MarshalText()if err != nil {panic(err)}sText, err = s.MarshalText()if err != nil {panic(err)}return rText,sText }//驗簽 func VerifyECDSA (plainText, rText, sText []byte, pubFileName string) bool {//1.打開公鑰文件,讀出數據file, err := os.Open(pubFileName)if err != nil {panic(err)}defer file.Close()fileInfo, err := file.Stat()if err != nil {panic(err)}buf := make([]byte, fileInfo.Size())_, err = file.Read(buf)if err != nil {panic(err)}//2.使用pem進行解碼block, _ := pem.Decode(buf)//3.使用x509進行公鑰數據還原pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)if err != nil {panic(err)}//4.由于上一步返回的是一個接口類型,因此需要進行類型斷言,將接口類型轉換為公鑰publicKey := pubInterface.(*ecdsa.PublicKey)//5.對原始數據進行哈希運算hashText := sha256.Sum256(plainText)//6.驗簽var r, s big.Intr.UnmarshalText(rText)s.UnmarshalText(sText)res := ecdsa.Verify(publicKey, hashText[:], &r, &s)return res }

總結

以上是生活随笔為你收集整理的密码技术--椭圆曲线算法EDCSA数字签名及Go语言应用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。