base-golang/pkg/aes/padding.go
2024-07-23 10:23:43 +08:00

26 lines
968 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package aes
import (
"bytes"
)
/**
* 填充有六种NoPadding, PKCS#5, PKCS#7, ISO 10126, ANSI X9.23和ZerosPadding
* 对于AES来说PKCS5Padding和PKCS7Padding是完全一样的不同在于PKCS5限定了块大小为8bytes而PKCS7没有限定
* 因此对于AES来说两者完全相同但是对于Rijndael就不一样了
* AES是Rijndael在块大小为8bytes时的特例对于使用其他信息块大小的Rijndael算法只能使用PKCS7
* 在AES加密当中严格来说是不能使用pkcs5的因为AES的块大小是16bytes而pkcs5只能用于8bytes通常我们在AES加密中所说的pkcs5指的就是pkcs7
*/
func pkcsPadding(cipherText []byte, blockSize int) []byte {
p := blockSize - len(cipherText)%blockSize
padText := bytes.Repeat([]byte{byte(p)}, p)
return append(cipherText, padText...)
}
func pkcsUnPadding(decrypted []byte) []byte {
length := len(decrypted)
u := int(decrypted[length-1])
return decrypted[:(length - u)]
}