-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnCryptString.go
54 lines (47 loc) · 1.12 KB
/
EnCryptString.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
52
53
54
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
)
func main() {
key := "123456789012345678901234"
Hello := "SanchuV"
bar := Encrypt(key, Hello)
fmt.Println(bar)
fmt.Println(Decrypt(key, bar))
}
var iv = []byte{35, 46, 57, 24, 85, 35, 24, 74, 87, 35, 88, 98, 66, 32, 14, 05}
func encodeBase64(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}
func decodeBase64(s string) []byte {
data, err := base64.StdEncoding.DecodeString(s)
if err != nil {
panic(err)
}
return data
}
func Encrypt(key, text string) string {
block, err := aes.NewCipher([]byte(key))
if err != nil {
panic(err)
}
plaintext := []byte(text)
cfb := cipher.NewCFBEncrypter(block, iv)
cipherText := make([]byte, len(plaintext))
cfb.XORKeyStream(cipherText, plaintext)
return encodeBase64(cipherText)
}
func Decrypt(key, text string) string {
block, err := aes.NewCipher([]byte(key))
if err != nil {
panic(err)
}
cipherText := decodeBase64(text)
cfb := cipher.NewCFBEncrypter(block, iv)
plaintext := make([]byte, len(cipherText))
cfb.XORKeyStream(plaintext, cipherText)
return string(plaintext)
}