后台,密钥对生成
This commit is contained in:
parent
ad56b5f0e5
commit
bf1bc4b1a2
|
@ -47,6 +47,8 @@ const (
|
||||||
AppSM4DecryptFail = 1231
|
AppSM4DecryptFail = 1231
|
||||||
AppSM4EncryptKeyNotFound = 1232
|
AppSM4EncryptKeyNotFound = 1232
|
||||||
AppSM4EncryptFail = 1233
|
AppSM4EncryptFail = 1233
|
||||||
|
// 加密方式不存在
|
||||||
|
EncryptTypeNotFound = 1240
|
||||||
|
|
||||||
//渠道
|
//渠道
|
||||||
PayChannelNotFound = 1300
|
PayChannelNotFound = 1300
|
||||||
|
@ -108,6 +110,8 @@ var MsgZH = map[int]string{
|
||||||
AppSM4EncryptKeyNotFound: "密匙缺失,无法进行sm4加密",
|
AppSM4EncryptKeyNotFound: "密匙缺失,无法进行sm4加密",
|
||||||
AppSM4EncryptFail: "sm4加密失败",
|
AppSM4EncryptFail: "sm4加密失败",
|
||||||
|
|
||||||
|
EncryptTypeNotFound: "加密方式不存在",
|
||||||
|
|
||||||
PayChannelNotFound: "支付方式不存在",
|
PayChannelNotFound: "支付方式不存在",
|
||||||
PayChannelNotBuild: "支付方式尚未开通",
|
PayChannelNotBuild: "支付方式尚未开通",
|
||||||
PayChannelExtJsonError: "支付方式扩展参数错误",
|
PayChannelExtJsonError: "支付方式扩展参数错误",
|
||||||
|
|
|
@ -7,7 +7,9 @@ import (
|
||||||
"PaymentCenter/app/http/entities/backend"
|
"PaymentCenter/app/http/entities/backend"
|
||||||
"PaymentCenter/app/models/appmodel"
|
"PaymentCenter/app/models/appmodel"
|
||||||
"PaymentCenter/app/services"
|
"PaymentCenter/app/services"
|
||||||
|
"PaymentCenter/app/utils/encrypt/rsa"
|
||||||
"PaymentCenter/app/utils/encrypt/sm2"
|
"PaymentCenter/app/utils/encrypt/sm2"
|
||||||
|
"PaymentCenter/app/utils/encrypt/sm4"
|
||||||
"github.com/ahmetb/go-linq/v3"
|
"github.com/ahmetb/go-linq/v3"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
@ -60,12 +62,19 @@ func GenerateDecrypt(c *gin.Context) {
|
||||||
var publicKey, privateKey string
|
var publicKey, privateKey string
|
||||||
var err error
|
var err error
|
||||||
switch req.KeyType {
|
switch req.KeyType {
|
||||||
default:
|
case "sm2":
|
||||||
publicKey, privateKey, err = sm2.GenerateSM2Key()
|
publicKey, privateKey, err = sm2.GenerateSM2Key()
|
||||||
if err != nil {
|
case "rsa":
|
||||||
controllers.Error(c, errorcode.SystemError, err.Error())
|
publicKey, privateKey, err = rsa.GenerateKey()
|
||||||
return
|
case "sm4":
|
||||||
}
|
privateKey, publicKey = sm4.GenerateKey()
|
||||||
|
default:
|
||||||
|
controllers.HandCodeRes(c, "", errorcode.EncryptTypeNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
controllers.Error(c, errorcode.SystemError, err.Error())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
controllers.HandCodeRes(c, map[string]string{
|
controllers.HandCodeRes(c, map[string]string{
|
||||||
|
|
|
@ -96,5 +96,5 @@ func (a *AppUpdateRequest) RequestToDb() (db appmodel.App) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type GenerateDecryptKeyRequest struct {
|
type GenerateDecryptKeyRequest struct {
|
||||||
KeyType int `json:"key_type" label:"密钥类型"`
|
KeyType string `json:"key_type" form:"key_type" label:"密钥类型"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,3 +108,33 @@ func Decrypt(privateKeyPEM string, encryptedDataBase64 string) ([]byte, error) {
|
||||||
|
|
||||||
return decrypted, nil
|
return decrypted, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 生成密钥对
|
||||||
|
func GenerateKey() (string, string, error) {
|
||||||
|
// 生成私钥
|
||||||
|
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
// 导出私钥PKCS#1格式
|
||||||
|
privKey := x509.MarshalPKCS1PrivateKey(privateKey)
|
||||||
|
// 将私钥转换为PEM编码
|
||||||
|
var privBlock = &pem.Block{
|
||||||
|
Type: "RSA PRIVATE KEY",
|
||||||
|
Bytes: privKey,
|
||||||
|
}
|
||||||
|
privPem := pem.EncodeToMemory(privBlock)
|
||||||
|
// 导出公钥
|
||||||
|
pubKey := &privateKey.PublicKey
|
||||||
|
derPkix, err := x509.MarshalPKIXPublicKey(pubKey)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
// 将公钥转换为PEM编码
|
||||||
|
var pubBlock = &pem.Block{
|
||||||
|
Type: "PUBLIC KEY",
|
||||||
|
Bytes: derPkix,
|
||||||
|
}
|
||||||
|
pubPem := pem.EncodeToMemory(pubBlock)
|
||||||
|
return string(pubPem), string(privPem), nil
|
||||||
|
}
|
||||||
|
|
|
@ -39,3 +39,17 @@ func encrypt() string {
|
||||||
|
|
||||||
return base64.StdEncoding.EncodeToString(en)
|
return base64.StdEncoding.EncodeToString(en)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 测试生成密钥对
|
||||||
|
func TestGenerateRSAKey(t *testing.T) {
|
||||||
|
pub, pri, err := GenerateKey()
|
||||||
|
data := "{\"pay_channel_id\":8935141660703064070,\"out_trade_no\":\"asdadasdas\",\"order_type\":1,\"amount\":1,\"desc\":\"abc\",\"ext_json\":\"\",\"app_id\":5476377146882523138,\"timestamp\":53612533412643}"
|
||||||
|
dataJson := []byte(data)
|
||||||
|
en, err := Encrypt(pub, dataJson)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
content := base64.StdEncoding.EncodeToString(en)
|
||||||
|
res, err := Decrypt(pri, content)
|
||||||
|
fmt.Println("解密", string(res), err)
|
||||||
|
}
|
||||||
|
|
|
@ -17,7 +17,20 @@ func TestGenerateSM2KeyPair(t *testing.T) {
|
||||||
|
|
||||||
// Print the private and public keys
|
// Print the private and public keys
|
||||||
fmt.Printf("Private Key: %s\n", privateKey)
|
fmt.Printf("Private Key: %s\n", privateKey)
|
||||||
fmt.Printf("Public Key: %s", publicKey)
|
fmt.Printf("Public Key: %s\n", publicKey)
|
||||||
|
|
||||||
|
data := "{\"name\":\"张三\",\"sex\":1,\"is_human\":true}"
|
||||||
|
en, err := SM2Encrypt(data, publicKey)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
decrypt, err := SM2Decrypt(en, publicKey, privateKey)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
t.Log(decrypt)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSM2Encrypt(t *testing.T) {
|
func TestSM2Encrypt(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue