This commit is contained in:
2025-04-09 17:27:40 +08:00
parent f6a38a1246
commit 07b33ec35d
23 changed files with 1239 additions and 175 deletions

235
pkg/lzkit/crypto/README.md Normal file
View File

@@ -0,0 +1,235 @@
# AES 加密工具包
本包提供了多种加密方式,特别是用于处理敏感个人信息(如手机号、身份证号等)的加密和解密功能。
## 主要功能
- **AES-CBC 模式加密/解密** - 标准加密模式,适用于一般数据加密
- **AES-ECB 模式加密/解密** - 确定性加密模式,适用于数据库字段加密和查询
- **专门针对个人敏感信息的加密/解密方法**
- **密钥生成和管理工具**
## 安全性说明
- **AES-CBC 模式**:使用随机 IV相同明文每次加密结果不同安全性较高
- **AES-ECB 模式**:确定性加密,相同明文每次加密结果相同,便于数据库查询,但安全性较低
> **⚠️ 警告**ECB 模式仅适用于短文本(如手机号、身份证号)的确定性加密,不建议用于加密大段文本或高安全需求场景。
## 使用示例
### 1. 加密手机号
使用 AES-ECB 模式加密手机号,保证确定性(相同手机号总是产生相同密文)
```go
import (
"fmt"
"tydata-server/pkg/lzkit/crypto"
)
func encryptMobileExample() {
// 您的密钥(需安全保存,建议存储在配置中)
key := []byte("1234567890abcdef") // 16字节AES-128密钥
// 加密手机号
mobile := "13800138000"
encryptedMobile, err := crypto.EncryptMobile(mobile, key)
if err != nil {
panic(err)
}
fmt.Println("加密后的手机号:", encryptedMobile)
// 解密手机号
decryptedMobile, err := crypto.DecryptMobile(encryptedMobile, key)
if err != nil {
panic(err)
}
fmt.Println("解密后的手机号:", decryptedMobile)
}
```
### 2. 在数据库中存储和查询加密手机号
```go
// 加密并存储手机号
func saveUser(db *sqlx.DB, mobile string, key []byte) (int64, error) {
encryptedMobile, err := crypto.EncryptMobile(mobile, key)
if err != nil {
return 0, err
}
var id int64
err = db.QueryRow(
"INSERT INTO users (mobile, create_time) VALUES (?, NOW()) RETURNING id",
encryptedMobile,
).Scan(&id)
return id, err
}
// 根据手机号查询用户
func findUserByMobile(db *sqlx.DB, mobile string, key []byte) (*User, error) {
encryptedMobile, err := crypto.EncryptMobile(mobile, key)
if err != nil {
return nil, err
}
var user User
err = db.QueryRow(
"SELECT id, mobile, create_time FROM users WHERE mobile = ?",
encryptedMobile,
).Scan(&user.ID, &user.EncryptedMobile, &user.CreateTime)
if err != nil {
return nil, err
}
// 解密手机号用于显示
user.Mobile, _ = crypto.DecryptMobile(user.EncryptedMobile, key)
return &user, nil
}
```
### 3. 加密身份证号
```go
func encryptIDCardExample() {
key := []byte("1234567890abcdef")
idCard := "440101199001011234"
encryptedIDCard, err := crypto.EncryptIDCard(idCard, key)
if err != nil {
panic(err)
}
fmt.Println("加密后的身份证号:", encryptedIDCard)
// 解密身份证号
decryptedIDCard, err := crypto.DecryptIDCard(encryptedIDCard, key)
if err != nil {
panic(err)
}
fmt.Println("解密后的身份证号:", decryptedIDCard)
}
```
### 4. 密钥管理
```go
func keyManagementExample() {
// 生成随机密钥
key, err := crypto.GenerateAESKey(16) // AES-128
if err != nil {
panic(err)
}
fmt.Printf("生成的密钥(十六进制): %x\n", key)
// 从密码派生密钥(便于记忆)
password := "my-secure-password"
derivedKey, err := crypto.DeriveKeyFromPassword(password, 16)
if err != nil {
panic(err)
}
fmt.Printf("从密码派生的密钥: %x\n", derivedKey)
}
```
### 5. 使用十六进制输出(适用于 URL 参数)
```go
func hexEncodingExample() {
key := []byte("1234567890abcdef")
mobile := "13800138000"
// 使用十六进制编码(适合URL参数)
encryptedHex, err := crypto.EncryptMobileHex(mobile, key)
if err != nil {
panic(err)
}
fmt.Println("十六进制编码的加密手机号:", encryptedHex)
// 解密十六进制编码的手机号
decryptedMobile, err := crypto.DecryptMobileHex(encryptedHex, key)
if err != nil {
panic(err)
}
fmt.Println("解密后的手机号:", decryptedMobile)
}
```
## 在 Go-Zero 项目中使用
在 Go-Zero 项目中,建议将加密密钥放在配置文件中:
1. 在配置文件中添加密钥配置:
```yaml
# etc/main.yaml
Name: user-api
Host: 0.0.0.0
Port: 8888
Encrypt:
MobileKey: "1234567890abcdef" # 16字节AES-128密钥
IDCardKey: "1234567890abcdef1234567890abcdef" # 32字节AES-256密钥
```
2. 在配置结构中定义:
```go
type Config struct {
rest.RestConf
Encrypt struct {
MobileKey string
IDCardKey string
}
}
```
3. 在服务上下文中使用:
```go
type ServiceContext struct {
Config config.Config
UserModel model.UserModel
MobileKey []byte
IDCardKey []byte
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
UserModel: model.NewUserModel(sqlx.NewMysql(c.DB.DataSource), c.Cache),
MobileKey: []byte(c.Encrypt.MobileKey),
IDCardKey: []byte(c.Encrypt.IDCardKey),
}
}
```
4. 在 Logic 中使用:
```go
func (l *RegisterLogic) Register(req *types.RegisterReq) (*types.RegisterResp, error) {
// 加密手机号用于存储
encryptedMobile, err := crypto.EncryptMobile(req.Mobile, l.svcCtx.MobileKey)
if err != nil {
return nil, errors.New("手机号加密失败")
}
// 保存到数据库
user := &model.User{
Mobile: encryptedMobile,
// 其他字段...
}
result, err := l.svcCtx.UserModel.Insert(l.ctx, nil, user)
// 其余逻辑...
}
```

