Skip to content

Commit 52cc049

Browse files
committed
添加aes异常数据判断
1 parent 19021da commit 52cc049

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

util/aes/ecb.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"crypto/aes"
66
"crypto/cipher"
77
"encoding/base64"
8-
"fmt"
98
"strings"
109

1110
"github.com/juju/errors"
@@ -52,7 +51,6 @@ func Decrypt(decodeStr string, key string) (string, error) {
5251
blockSize := block.BlockSize()
5352
blockMode := cipher.NewCBCDecrypter(block, decodeKey[:blockSize])
5453
origData := make([]byte, len(decodeBytes))
55-
fmt.Printf("ori:%d, dec:%d\n", len(origData), len(decodeBytes))
5654
blockMode.CryptBlocks(origData, decodeBytes)
5755
origData, err = pkcs5UnPadding(origData)
5856
if err != nil {
@@ -72,11 +70,13 @@ func pkcs5Padding(ciphertext []byte, blockSize int) []byte {
7270
// pkcs5UnPadding pkcs5 删除数据.
7371
func pkcs5UnPadding(origData []byte) ([]byte, error) {
7472
length := len(origData)
75-
76-
if length <= 0 {
77-
return nil, errors.New("pkcs5Padding len(origData) <= 0 error")
73+
if length < 1 {
74+
return nil, errors.New("origData is empty")
7875
}
7976

8077
unpadding := int(origData[length-1])
78+
if unpadding > length {
79+
return nil, errors.New("invalid origData")
80+
}
8181
return origData[:(length - unpadding)], nil
8282
}

0 commit comments

Comments
 (0)