完善其他类型的加解密

This commit is contained in:
Yun
2025-11-03 23:03:17 +08:00
parent 8bcdc16955
commit 3dd73f928d
6 changed files with 831 additions and 59 deletions
+248 -51
View File
@@ -9,46 +9,116 @@ import (
"errors"
"fmt"
"io"
"strings"
)
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func PKCS7UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
// AES加密,CBC
func AesEncrypt(origData, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
// AES-256-ECB 加密,返回大写的十六进制字符串
func EncryptECBHex(input, key string) (string, error) {
// 创建AES加密器
block, err := aes.NewCipher([]byte(key))
if err != nil {
return nil, err
return "", err
}
// 对输入数据进行PKCS7填充
paddedInput := PKCS7Padding([]byte(input), block.BlockSize())
// ECB模式加密
encrypted := make([]byte, len(paddedInput))
for i := 0; i < len(paddedInput); i += aes.BlockSize {
block.Encrypt(encrypted[i:i+aes.BlockSize], paddedInput[i:i+aes.BlockSize])
}
// 转换为十六进制并大写
result := hex.EncodeToString(encrypted)
return strings.ToUpper(result), nil
}
// AES-256-ECB 解密,输入大写的十六进制字符串
func DecryptECBHex(encryptedHex, key string) (string, error) {
encrypted, err := hex.DecodeString(encryptedHex)
if err != nil {
return "", fmt.Errorf("hex解码失败: %v", err)
}
block, err := aes.NewCipher([]byte(key))
if err != nil {
return "", fmt.Errorf("创建AES解密器失败: %v", err)
}
// ECB模式解密
decrypted := make([]byte, len(encrypted))
for i := 0; i < len(encrypted); i += aes.BlockSize {
block.Decrypt(decrypted[i:i+aes.BlockSize], encrypted[i:i+aes.BlockSize])
}
// 去除PKCS7填充
unpaddedData, err := PKCS7UnPadding(decrypted)
if err != nil {
return "", fmt.Errorf("去除填充失败: %v", err)
}
return string(unpaddedData), nil
}
// AES-256-CBC 加密
func EncryptCBCHex(plaintext, key string) (string, error) {
block, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
blockSize := block.BlockSize()
origData = PKCS7Padding(origData, blockSize)
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
crypted := make([]byte, len(origData))
blockMode.CryptBlocks(crypted, origData)
return crypted, nil
plainBytes := PKCS7Padding([]byte(plaintext), blockSize)
// 创建密文字节切片,包含IV空间
cipherbyte := make([]byte, aes.BlockSize+len(plainBytes))
// 生成随机IV
iv := cipherbyte[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return "", err
}
// 创建CBC模式加密器
blockMode := cipher.NewCBCEncrypter(block, iv)
// 加密数据
blockMode.CryptBlocks(cipherbyte[aes.BlockSize:], plainBytes)
// 转换为十六进制并大写
result := hex.EncodeToString(cipherbyte)
return strings.ToUpper(result), nil
}
// AES解密
func AesDecrypt(crypted, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
// AES-256-CBC 解密
func DecryptCBCHex(encryptedHex, key string) (string, error) {
cipherbyte, err := hex.DecodeString(encryptedHex)
if err != nil {
return nil, err
return "", fmt.Errorf("hex解码失败: %v", err)
}
blockSize := block.BlockSize()
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
origData := make([]byte, len(crypted))
blockMode.CryptBlocks(origData, crypted)
origData = PKCS7UnPadding(origData)
return origData, nil
if len(cipherbyte) < aes.BlockSize {
return "", errors.New("ciphertext too short")
}
block, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
iv := cipherbyte[:aes.BlockSize]
cipherbyte = cipherbyte[aes.BlockSize:]
if len(cipherbyte)%aes.BlockSize != 0 {
return "", errors.New("ciphertext is not a multiple of the block size")
}
blockMode := cipher.NewCBCDecrypter(block, iv)
origData := make([]byte, len(cipherbyte))
blockMode.CryptBlocks(origData, cipherbyte)
origBy, err := PKCS7UnPadding(origData)
if err != nil {
return "", err
}
return string(origBy), nil
}
/*
@@ -81,12 +151,10 @@ func AESCBCEncrypt(key, plaintext string) (ciphertext string, err error) {
return
}
/*
AES CBC 解码
key:解密key
ciphertext:加密返回的串
plaintext:解密后的字符串
*/
// AES CBC 解码
// key:解密key
// ciphertext:加密返回的串
// plaintext:解密后的字符串
func AESCBCDecrypter(key, ciphertext string) (plaintext string, err error) {
cipherbyte, _ := hex.DecodeString(ciphertext)
keybyte := []byte(key)
@@ -112,13 +180,11 @@ func AESCBCDecrypter(key, ciphertext string) (plaintext string, err error) {
return
}
/*
AES GCM 加密
key:加密key
plaintext:加密明文
ciphertext:解密返回字节字符串[ 整型以十六进制方式显示]
*/
func AESGCMEncrypt(key, plaintext string) (ciphertext, noncetext string, err error) {
// AES GCM 加密
// key:加密key
// plaintext:加密明文
// ciphertext:解密返回字节字符串[ 整型以十六进制方式显示]
func EncryptGCM(key, plaintext string) (ciphertext, noncetext string, err error) {
plainbyte := []byte(plaintext)
keybyte := []byte(key)
block, err := aes.NewCipher(keybyte)
@@ -143,13 +209,11 @@ func AESGCMEncrypt(key, plaintext string) (ciphertext, noncetext string, err err
return
}
/*
AES GCM 解码
key:解密key
ciphertext:加密返回的串
plaintext:解密后的字符串
*/
func AESGCMDecrypter(key, ciphertext, noncetext string) (plaintext string, err error) {
// AES GCM 解码
// key:解密key
// ciphertext:加密返回的串
// plaintext:解密后的字符串
func DecryptGCM(key, ciphertext, noncetext string) (plaintext string, err error) {
cipherbyte, _ := hex.DecodeString(ciphertext)
nonce, _ := hex.DecodeString(noncetext)
keybyte := []byte(key)
@@ -172,3 +236,136 @@ func AESGCMDecrypter(key, ciphertext, noncetext string) (plaintext string, err e
plaintext = string(plainbyte[:])
return
}
// AES CFB 加密
func EncryptCFBHex(plaintext, key string) (string, error) {
block, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
// 创建密文字节切片,包含IV空间
cipherbyte := make([]byte, aes.BlockSize+len(plaintext))
// 生成随机IV
iv := cipherbyte[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return "", err
}
// 创建CFB模式加密器
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(cipherbyte[aes.BlockSize:], []byte(plaintext))
// 转换为十六进制并大写
result := hex.EncodeToString(cipherbyte)
return strings.ToUpper(result), nil
}
// AES CFB 解密
func DecryptCFBHex(encryptedHex, key string) (string, error) {
cipherbyte, err := hex.DecodeString(encryptedHex)
if err != nil {
return "", fmt.Errorf("hex解码失败: %v", err)
}
if len(cipherbyte) < aes.BlockSize {
return "", errors.New("ciphertext too short")
}
block, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
// 获取IV
iv := cipherbyte[:aes.BlockSize]
cipherbyte = cipherbyte[aes.BlockSize:]
// 创建CFB模式解密器
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(cipherbyte, cipherbyte)
return string(cipherbyte), nil
}
// AES OFB 加密
func EncryptOFBHex(plaintext, key string) (string, error) {
block, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
// 创建密文字节切片,包含IV空间
cipherbyte := make([]byte, aes.BlockSize+len(plaintext))
// 生成随机IV
iv := cipherbyte[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return "", err
}
// 创建OFB模式加密器
stream := cipher.NewOFB(block, iv)
stream.XORKeyStream(cipherbyte[aes.BlockSize:], []byte(plaintext))
// 转换为十六进制并大写
result := hex.EncodeToString(cipherbyte)
return strings.ToUpper(result), nil
}
// AES OFB 解密
func DecryptOFBHex(encryptedHex, key string) (string, error) {
cipherbyte, err := hex.DecodeString(encryptedHex)
if err != nil {
return "", fmt.Errorf("hex解码失败: %v", err)
}
if len(cipherbyte) < aes.BlockSize {
return "", errors.New("ciphertext too short")
}
block, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
// 获取IV
iv := cipherbyte[:aes.BlockSize]
cipherbyte = cipherbyte[aes.BlockSize:]
// 创建OFB模式解密器
stream := cipher.NewOFB(block, iv)
stream.XORKeyStream(cipherbyte, cipherbyte)
return string(cipherbyte), nil
}
// PKCS7补码
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
// 去除PKCS7补码
func PKCS7UnPadding(data []byte) ([]byte, error) {
length := len(data)
if length == 0 {
return nil, errors.New("ciphertext is empty")
}
unpadding := int(data[length-1])
if unpadding > length || unpadding == 0 {
return nil, errors.New("unpadding size is invalid")
}
// 检查填充是否正确
for i := length - unpadding; i < length; i++ {
if data[i] != byte(unpadding) {
return nil, errors.New("invalid padding")
}
}
return data[:(length - unpadding)], nil
}