From 3c550761d66f5756a85fd62e6205a84bafe702cb Mon Sep 17 00:00:00 2001 From: jiangyong Date: Sun, 27 Aug 2023 15:58:37 +0800 Subject: [PATCH] decrypt --- worker/common/haha.go | 76 ++++++++++++++++++++++++++++++++++ worker/haha/haha_sync_order.go | 7 +++- 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 worker/common/haha.go diff --git a/worker/common/haha.go b/worker/common/haha.go new file mode 100644 index 0000000..f107fef --- /dev/null +++ b/worker/common/haha.go @@ -0,0 +1,76 @@ +package common + +import ( + "crypto/aes" + "crypto/cipher" + "encoding/base64" + "github.com/gogf/gf/v2/crypto/gmd5" + "strings" +) + +// 假设p()就是md5 +func p(data string) []byte { + return []byte(gmd5.MustEncryptString(data)) +} + +//func p(hexStr string) []byte { +// bytes, _ := hex.DecodeString(hexStr) +// return bytes +//} + +func Encrypt(plaintext string, keyPrefix string) (string, error) { + + // 生成key和iv + key := p(keyPrefix + "piaofan@123") + iv := p(keyPrefix + "piaofan@456")[:16] + + // plaintext to bytes + plaintextBytes := []byte(plaintext) + + // 创建AES加密器 + block, err := aes.NewCipher(key) + if err != nil { + return "", err + } + + // 初始化CBC模式 + stream := cipher.NewCBCEncrypter(block, iv) + + // 加密 + ciphertext := make([]byte, len(plaintextBytes)) + stream.CryptBlocks(ciphertext, plaintextBytes) + + // 返回base64编码的ciphertext + return base64.StdEncoding.EncodeToString(ciphertext), nil + +} + +func Decrypt(base64Ciphertext string, keyPrefix string) (string, error) { + // base64解码ciphertext + ciphertext, err := base64.StdEncoding.DecodeString(base64Ciphertext) + if err != nil { + return "", err + } + + // 生成key和iv + key := p(keyPrefix + "piaofan@123") + iv := p(keyPrefix + "piaofan@456")[:16] + + //g.Log().Infof(context.Background(), "key: %s", gconv.String(key)) + + // 创建AES解密器 + block, err := aes.NewCipher(key) + if err != nil { + return "", err + } + + // 初始化CBC模式 + stream := cipher.NewCBCDecrypter(block, iv) + + // 解密 + plaintext := make([]byte, len(ciphertext)) + stream.CryptBlocks(plaintext, ciphertext) + + return strings.TrimSpace(string(plaintext)), nil + +} diff --git a/worker/haha/haha_sync_order.go b/worker/haha/haha_sync_order.go index 79de0a6..cd3050a 100644 --- a/worker/haha/haha_sync_order.go +++ b/worker/haha/haha_sync_order.go @@ -67,7 +67,12 @@ func (s *SyncOrder) run() { return } - datas := cast.ToSlice(result["data"]) + cipherText := cast.ToSlice(result["data"]) + datas, err := common.Decrypt(cipherText, s.token) + if err != nil { + log.Errorf("common.Decrypt error :%s", err.Error()) + return + } for _, d := range datas { data := cast.ToStringMap(d)