Navigation Menu

Skip to content

Commit

Permalink
fix bug in aes unpadding
Browse files Browse the repository at this point in the history
  • Loading branch information
AlverLyu committed Mar 28, 2018
1 parent 1178307 commit e5a658c
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions aes/aes.go
Expand Up @@ -60,9 +60,7 @@ func AesDecrypt(ciphertext []byte, key []byte, iv []byte) ([]byte, error) {

plaintext := make([]byte, len(ciphertext))
blockModel.CryptBlocks(plaintext, ciphertext)
plaintext = PKCS5UnPadding(plaintext)

return plaintext, nil
return PKCS5UnPadding(plaintext)
}

func PKCS5Padding(src []byte, blockSize int) []byte {
Expand All @@ -72,8 +70,11 @@ func PKCS5Padding(src []byte, blockSize int) []byte {
return append(src, padtext...)
}

func PKCS5UnPadding(src []byte) []byte {
func PKCS5UnPadding(src []byte) ([]byte, error) {
length := len(src)
unpadding := int(src[length-1])
return src[:(length - unpadding)]
if unpadding > length {
return nil, errors.New("unpadding error: invalid paddding length")
}
return src[:(length - unpadding)], nil
}

0 comments on commit e5a658c

Please sign in to comment.