ai update.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
// Package aes 提供AES加密解密功能
|
||||
// 支持GCM、CBC、ECB等多种加密模式
|
||||
package aes
|
||||
|
||||
import (
|
||||
@@ -13,8 +15,11 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// =================== GCM ======================
|
||||
// AEC GCM 加密
|
||||
// =================== GCM模式 ======================
|
||||
// AESGCMEncrypt AES GCM模式加密
|
||||
// plaintext: 明文数据
|
||||
// key: 加密密钥
|
||||
// 返回: 十六进制编码的密文字符串
|
||||
func AESGCMEncrypt(plaintext, key []byte) (string, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
@@ -32,7 +37,10 @@ func AESGCMEncrypt(plaintext, key []byte) (string, error) {
|
||||
return hex.EncodeToString(ciphertext), nil
|
||||
}
|
||||
|
||||
// AEC GCM 解密
|
||||
// AESGCMDecrypt AES GCM模式解密
|
||||
// ciphertext: 十六进制编码的密文字符串
|
||||
// key: 解密密钥
|
||||
// 返回: 解密后的明文数据
|
||||
func AESGCMDecrypt(ciphertext string, key []byte) ([]byte, error) {
|
||||
data, err := hex.DecodeString(ciphertext)
|
||||
if err != nil {
|
||||
@@ -54,8 +62,12 @@ func AESGCMDecrypt(ciphertext string, key []byte) ([]byte, error) {
|
||||
return gcm.Open(nil, nonce, cipherbyte, nil)
|
||||
}
|
||||
|
||||
// =================== CBC ======================
|
||||
// AES CBC加密
|
||||
// =================== CBC模式 ======================
|
||||
// Encrypt AES CBC模式加密
|
||||
// key: Base64编码的密钥
|
||||
// iv: Base64编码的初始化向量
|
||||
// data: 要加密的数据
|
||||
// 返回: Base64编码的密文
|
||||
func Encrypt(key string, iv string, data string) string {
|
||||
if len(data) == 0 {
|
||||
return ""
|
||||
@@ -72,7 +84,11 @@ func Encrypt(key string, iv string, data string) string {
|
||||
return data
|
||||
}
|
||||
|
||||
// AES CBC解密
|
||||
// Decrypt AES CBC模式解密
|
||||
// key: Base64编码的密钥
|
||||
// iv: Base64编码的初始化向量
|
||||
// data: Base64编码的密文
|
||||
// 返回: 解密后的明文
|
||||
func Decrypt(key string, iv string, data string) string {
|
||||
if len(data) == 0 {
|
||||
return ""
|
||||
@@ -91,11 +107,19 @@ func Decrypt(key string, iv string, data string) string {
|
||||
return data
|
||||
}
|
||||
|
||||
// _PKCS5Padding PKCS5填充
|
||||
// cipherText: 需要填充的数据
|
||||
// blockSize: 块大小
|
||||
// 返回: 填充后的数据
|
||||
func _PKCS5Padding(cipherText []byte, blockSize int) []byte {
|
||||
padding := blockSize - len(cipherText)%blockSize
|
||||
padText := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||
return append(cipherText, padText...)
|
||||
}
|
||||
|
||||
// _PKCS5UnPadding PKCS5去填充
|
||||
// origData: 需要去填充的数据
|
||||
// 返回: 去填充后的数据
|
||||
func _PKCS5UnPadding(origData []byte) []byte {
|
||||
length := len(origData)
|
||||
unpadding := int(origData[length-1])
|
||||
@@ -105,7 +129,11 @@ func _PKCS5UnPadding(origData []byte) []byte {
|
||||
return origData[:(length - unpadding)]
|
||||
}
|
||||
|
||||
// =================== ECB ======================
|
||||
// =================== ECB模式 ======================
|
||||
// AesEncryptECB AES ECB模式加密
|
||||
// origData: 原始数据
|
||||
// key: 加密密钥
|
||||
// 返回: Base64编码的密文
|
||||
func AesEncryptECB(origData []byte, key []byte) (data string) {
|
||||
cipher, _ := aes.NewCipher(generateKey(key))
|
||||
length := (len(origData) + aes.BlockSize) / aes.BlockSize
|
||||
@@ -124,11 +152,16 @@ func AesEncryptECB(origData []byte, key []byte) (data string) {
|
||||
data = base64.StdEncoding.EncodeToString(encrypted)
|
||||
return data
|
||||
}
|
||||
|
||||
// AesDecryptECB AES ECB模式解密
|
||||
// encrypted: Base64编码的密文
|
||||
// key: 解密密钥
|
||||
// 返回: 解密后的明文数据
|
||||
func AesDecryptECB(encrypted string, key []byte) (decrypted []byte) {
|
||||
decodedCiphertext, _ := base64.StdEncoding.DecodeString(encrypted)
|
||||
cipher, _ := aes.NewCipher(generateKey(key))
|
||||
decrypted = make([]byte, len(decodedCiphertext))
|
||||
//
|
||||
// 分组分块解密
|
||||
for bs, be := 0, cipher.BlockSize(); bs < len(decodedCiphertext); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
|
||||
cipher.Decrypt(decrypted[bs:be], decodedCiphertext[bs:be])
|
||||
}
|
||||
@@ -140,6 +173,10 @@ func AesDecryptECB(encrypted string, key []byte) (decrypted []byte) {
|
||||
|
||||
return decrypted[:trim]
|
||||
}
|
||||
|
||||
// generateKey 生成标准长度的密钥
|
||||
// key: 原始密钥
|
||||
// 返回: 16字节的标准密钥
|
||||
func generateKey(key []byte) (genKey []byte) {
|
||||
genKey = make([]byte, 16)
|
||||
copy(genKey, key)
|
||||
@@ -151,6 +188,9 @@ func generateKey(key []byte) (genKey []byte) {
|
||||
return genKey
|
||||
}
|
||||
|
||||
// AesKeyCheck 检查AES密钥环境变量
|
||||
// key: 环境变量名
|
||||
// 返回: 十六进制编码的密钥字符串
|
||||
func AesKeyCheck(key string) (string, error) {
|
||||
// 从环境变量获取密钥
|
||||
keyHex := os.Getenv(key)
|
||||
|
||||
Reference in New Issue
Block a user