解决无法获取用户绑定信息

This commit is contained in:
2025-06-16 16:50:10 +08:00
parent 2903be6223
commit 2bbe45f40c
7 changed files with 85 additions and 106 deletions

View File

@ -1,58 +1,31 @@
package encrypt
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"errors"
)
// AesEncrypt 使用 AES-CBC 模式对数据进行加密。
//
// 参数:
// - plainText: 原始明文数据(必须是任意长度)
// - key: 加密密钥(长度必须是 16、24 或 32 字节)
// - iv: 初始化向量(必须是 16 字节)
//
// 返回值:
// - 加密后的密文
// - 错误信息(如果加密失败)
func AesEncrypt(plainText, key, iv []byte) ([]byte, error) {
// PKCS5 填充
func pkcs5Padding(src []byte, blockSize int) []byte {
padding := blockSize - len(src)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padText...)
}
func AesEncryptCBCPKCS5(plainText, key, iv []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if len(iv) != aes.BlockSize {
return nil, errors.New("IV 长度必须为 16 字节")
}
plainText = pkcs7Padding(plainText, aes.BlockSize)
cipherText := make([]byte, len(plainText))
// PKCS5 填充(与 PKCS7 相同,只是 block size 为 8
paddedText := pkcs5Padding(plainText, block.BlockSize())
cipherText := make([]byte, len(paddedText))
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(cipherText, plainText)
mode.CryptBlocks(cipherText, paddedText)
return cipherText, nil
}
// pkcs7Padding 对数据进行 PKCS7 填充。
//
// 参数:
// - data: 原始数据
// - blockSize: 块大小(通常为 16
//
// 返回值:
// - 填充后的数据
func pkcs7Padding(data []byte, blockSize int) []byte {
padding := blockSize - len(data)%blockSize
padText := bytesRepeat(byte(padding), padding)
return append(data, padText...)
}
// bytesRepeat 返回一个重复 count 次的字节切片。
func bytesRepeat(b byte, count int) []byte {
buf := make([]byte, count)
for i := 0; i < count; i++ {
buf[i] = b
}
return buf
}