2024-10-02 00:57:17 +08:00
|
|
|
|
package crypto
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"crypto/aes"
|
|
|
|
|
"crypto/cipher"
|
|
|
|
|
"crypto/rand"
|
|
|
|
|
"encoding/base64"
|
|
|
|
|
"errors"
|
|
|
|
|
"io"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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(origData []byte) ([]byte, error) {
|
|
|
|
|
length := len(origData)
|
|
|
|
|
if length == 0 {
|
|
|
|
|
return nil, errors.New("input data error")
|
|
|
|
|
}
|
|
|
|
|
unpadding := int(origData[length-1])
|
|
|
|
|
if unpadding > length {
|
|
|
|
|
return nil, errors.New("unpadding size is invalid")
|
|
|
|
|
}
|
2024-10-13 22:17:25 +08:00
|
|
|
|
|
|
|
|
|
// 检查填充字节是否一致
|
|
|
|
|
for i := 0; i < unpadding; i++ {
|
|
|
|
|
if origData[length-1-i] != byte(unpadding) {
|
|
|
|
|
return nil, errors.New("invalid padding")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 00:57:17 +08:00
|
|
|
|
return origData[:(length - unpadding)], nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AES CBC模式加密,Base64传入传出
|
|
|
|
|
func AesEncrypt(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.StdEncoding.EncodeToString(cipherText), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AES CBC模式解密,Base64传入传出
|
|
|
|
|
func AesDecrypt(cipherTextBase64 string, key []byte) ([]byte, error) {
|
|
|
|
|
cipherText, err := base64.StdEncoding.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
|
|
|
|
|
}
|