github.com/suiyunonghen/DxCommonLib@v0.5.3/cryptlib/padding.go (about)

     1  package cryptlib
     2  
     3  import "bytes"
     4  
     5  func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
     6  	padding := blockSize - len(ciphertext)%blockSize
     7  	padtext := bytes.Repeat([]byte{byte(padding)}, padding)
     8  	return append(ciphertext, padtext...)
     9  }
    10  
    11  func PKCS5UnPadding(origData []byte) []byte {
    12  	length := len(origData)
    13  	// 去掉最后一个字节 unpadding 次
    14  	unpadding := int(origData[length-1])
    15  	trcateLen := length - unpadding
    16  	if trcateLen < 0 || trcateLen >= length{
    17  		return nil
    18  	}
    19  	return origData[:trcateLen]
    20  }