You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
package tools
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/aes"
|
|
|
|
|
"crypto/cipher"
|
|
|
|
|
"errors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// AesCBCDecrypt AEC解密(CBC模式)
|
|
|
|
|
func AesCBCDecrypt(encryptData, key, iv []byte) ([]byte, error) {
|
|
|
|
|
block, err := aes.NewCipher(key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
blockSize := block.BlockSize()
|
|
|
|
|
if len(encryptData) < blockSize {
|
|
|
|
|
return nil, errors.New("ciphertext too short")
|
|
|
|
|
}
|
|
|
|
|
if len(encryptData)%blockSize != 0 {
|
|
|
|
|
return nil, errors.New("ciphertext is not a multiple of the block size")
|
|
|
|
|
}
|
|
|
|
|
mode := cipher.NewCBCDecrypter(block, iv)
|
|
|
|
|
mode.CryptBlocks(encryptData, encryptData)
|
|
|
|
|
// 解填充
|
|
|
|
|
encryptData = PKCS7UnPadding(encryptData)
|
|
|
|
|
return encryptData, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PKCS7UnPadding 对密文删除填充
|
|
|
|
|
func PKCS7UnPadding(origData []byte) []byte {
|
|
|
|
|
length := len(origData)
|
|
|
|
|
unpadding := int(origData[length-1])
|
|
|
|
|
return origData[:(length - unpadding)]
|
|
|
|
|
}
|