From 4b6e5839ba1c223207fd6cd324b1b29fd9036056 Mon Sep 17 00:00:00 2001 From: hahwu <31872165+hahwu@users.noreply.github.com> Date: Thu, 2 Jan 2025 16:32:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=96=E6=B6=88=E6=98=8E=E6=96=87=E5=AF=86?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/GoUtil/GoUtil.go | 53 +++++++++++++++++++++++++++++++++++++ src/server/conf/server.json | 2 +- src/server/db/Mysql.go | 3 ++- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/server/GoUtil/GoUtil.go b/src/server/GoUtil/GoUtil.go index 2b5d4536..dd4fc083 100644 --- a/src/server/GoUtil/GoUtil.go +++ b/src/server/GoUtil/GoUtil.go @@ -2,8 +2,13 @@ package GoUtil import ( "bytes" + "crypto/aes" + "crypto/cipher" + crand "crypto/rand" + "encoding/base64" "encoding/gob" "fmt" + "io" "math/rand" "reflect" "strconv" @@ -19,6 +24,54 @@ type EventObj struct { Obj interface{} } +const ( + SECRET_KEY = ")VQbB(vpy=U(wcp)" +) + +// 加密字符串 +func Encrypt(plainText string) (string, error) { + block, err := aes.NewCipher([]byte(SECRET_KEY)) + if err != nil { + return "", err + } + + cipherText := make([]byte, aes.BlockSize+len(plainText)) + iv := cipherText[:aes.BlockSize] + if _, err := io.ReadFull(crand.Reader, iv); err != nil { + return "", err + } + + stream := cipher.NewCFBEncrypter(block, iv) + stream.XORKeyStream(cipherText[aes.BlockSize:], []byte(plainText)) + + return base64.URLEncoding.EncodeToString(cipherText), nil +} + +// 解密字符串 +func Decrypt(cipherText string) (string, error) { + cipherTextBytes, err := base64.URLEncoding.DecodeString(cipherText) + if err != nil { + return "", err + } + + block, err := aes.NewCipher([]byte(SECRET_KEY)) + if err != nil { + return "", err + } + + if len(cipherTextBytes) < aes.BlockSize { + return "", fmt.Errorf("cipherText too short") + } + + iv := cipherTextBytes[:aes.BlockSize] + cipherTextBytes = cipherTextBytes[aes.BlockSize:] + + stream := cipher.NewCFBDecrypter(block, iv) + stream.XORKeyStream(cipherTextBytes, cipherTextBytes) + + return string(cipherTextBytes), nil +} + func (o *EventObj) isEqual(tar *EventObj) bool { if reflect.ValueOf(o.Callback).Pointer() == reflect.ValueOf(tar.Callback).Pointer() && o.Obj == tar.Obj { return true diff --git a/src/server/conf/server.json b/src/server/conf/server.json index a1a6281a..21d7fca4 100644 --- a/src/server/conf/server.json +++ b/src/server/conf/server.json @@ -7,7 +7,7 @@ "MySqlAddr": "127.0.0.1", "MySqlPort": "3306", "MySqlUsr": "root", - "MySqlPwd": "root", + "MySqlPwd": "IOagNEq3C84c-20CmHEin5iODVc=", "MaxConnNum": 20000, "DbName": "Merge_Pet", "HttpPort": ":8081", diff --git a/src/server/db/Mysql.go b/src/server/db/Mysql.go index 433e0018..179aeb6d 100644 --- a/src/server/db/Mysql.go +++ b/src/server/db/Mysql.go @@ -25,7 +25,8 @@ var SqlDb *sqlx.DB func InitDB() { //"用户名:密码@[连接方式](主机名:端口号)/数据库名" - connect := fmt.Sprintf("%s:%s@(%s:%s)/%s", conf.Server.MySqlUsr, conf.Server.MySqlPwd, conf.Server.MySqlAddr, conf.Server.MySqlPort, conf.Server.DbName) + MysqlPwd, _ := GoUtil.Decrypt(conf.Server.MySqlPwd) + connect := fmt.Sprintf("%s:%s@(%s:%s)/%s", conf.Server.MySqlUsr, MysqlPwd, conf.Server.MySqlAddr, conf.Server.MySqlPort, conf.Server.DbName) SqlDb = sqlx.MustConnect("mysql", connect) // 设置连接数据库的参数 SqlDb.SetMaxOpenConns(20) // 设置最大打开的连接数 log.Debug("connect mysql success")