forked from Peony2022/shiro_killer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.go
38 lines (33 loc) · 1.02 KB
/
encrypt.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
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"io"
uuid "github.com/satori/go.uuid"
)
func Padding(plainText []byte, blockSize int) []byte {
n := blockSize - len(plainText)%blockSize
temp := bytes.Repeat([]byte{byte(n)}, n)
plainText = append(plainText, temp...)
return plainText
}
func AesCbcEncrypt(key []byte, Content []byte) string {
block, _ := aes.NewCipher(key)
Content = Padding(Content, block.BlockSize())
iv := uuid.NewV4().Bytes()
blockMode := cipher.NewCBCEncrypter(block, iv)
cipherText := make([]byte, len(Content))
blockMode.CryptBlocks(cipherText, Content)
return base64.StdEncoding.EncodeToString(append(iv[:], cipherText[:]...))
}
func AesGcmEncrypt(key []byte, Content []byte) string {
block, _ := aes.NewCipher(key)
nonce := make([]byte, 16)
io.ReadFull(rand.Reader, nonce)
aesgcm, _ := cipher.NewGCMWithNonceSize(block, 16)
ciphertext := aesgcm.Seal(nil, nonce, Content, nil)
return base64.StdEncoding.EncodeToString(append(nonce, ciphertext...))
}