refactor(utils): 移除AES CBC解密函数并优化token加密逻辑
- 删除了AesCBCDecrypt函数,不再支持AES/CBC/PKCS7Padding解密方式- 在token.go中引入hex包用于密钥和IV的解码 - 使用hex.DecodeString对secretCfg中的Key和IV进行解码 - 添加日志记录key和iv解码失败的情况 - 加密失败时增加错误日志输出 - 保持原有加密功能不变,仅调整实现细节和依赖处理
This commit is contained in:
@@ -5,7 +5,9 @@ import (
|
||||
"boss/internal/utils"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"github.com/beego/beego/v2/core/logs"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -28,8 +30,19 @@ func (t *TokenService) GetToken(ctx context.Context, userId string) (token strin
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
tokenB, err := utils.AesCBCEncrypt(tokenBytes, []byte(secretCfg.Key), []byte(secretCfg.IV))
|
||||
key, err := hex.DecodeString(secretCfg.Key)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return ""
|
||||
}
|
||||
iv, err := hex.DecodeString(secretCfg.IV)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return ""
|
||||
}
|
||||
tokenB, err := utils.AesCBCEncrypt(tokenBytes, key, iv)
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
return ""
|
||||
}
|
||||
return base64.URLEncoding.EncodeToString(tokenB)
|
||||
|
||||
@@ -63,27 +63,6 @@ func AesCBCEncrypt(plaintext []byte, key []byte, iv []byte) ([]byte, error) {
|
||||
return plaintext, nil
|
||||
}
|
||||
|
||||
// AesCBCDecrypt AES/CBC/PKCS7Padding 解密
|
||||
func AesCBCDecrypt(ciphertext []byte, key []byte, iv []byte) ([]byte, error) {
|
||||
// AES
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if len(ciphertext)%aes.BlockSize != 0 {
|
||||
panic("ciphertext is not a multiple of the block size")
|
||||
}
|
||||
|
||||
// CBC 解密
|
||||
mode := cipher.NewCBCDecrypter(block, iv)
|
||||
mode.CryptBlocks(ciphertext, ciphertext)
|
||||
|
||||
// PKCS7 反填充
|
||||
result := unPaddingPKCS7(ciphertext)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// PKCS7 填充
|
||||
func paddingPKCS7(plaintext []byte, blockSize int) []byte {
|
||||
paddingSize := blockSize - len(plaintext)%blockSize
|
||||
|
||||
Reference in New Issue
Block a user