-
Notifications
You must be signed in to change notification settings - Fork 50
/
aes_sc.go
51 lines (44 loc) · 1.1 KB
/
aes_sc.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 main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
)
func CheckError(err error) {
if err != nil {
panic(err)
}
}
//填充字符串(末尾)
func PaddingText1(str []byte, blockSize int) []byte {
//需要填充的数据长度
paddingCount := blockSize - len(str)%blockSize
//填充数据为:paddingCount ,填充的值为:paddingCount
paddingStr := bytes.Repeat([]byte{byte(paddingCount)}, paddingCount)
newPaddingStr := append(str, paddingStr...)
//fmt.Println(newPaddingStr)
return newPaddingStr
}
//---------------DES加密 解密--------------------
func EncyptogAES(src, key []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
fmt.Println(nil)
return nil
}
src = PaddingText1(src, block.BlockSize())
blockMode := cipher.NewCBCEncrypter(block, key)
blockMode.CryptBlocks(src, src)
return src
}
func main() {
//payload替换
str := "payload"
//密钥长度16
key := []byte("LeslieCheungKwok")
src := EncyptogAES([]byte(str), key)
base64Str := base64.StdEncoding.EncodeToString(src)
fmt.Println("加密后的数据为:", base64Str)
}