telco_server/tools/crypto/aes.go

103 lines
2.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package crypto
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
)
// AESEncryptJson AES加密json函数
func AESEncryptJson(fixedAESKey string, plainText string) (string, error) {
// 将十六进制字符串转换为字节数组
key, err := hex.DecodeString(fixedAESKey)
if err != nil {
fmt.Printf("解码 AES 密钥失败: %v", err)
return "", err
}
// 密钥长度检查,确保是 16、24 或 32 字节
if len(key) != 16 && len(key) != 24 && len(key) != 32 {
return "", fmt.Errorf("无效的AES密钥长度: %v", len(key))
}
// 创建AES块
block, err := aes.NewCipher(key)
if err != nil {
return "", fmt.Errorf("创建AES块失败: %v", err)
}
// 使用AES GCM加密
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", fmt.Errorf("创建GCM模式失败: %v", err)
}
// 创建随机的Nonce
nonce := make([]byte, gcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return "", fmt.Errorf("生成随机数失败: %v", err)
}
// 加密数据
ciphertext := gcm.Seal(nonce, nonce, []byte(plainText), nil)
// 返回Base64编码的密文
return base64.StdEncoding.EncodeToString(ciphertext), nil
}
// AESDecryptJson 解密JSON数据
func AESDecryptJson(fixedAESKey string, encryptedText string) (string, error) {
// 将十六进制字符串转换为字节数组
key, err := hex.DecodeString(fixedAESKey)
if err != nil {
fmt.Printf("解码 AES 密钥失败: %v", err)
return "", err
}
// 密钥长度检查,确保是 16、24 或 32 字节
if len(key) != 16 && len(key) != 24 && len(key) != 32 {
return "", fmt.Errorf("无效的AES密钥长度: %v", len(key))
}
// 创建AES块
block, err := aes.NewCipher(key)
if err != nil {
return "", fmt.Errorf("创建AES块失败: %v", err)
}
// 解码Base64加密的文本
ciphertext, err := base64.StdEncoding.DecodeString(encryptedText)
if err != nil {
return "", fmt.Errorf("解码Base64密文失败: %v", err)
}
// 获取nonce大小
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", fmt.Errorf("创建GCM模式失败: %v", err)
}
nonceSize := gcm.NonceSize()
// 检查密文长度确保包含nonce
if len(ciphertext) < nonceSize {
return "", fmt.Errorf("密文过短")
}
// 提取nonce和密文
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
// 解密
plainText, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return "", fmt.Errorf("解密失败: %v", err)
}
// 将解密后的字节数组转换为字符串并返回
return string(plainText), nil
}