-
Notifications
You must be signed in to change notification settings - Fork 1
/
encrypt.go
64 lines (50 loc) · 1.31 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
_ "github.com/lib/pq"
)
type Encryption struct {
SECRET_KEY string
Bytes []byte
}
func NewEncryption() *Encryption {
return &Encryption{
SECRET_KEY: "Key137&*#!~c(8~=2?.?%b74",
Bytes: []byte{35, 46, 57, 24, 85, 35, 24, 74, 87, 35, 88, 98, 66, 32, 14, 05},
}
}
func (e *Encryption) Encode(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}
func (e *Encryption) Decode(s string) []byte {
data, err := base64.StdEncoding.DecodeString(s)
if err != nil {
panic(err)
}
return data
}
func (e *Encryption) Encrypt(text string) (string, error) {
block, err := aes.NewCipher([]byte(e.SECRET_KEY))
if err != nil {
return "", err
}
plainText := []byte(text)
cfb := cipher.NewCFBEncrypter(block, e.Bytes)
cipherText := make([]byte, len(plainText))
cfb.XORKeyStream(cipherText, plainText)
return e.Encode(cipherText), nil
}
// Decrypt method is to extract back the encrypted text
func (e *Encryption) Decrypt(text string) (string, error) {
block, err := aes.NewCipher([]byte(e.SECRET_KEY))
if err != nil {
return "", err
}
cipherText := e.Decode(text)
cfb := cipher.NewCFBDecrypter(block, e.Bytes)
plainText := make([]byte, len(cipherText))
cfb.XORKeyStream(plainText, cipherText)
return string(plainText), nil
}