-
Notifications
You must be signed in to change notification settings - Fork 171
/
pkcs5padding.go
37 lines (32 loc) · 940 Bytes
/
pkcs5padding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package goEncrypt
import (
"bytes"
)
/*
@Time : 2018/11/1 21:16
@Author : wuman
@File : padding
@Software: GoLand
*/
/**
1. Group plaintext
If the blocksize is not an integer multiple of blocksize, the blocksize bit should be considered
If des algorithm is used, the block size is 8 bytes
With the AES algorithm, 16 bytes of fast size are filled in
A tool for populating data when using block encryption mode
*/
// It is populated using pkcs5
func PKCS5Padding(plainText []byte, blockSize int) []byte {
padding := blockSize - (len(plainText) % blockSize)
padText := bytes.Repeat([]byte{byte(padding)}, padding)
newText := append(plainText, padText...)
return newText
}
func PKCS5UnPadding(plainText []byte, blockSize int) ([]byte, error) {
length := len(plainText)
number := int(plainText[length-1])
if number >= length || number > blockSize {
return nil, ErrPaddingSize
}
return plainText[:length-number], nil
}