mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-12-16 04:36:35 +08:00
!123 一些bug修复
* fix: 数据同步、数据迁移体验优化 * fix: des加密传输sql * fix: 修复达梦字段注释显示问题 * fix: mysql timestamp 字段类型导出ddl错误修复
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/des"
|
||||
"crypto/md5"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
"encoding/hex"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mayfly-go/pkg/cache"
|
||||
"mayfly-go/pkg/logx"
|
||||
"os"
|
||||
@@ -279,6 +281,68 @@ func AesDecryptBase64(data string, key []byte) ([]byte, error) {
|
||||
return AesDecrypt(dataByte, key)
|
||||
}
|
||||
|
||||
// DES加密函数
|
||||
func DesEncrypt(data, key []byte) (string, error) {
|
||||
block, err := des.NewTripleDESCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// 对数据进行填充
|
||||
data = pkcs7Padding(data, des.BlockSize)
|
||||
|
||||
// 创建一个初始化向量
|
||||
iv := make([]byte, des.BlockSize)
|
||||
if _, err := rand.Read(iv); err != nil {
|
||||
return "", err
|
||||
}
|
||||
// 创建加密器
|
||||
mode := cipher.NewCBCEncrypter(block, iv)
|
||||
|
||||
// 加密
|
||||
encrypted := make([]byte, len(data))
|
||||
mode.CryptBlocks(encrypted, data)
|
||||
|
||||
// 将IV和加密数据组合
|
||||
result := append(iv, encrypted...)
|
||||
|
||||
// 使用Base64编码
|
||||
return base64.StdEncoding.EncodeToString(result), nil
|
||||
}
|
||||
|
||||
func DesDecryptByToken(data string, token string) (string, error) {
|
||||
key := []byte(token[:24])
|
||||
return DesDecrypt(data, key)
|
||||
}
|
||||
|
||||
func DesDecrypt(data string, key []byte) (string, error) {
|
||||
// Base64解码
|
||||
ciphertext, err := base64.StdEncoding.DecodeString(data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
block, err := des.NewTripleDESCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 确保密文长度正确
|
||||
if len(ciphertext) < des.BlockSize {
|
||||
return "", fmt.Errorf("ciphertext too short")
|
||||
}
|
||||
|
||||
// 提取IV
|
||||
iv := ciphertext[:des.BlockSize]
|
||||
ciphertext = ciphertext[des.BlockSize:]
|
||||
// 创建解密器
|
||||
mode := cipher.NewCBCDecrypter(block, iv)
|
||||
// 解密仍然用已存在的切片接收结果,无需重新创建切片
|
||||
mode.CryptBlocks(ciphertext, ciphertext)
|
||||
// 去除填充
|
||||
res, err := pkcs7UnPadding(ciphertext)
|
||||
return string(res), err
|
||||
}
|
||||
|
||||
// pkcs7Padding 填充
|
||||
func pkcs7Padding(data []byte, blockSize int) []byte {
|
||||
//判断缺少几位长度。最少1,最多 blockSize
|
||||
|
||||
Reference in New Issue
Block a user