|
@@ -13,13 +13,56 @@ func PKCS5Padding(plaintext []byte, blockSize int) []byte { |
|
@@ -13,13 +13,56 @@ func PKCS5Padding(plaintext []byte, blockSize int) []byte { |
13
|
}
|
13
|
}
|
14
|
|
14
|
|
15
|
//@brief:去除填充数据
|
15
|
//@brief:去除填充数据
|
16
|
-func PKCS5UnPadding(origData []byte) []byte {
|
16
|
+func PKCS5UnPadding(origData []byte, blockSize int) []byte {
|
17
|
length := len(origData)
|
17
|
length := len(origData)
|
18
|
unpadding := int(origData[length-1])
|
18
|
unpadding := int(origData[length-1])
|
19
|
return origData[:(length - unpadding)]
|
19
|
return origData[:(length - unpadding)]
|
20
|
}
|
20
|
}
|
21
|
|
21
|
|
22
|
-//@brief:AES加密
|
22
|
+// PKCS7Padding right-pads the given byte slice with 1 to n bytes, where
|
|
|
23
|
+// n is the block size. The size of the result is x times n, where x
|
|
|
24
|
+// is at least 1.
|
|
|
25
|
+func PKCS7Padding(b []byte, blockSize int) []byte {
|
|
|
26
|
+ if blockSize <= 0 {
|
|
|
27
|
+ return nil
|
|
|
28
|
+ }
|
|
|
29
|
+ if b == nil || len(b) == 0 {
|
|
|
30
|
+ return nil
|
|
|
31
|
+ }
|
|
|
32
|
+ n := blockSize - (len(b) % blockSize)
|
|
|
33
|
+ pb := make([]byte, len(b)+n)
|
|
|
34
|
+ copy(pb, b)
|
|
|
35
|
+ copy(pb[len(b):], bytes.Repeat([]byte{byte(n)}, n))
|
|
|
36
|
+ return pb
|
|
|
37
|
+}
|
|
|
38
|
+
|
|
|
39
|
+// PKCS7UnPadding validates and unpads data from the given bytes slice.
|
|
|
40
|
+// The returned value will be 1 to n bytes smaller depending on the
|
|
|
41
|
+// amount of padding, where n is the block size.
|
|
|
42
|
+func PKCS7UnPadding(b []byte, blockSize int) []byte {
|
|
|
43
|
+ if blockSize <= 0 {
|
|
|
44
|
+ return nil
|
|
|
45
|
+ }
|
|
|
46
|
+ if b == nil || len(b) == 0 {
|
|
|
47
|
+ return nil
|
|
|
48
|
+ }
|
|
|
49
|
+ if len(b)%blockSize != 0 {
|
|
|
50
|
+ return nil
|
|
|
51
|
+ }
|
|
|
52
|
+ c := b[len(b)-1]
|
|
|
53
|
+ n := int(c)
|
|
|
54
|
+ if n == 0 || n > len(b) {
|
|
|
55
|
+ return nil
|
|
|
56
|
+ }
|
|
|
57
|
+ for i := 0; i < n; i++ {
|
|
|
58
|
+ if b[len(b)-n+i] != c {
|
|
|
59
|
+ return nil
|
|
|
60
|
+ }
|
|
|
61
|
+ }
|
|
|
62
|
+ return b[:len(b)-n]
|
|
|
63
|
+}
|
|
|
64
|
+
|
|
|
65
|
+// AES加密
|
23
|
func AesEncrypt(origData, key []byte) ([]byte, error) {
|
66
|
func AesEncrypt(origData, key []byte) ([]byte, error) {
|
24
|
block, err := aes.NewCipher(key)
|
67
|
block, err := aes.NewCipher(key)
|
25
|
if err != nil {
|
68
|
if err != nil {
|
|
@@ -28,14 +71,14 @@ func AesEncrypt(origData, key []byte) ([]byte, error) { |
|
@@ -28,14 +71,14 @@ func AesEncrypt(origData, key []byte) ([]byte, error) { |
28
|
|
71
|
|
29
|
//AES分组长度为128位,所以blockSize=16,单位字节
|
72
|
//AES分组长度为128位,所以blockSize=16,单位字节
|
30
|
blockSize := block.BlockSize()
|
73
|
blockSize := block.BlockSize()
|
31
|
- origData = PKCS5Padding(origData, blockSize)
|
74
|
+ origData = PKCS7Padding(origData, blockSize)
|
32
|
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize]) //初始向量的长度必须等于块block的长度16字节
|
75
|
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize]) //初始向量的长度必须等于块block的长度16字节
|
33
|
crypted := make([]byte, len(origData))
|
76
|
crypted := make([]byte, len(origData))
|
34
|
blockMode.CryptBlocks(crypted, origData)
|
77
|
blockMode.CryptBlocks(crypted, origData)
|
35
|
return crypted, nil
|
78
|
return crypted, nil
|
36
|
}
|
79
|
}
|
37
|
|
80
|
|
38
|
-//@brief:AES解密
|
81
|
+// AES解密
|
39
|
func AesDecrypt(crypted, key []byte) ([]byte, error) {
|
82
|
func AesDecrypt(crypted, key []byte) ([]byte, error) {
|
40
|
block, err := aes.NewCipher(key)
|
83
|
block, err := aes.NewCipher(key)
|
41
|
if err != nil {
|
84
|
if err != nil {
|
|
@@ -47,6 +90,6 @@ func AesDecrypt(crypted, key []byte) ([]byte, error) { |
|
@@ -47,6 +90,6 @@ func AesDecrypt(crypted, key []byte) ([]byte, error) { |
47
|
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) //初始向量的长度必须等于块block的长度16字节
|
90
|
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) //初始向量的长度必须等于块block的长度16字节
|
48
|
origData := make([]byte, len(crypted))
|
91
|
origData := make([]byte, len(crypted))
|
49
|
blockMode.CryptBlocks(origData, crypted)
|
92
|
blockMode.CryptBlocks(origData, crypted)
|
50
|
- origData = PKCS5UnPadding(origData)
|
93
|
+ origData = PKCS7UnPadding(origData, blockSize)
|
51
|
return origData, nil
|
94
|
return origData, nil
|
52
|
} |
95
|
} |