View File

@@ -0,0 +1,67 @@
package crypto
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"errors"
"io"
)
// AES CBC模式加密Base64传入传出
func AesEncryptURL(plainText, key []byte) (string, error) {
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
blockSize := block.BlockSize()
plainText = PKCS7Padding(plainText, blockSize)
cipherText := make([]byte, blockSize+len(plainText))
iv := cipherText[:blockSize] // 使用前blockSize字节作为IV
_, err = io.ReadFull(rand.Reader, iv)
if err != nil {
return "", err
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(cipherText[blockSize:], plainText)
return base64.URLEncoding.EncodeToString(cipherText), nil
}
// AES CBC模式解密Base64传入传出
func AesDecryptURL(cipherTextBase64 string, key []byte) ([]byte, error) {
cipherText, err := base64.URLEncoding.DecodeString(cipherTextBase64)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
if len(cipherText) < blockSize {
return nil, errors.New("ciphertext too short")
}
iv := cipherText[:blockSize]
cipherText = cipherText[blockSize:]
if len(cipherText)%blockSize != 0 {
return nil, errors.New("ciphertext is not a multiple of the block size")
}
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(cipherText, cipherText)
plainText, err := PKCS7UnPadding(cipherText)
if err != nil {
return nil, err
}
return plainText, nil
}

274
pkg/lzkit/crypto/ecb.go Normal file
View File

@@ -0,0 +1,274 @@
package crypto
import (
"crypto/aes"
"crypto/md5"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
)
// ECB模式是一种基本的加密模式每个明文块独立加密
// 警告ECB模式存在安全问题仅用于需要确定性加密的场景如数据库字段查询
// 不要用于加密大段文本或安全要求高的场景
// 验证密钥长度是否有效 (AES-128, AES-192, AES-256)
func validateAESKey(key []byte) error {
switch len(key) {
case 16, 24, 32:
return nil
default:
return errors.New("AES密钥长度必须是16、24或32字节(对应AES-128、AES-192、AES-256)")
}
}
// AesEcbEncrypt AES-ECB模式加密返回Base64编码的密文
// 使用已有的ECB实现但提供更易用的接口
func AesEcbEncrypt(plainText, key []byte) (string, error) {
if err := validateAESKey(key); err != nil {
return "", err
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
// 使用PKCS7填充
plainText = PKCS7Padding(plainText, block.BlockSize())
// 创建密文数组
cipherText := make([]byte, len(plainText))
// ECB模式加密使用west_crypto.go中已有的实现
mode := newECBEncrypter(block)
mode.CryptBlocks(cipherText, plainText)
// 返回Base64编码的密文
return base64.StdEncoding.EncodeToString(cipherText), nil
}
// AesEcbDecrypt AES-ECB模式解密输入Base64编码的密文
func AesEcbDecrypt(cipherTextBase64 string, key []byte) ([]byte, error) {
if err := validateAESKey(key); err != nil {
return nil, err
}
// Base64解码
cipherText, err := base64.StdEncoding.DecodeString(cipherTextBase64)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
// 检查密文长度
if len(cipherText)%block.BlockSize() != 0 {
return nil, errors.New("密文长度必须是块大小的整数倍")
}
// 创建明文数组
plainText := make([]byte, len(cipherText))
// ECB模式解密使用west_crypto.go中已有的实现
mode := newECBDecrypter(block)
mode.CryptBlocks(plainText, cipherText)
// 去除PKCS7填充
plainText, err = PKCS7UnPadding(plainText)
if err != nil {
return nil, err
}
return plainText, nil
}
// AesEcbEncryptHex AES-ECB模式加密返回十六进制编码的密文
func AesEcbEncryptHex(plainText, key []byte) (string, error) {
if err := validateAESKey(key); err != nil {
return "", err
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
// 使用PKCS7填充
plainText = PKCS7Padding(plainText, block.BlockSize())
// 创建密文数组
cipherText := make([]byte, len(plainText))
// ECB模式加密
mode := newECBEncrypter(block)
mode.CryptBlocks(cipherText, plainText)
// 返回十六进制编码的密文
return hex.EncodeToString(cipherText), nil
}
// AesEcbDecryptHex AES-ECB模式解密输入十六进制编码的密文
func AesEcbDecryptHex(cipherTextHex string, key []byte) ([]byte, error) {
if err := validateAESKey(key); err != nil {
return nil, err
}
// 十六进制解码
cipherText, err := hex.DecodeString(cipherTextHex)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
// 检查密文长度
if len(cipherText)%block.BlockSize() != 0 {
return nil, errors.New("密文长度必须是块大小的整数倍")
}
// 创建明文数组
plainText := make([]byte, len(cipherText))
// ECB模式解密
mode := newECBDecrypter(block)
mode.CryptBlocks(plainText, cipherText)
// 去除PKCS7填充
plainText, err = PKCS7UnPadding(plainText)
if err != nil {
return nil, err
}
return plainText, nil
}
// 以下是专门用于处理手机号等敏感数据的实用函数
// EncryptMobile 使用AES-ECB加密手机号返回Base64编码
// 该方法保证对相同手机号总是产生相同密文,便于数据库查询
func EncryptMobile(mobile string, secretKey string) (string, error) {
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
return "", decodeErr
}
if mobile == "" {
return "", errors.New("手机号不能为空")
}
return AesEcbEncrypt([]byte(mobile), key)
}
// DecryptMobile 解密手机号
func DecryptMobile(encryptedMobile string, secretKey string) (string, error) {
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
return "", decodeErr
}
if encryptedMobile == "" {
return "", errors.New("加密手机号不能为空")
}
bytes, err := AesEcbDecrypt(encryptedMobile, key)
if err != nil {
return "", fmt.Errorf("解密手机号失败: %v", err)
}
return string(bytes), nil
}
// EncryptMobileHex 使用AES-ECB加密手机号返回十六进制编码(适用于URL参数)
func EncryptMobileHex(mobile string, key []byte) (string, error) {
if mobile == "" {
return "", errors.New("手机号不能为空")
}
return AesEcbEncryptHex([]byte(mobile), key)
}
// DecryptMobileHex 解密十六进制编码的手机号
func DecryptMobileHex(encryptedMobileHex string, key []byte) (string, error) {
if encryptedMobileHex == "" {
return "", errors.New("加密手机号不能为空")
}
bytes, err := AesEcbDecryptHex(encryptedMobileHex, key)
if err != nil {
return "", fmt.Errorf("解密手机号失败: %v", err)
}
return string(bytes), nil
}
// EncryptIDCard 使用AES-ECB加密身份证号
func EncryptIDCard(idCard string, key []byte) (string, error) {
if idCard == "" {
return "", errors.New("身份证号不能为空")
}
return AesEcbEncrypt([]byte(idCard), key)
}
// DecryptIDCard 解密身份证号
func DecryptIDCard(encryptedIDCard string, key []byte) (string, error) {
if encryptedIDCard == "" {
return "", errors.New("加密身份证号不能为空")
}
bytes, err := AesEcbDecrypt(encryptedIDCard, key)
if err != nil {
return "", fmt.Errorf("解密身份证号失败: %v", err)
}
return string(bytes), nil
}
// IsEncrypted 检查字符串是否为Base64编码的加密数据
func IsEncrypted(data string) bool {
// 检查是否是有效的Base64编码
_, err := base64.StdEncoding.DecodeString(data)
return err == nil && len(data) >= 20 // 至少20个字符的Base64字符串
}
// GenerateAESKey 生成AES密钥
// keySize: 可选16, 24, 32字节(对应AES-128, AES-192, AES-256)
func GenerateAESKey(keySize int) ([]byte, error) {
if keySize != 16 && keySize != 24 && keySize != 32 {
return nil, errors.New("密钥长度必须是16、24或32字节")
}
key := make([]byte, keySize)
_, err := rand.Read(key)
if err != nil {
return nil, err
}
return key, nil
}
// DeriveKeyFromPassword 基于密码派生固定长度的AES密钥
func DeriveKeyFromPassword(password string, keySize int) ([]byte, error) {
if keySize != 16 && keySize != 24 && keySize != 32 {
return nil, errors.New("密钥长度必须是16、24或32字节")
}
// 使用PBKDF2或简单的方法从密码派生密钥
// 这里使用简单的MD5方法实际生产环境应使用更安全的PBKDF2
hash := md5.New()
hash.Write([]byte(password))
key := hash.Sum(nil) // 16字节
// 如果需要24或32字节继续哈希
if keySize > 16 {
hash.Reset()
hash.Write(key)
key = append(key, hash.Sum(nil)[:keySize-16]...)
}
return key, nil
}

View File

@@ -0,0 +1,185 @@
package crypto
import (
"encoding/base64"
"encoding/hex"
"fmt"
"testing"
)
func TestAesEcbMobileEncryption(t *testing.T) {
// 测试手机号加密
mobile := "13800138000"
key := []byte("1234567890abcdef") // 16字节AES-128密钥
keyStr := hex.EncodeToString(key)
// 测试加密
encrypted, err := EncryptMobile(mobile, keyStr)
if err != nil {
t.Fatalf("手机号加密失败: %v", err)
}
fmt.Println(encrypted)
// 测试解密
decrypted, err := DecryptMobile(encrypted, keyStr)
if err != nil {
t.Fatalf("手机号解密失败: %v", err)
}
fmt.Println(decrypted)
// 验证结果
if decrypted != mobile {
t.Errorf("解密结果不匹配,期望: %s, 实际: %s", mobile, decrypted)
}
// 测试相同输入产生相同输出(确定性)
encrypted2, _ := EncryptMobile(mobile, keyStr)
if encrypted != encrypted2 {
t.Errorf("AES-ECB不是确定性的两次加密结果不同: %s vs %s", encrypted, encrypted2)
}
}
func TestAesEcbHexEncryption(t *testing.T) {
// 测试十六进制编码加密
idCard := "440101199001011234"
key := []byte("1234567890abcdef") // 16字节AES-128密钥
// 测试HEX加密
encryptedHex, err := EncryptIDCard(idCard, key)
if err != nil {
t.Fatalf("身份证加密失败: %v", err)
}
// 测试HEX解密
decrypted, err := DecryptIDCard(encryptedHex, key)
if err != nil {
t.Fatalf("身份证解密失败: %v", err)
}
// 验证结果
if decrypted != idCard {
t.Errorf("解密结果不匹配,期望: %s, 实际: %s", idCard, decrypted)
}
}
func TestAesEcbKeyValidation(t *testing.T) {
// 测试不同长度的密钥
validKeys := [][]byte{
make([]byte, 16), // AES-128
make([]byte, 24), // AES-192
make([]byte, 32), // AES-256
}
invalidKeys := [][]byte{
make([]byte, 15),
make([]byte, 20),
make([]byte, 33),
}
text := []byte("test text")
// 测试有效密钥
for _, key := range validKeys {
_, err := AesEcbEncrypt(text, key)
if err != nil {
t.Errorf("有效密钥(%d字节)校验失败: %v", len(key), err)
}
}
// 测试无效密钥
for _, key := range invalidKeys {
_, err := AesEcbEncrypt(text, key)
if err == nil {
t.Errorf("无效密钥(%d字节)未被检测出", len(key))
}
}
}
func TestIsEncrypted(t *testing.T) {
// 有效的Base64编码字符串
validBase64 := base64.StdEncoding.EncodeToString([]byte("这是一个足够长的字符串以通过IsEncrypted检查"))
// 无效的字符串
invalidStrings := []string{
"",
"abc",
"not-base64!@#",
hex.EncodeToString([]byte("hexstring")),
}
// 测试有效的加密数据
if !IsEncrypted(validBase64) {
t.Errorf("有效的Base64未被识别为加密数据: %s", validBase64)
}
// 测试无效的数据
for _, s := range invalidStrings {
if IsEncrypted(s) {
t.Errorf("无效字符串被错误识别为加密数据: %s", s)
}
}
}
func TestDeriveKeyFromPassword(t *testing.T) {
password := "my-secure-password"
// 测试不同长度的派生密钥
keySizes := []int{16, 24, 32}
for _, size := range keySizes {
key, err := DeriveKeyFromPassword(password, size)
if err != nil {
t.Errorf("从密码派生%d字节密钥失败: %v", size, err)
continue
}
if len(key) != size {
t.Errorf("派生的密钥长度错误,期望: %d, 实际: %d", size, len(key))
}
// 测试相同密码总是产生相同密钥
key2, _ := DeriveKeyFromPassword(password, size)
if string(key) != string(key2) {
t.Errorf("从相同密码派生的密钥不一致")
}
// 使用派生的密钥加密测试
_, err = AesEcbEncrypt([]byte("test"), key)
if err != nil {
t.Errorf("使用派生的密钥加密失败: %v", err)
}
}
// 测试无效的密钥大小
_, err := DeriveKeyFromPassword(password, 18)
if err == nil {
t.Error("无效的密钥大小未被检测出")
}
}
func TestGenerateAESKey(t *testing.T) {
// 测试生成不同长度的密钥
keySizes := []int{16, 24, 32}
for _, size := range keySizes {
key, err := GenerateAESKey(size)
if err != nil {
t.Errorf("生成%d字节密钥失败: %v", size, err)
continue
}
if len(key) != size {
t.Errorf("生成的密钥长度错误,期望: %d, 实际: %d", size, len(key))
}
// 使用生成的密钥加密测试
_, err = AesEcbEncrypt([]byte("test"), key)
if err != nil {
t.Errorf("使用生成的密钥加密失败: %v", err)
}
}
// 测试无效的密钥大小
_, err := GenerateAESKey(18)
if err == nil {
t.Error("无效的密钥大小未被检测出")
}
}