|
@@ -30,13 +30,13 @@ func (a AES_CBC) EncryptHex(value string) (string, error) {
|
|
|
}
|
|
|
|
|
|
func (a AES_CBC) encrypt(value string) ([]byte, error) {
|
|
|
+ defer Catch()
|
|
|
origData := []byte(value)
|
|
|
block, err := aes.NewCipher([]byte(a.Key))
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- blockSize := block.BlockSize()
|
|
|
- origData = a.PKCS5Padding(origData, blockSize)
|
|
|
+ origData = a.PKCS5Padding(origData, block.BlockSize())
|
|
|
// origData = a.ZeroPadding(origData, block.BlockSize())
|
|
|
blockMode := cipher.NewCBCEncrypter(block, []byte(a.Iv))
|
|
|
crypted := make([]byte, len(origData))
|
|
@@ -56,6 +56,7 @@ func (a AES_CBC) DecryptHex(crypted string) (string, error) {
|
|
|
}
|
|
|
|
|
|
func (a AES_CBC) decrypt(decodeData []byte, err error) (string, error) {
|
|
|
+ defer Catch()
|
|
|
if err != nil {
|
|
|
return "", err
|
|
|
}
|
|
@@ -80,7 +81,13 @@ func (a AES_CBC) ZeroPadding(ciphertext []byte, blockSize int) []byte {
|
|
|
|
|
|
func (a AES_CBC) ZeroUnPadding(origData []byte) []byte {
|
|
|
length := len(origData)
|
|
|
+ if length == 0 {
|
|
|
+ return origData
|
|
|
+ }
|
|
|
unpadding := int(origData[length-1])
|
|
|
+ if length < unpadding {
|
|
|
+ return origData
|
|
|
+ }
|
|
|
return origData[:(length - unpadding)]
|
|
|
}
|
|
|
|
|
@@ -92,7 +99,13 @@ func (a AES_CBC) PKCS5Padding(ciphertext []byte, blockSize int) []byte {
|
|
|
|
|
|
func (a AES_CBC) PKCS5UnPadding(origData []byte) []byte {
|
|
|
length := len(origData)
|
|
|
+ if length == 0 {
|
|
|
+ return origData
|
|
|
+ }
|
|
|
// 去掉最后一个字节 unpadding 次
|
|
|
unpadding := int(origData[length-1])
|
|
|
+ if length < unpadding {
|
|
|
+ return origData
|
|
|
+ }
|
|
|
return origData[:(length - unpadding)]
|
|
|
}
|