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.

35 lines
846 B

4 years ago
package tools
import (
"crypto/aes"
"crypto/cipher"
"errors"
)
4 years ago
// AesCBCDecrypt AEC解密CBC模式
func AesCBCDecrypt(encryptData, key, iv []byte) ([]byte, error) {
4 years ago
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
}
4 years ago
// PKCS7UnPadding 对密文删除填充
4 years ago
func PKCS7UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}