-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaes.go
More file actions
52 lines (44 loc) · 1.3 KB
/
Copy pathaes.go
File metadata and controls
52 lines (44 loc) · 1.3 KB
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
52
package GoCryptoTCP
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"math/big"
)
const AesLetterSet = "0123456789abcdefghijklmnopqrstuvwxyz"
var AesKeyLen int = 16
func GenAESKey() []byte {
key := make([]byte, AesKeyLen)
for i := range key {
randNum, _ := rand.Int(rand.Reader, big.NewInt(36))
key[i] = AesLetterSet[randNum.Int64()]
}
return key
}
func AesEncryptCBC(key, plainText []byte) []byte {
block, err := aes.NewCipher(key)
CheckFatalErr(err)
blockMode := cipher.NewCBCEncrypter(block, key[:aes.BlockSize])
padText := PKCS7Padding(plainText, aes.BlockSize)
cipherText := make([]byte, len(padText))
blockMode.CryptBlocks(cipherText, padText)
return cipherText
}
func AesDecryptCBC(key, cipherText []byte) []byte {
block, _ := aes.NewCipher(key)
blockMode := cipher.NewCBCDecrypter(block, key[:aes.BlockSize])
padText := make([]byte, len(cipherText))
blockMode.CryptBlocks(padText, cipherText)
plainText := PKCS7Trimming(padText)
return plainText
}
func PKCS7Padding(cipherText []byte, blockSize int) []byte {
padding := blockSize - len(cipherText)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(cipherText, padText...)
}
func PKCS7Trimming(encrypt []byte) []byte {
padding := encrypt[len(encrypt)-1]
return encrypt[:len(encrypt)-int(padding)]
}