-
Notifications
You must be signed in to change notification settings - Fork 25
/
cipher.go
51 lines (40 loc) · 948 Bytes
/
cipher.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package pkcs8
import(
"bytes"
"crypto/rand"
)
// 随机生成字符
func genRandom(len int) ([]byte, error) {
value := make([]byte, len)
_, err := rand.Read(value)
return value, err
}
// 明文补码算法
// 填充至符合块大小的整数倍,填充值为填充数量数
func pkcs7Padding(text []byte, blockSize int) []byte {
n := len(text)
if n == 0 || blockSize < 1 {
return text
}
paddingSize := blockSize - n%blockSize
// 为 0 时补位 blockSize 值
if paddingSize == 0 {
paddingSize = blockSize
}
paddingText := bytes.Repeat([]byte{byte(paddingSize)}, paddingSize)
return append(text, paddingText...)
}
// 明文减码算法
func pkcs7UnPadding(src []byte) []byte {
n := len(src)
if n == 0 {
return src
}
count := int(src[n-1])
num := n-count
if num < 0 {
return src
}
text := src[:num]
return text
}