38 lines
920 B
Go
38 lines
920 B
Go
package cryptoutil
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"encoding/base64"
|
|
"likedream-goframe/internal/framework/logger"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
const (
|
|
KEY = "u2oh6Vu^HWe4_AES"
|
|
)
|
|
|
|
func LoginEncrypt(str string) (string, error) {
|
|
key := []byte(KEY)
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
logger.Logger.Error("aes.NewCipher err:", zap.Error(err))
|
|
return "", err
|
|
}
|
|
paddedData := pad([]byte(str), block.BlockSize())
|
|
ciphertext := make([]byte, len(paddedData))
|
|
mode := cipher.NewCBCEncrypter(block, key)
|
|
mode.CryptBlocks(ciphertext, paddedData)
|
|
data := base64.StdEncoding.EncodeToString(ciphertext)
|
|
return data, nil
|
|
}
|
|
|
|
// pad 确保数据长度是块大小的整数倍,以便符合块加密算法的要求
|
|
func pad(src []byte, blockSize int) []byte {
|
|
padding := blockSize - len(src)%blockSize
|
|
padText := bytes.Repeat([]byte{byte(padding)}, padding)
|
|
return append(src, padText...)
|
|
}
|