Skip to content

Commit

Permalink
Merge branch 'fork'
Browse files Browse the repository at this point in the history
* fork:
  解决:解密之后的值  不等于 加密前的值
  fix 解决GenerateRSAKey 生成RSA私钥和公钥,无法解密问题
  • Loading branch information
go-sniper committed May 19, 2023
2 parents 1b2dea5 + aefd14e commit 94580e0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
17 changes: 9 additions & 8 deletions gorsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package gorsa

import (
"encoding/base64"
"encoding/hex"
)

// 公钥加密
// PublicEncrypt 公钥加密
func PublicEncrypt(data, publicKey string) (string, error) {

grsa := RSASecurity{}
Expand All @@ -19,7 +18,7 @@ func PublicEncrypt(data, publicKey string) (string, error) {
return base64.StdEncoding.EncodeToString(rsadata), nil
}

// 私钥加密
// PriKeyEncrypt 私钥加密
func PriKeyEncrypt(data, privateKey string) (string, error) {

grsa := RSASecurity{}
Expand All @@ -33,21 +32,22 @@ func PriKeyEncrypt(data, privateKey string) (string, error) {
return base64.StdEncoding.EncodeToString(rsadata), nil
}

// 公钥解密
// PublicDecrypt 公钥解密
func PublicDecrypt(data, publicKey string) (string, error) {

databs, _ := base64.StdEncoding.DecodeString(data)

grsa := RSASecurity{}
if err := grsa.SetPublicKey(publicKey);err !=nil{
return "",err
if err := grsa.SetPublicKey(publicKey); err != nil {
return "", err
}

rsadata, err := grsa.PubKeyDECRYPT(databs)
if err != nil {
return "", err
}
return hex.EncodeToString(rsadata),nil
//return hex.EncodeToString(rsadata),nil
return string(rsadata), nil
}

// 私钥解密
Expand All @@ -66,5 +66,6 @@ func PriKeyDecrypt(data, privateKey string) (string, error) {
return "", err
}

return hex.EncodeToString(rsadata), nil
//return hex.EncodeToString(rsadata), nil
return string(rsadata), nil
}
19 changes: 10 additions & 9 deletions rsa_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
)

type RSAKey struct {
Expand All @@ -19,7 +17,6 @@ type RSAKey struct {
// GenerateRSAKey 生成RSA私钥和公钥
// bits 证书大小
func GenerateRSAKey(bits int) (resp RSAKey, err error) {

// -------------------------- 设置私钥 --------------------------
// GenerateKey 函数使用随机数据生成器,random生成一对具有指定字位数的RSA密钥
// Reader 是一个全局、共享的密码用强随机数生成器
Expand All @@ -32,12 +29,14 @@ func GenerateRSAKey(bits int) (resp RSAKey, err error) {
X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey)
//使用pem格式对x509输出的内容进行编码
//构建一个pem.Block结构体对象
privateBlock := pem.Block{Type: "RSA Private Key", Bytes: X509PrivateKey}
privateBlock := pem.Block{
Type: "Private key",
Bytes: X509PrivateKey,
}
// 保存到内存
privateKeyPem := pem.EncodeToMemory(&privateBlock)
privateKeyStr := base64.StdEncoding.EncodeToString(privateKeyPem)
// 设置返回值:私钥
resp.PriStr = fmt.Sprintf("-----BEGIN Private key-----\n%v\n-----END Private key-----\n", privateKeyStr)
resp.PriStr = string(privateKeyPem)
resp.PriKey = privateKey

// -------------------------- 设置公钥 --------------------------
Expand All @@ -50,12 +49,14 @@ func GenerateRSAKey(bits int) (resp RSAKey, err error) {
}
//pem格式编码
//创建一个pem.Block结构体对象
publicBlock := pem.Block{Type: "RSA Public Key", Bytes: X509PublicKey}
publicBlock := pem.Block{
Type: "Public key",
Bytes: X509PublicKey,
}
//保存到内存
publicKeyPem := pem.EncodeToMemory(&publicBlock)
publicKeyStr := base64.StdEncoding.EncodeToString(publicKeyPem)
// 设置返回值:公钥
resp.PubStr = fmt.Sprintf("-----BEGIN Public key-----\n%v\n-----END Public key-----\n", publicKeyStr)
resp.PubStr = string(publicKeyPem)
resp.PubKey = &publicKey
return
}

0 comments on commit 94580e0

Please sign in to comment.