-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxrypt.go
executable file
·110 lines (84 loc) · 2.12 KB
/
lxrypt.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// lxcrypt en and decode strings with key generation
package lxcrypt
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
)
// GenerateKey, generate a key with size
func GenerateKey(size uint) ([]byte, error) {
// Error for return
var err error
// Create a key
key := make([]byte, size)
// Check the size
if size == 0 || size > 32 {
return key, fmt.Errorf("key size should be between %d and %d", 1, 32)
}
// Use the crypto random
_, err = rand.Read(key)
// Return hex string from key
return key, err
}
// GetKeyFromFile, get key from file, when not exists create new file with key
func GetKeyFromFile(kfp string) ([]byte, error) {
var (
err error
key []byte
)
// Generate a new key when file not exists
if _, err = os.Stat(kfp); os.IsNotExist(err) {
// Generate key for save in file
key, err = GenerateKey(32)
// When error nil write file
if err == nil {
err = ioutil.WriteFile(kfp, []byte(hex.EncodeToString(key)), 0644)
}
return key, err
}
// Read key from file
dat, err := ioutil.ReadFile(kfp)
// When error nil decode to []byte
if err == nil {
key, err = hex.DecodeString(string(dat))
}
return key, err
}
// EncryptAES, encrypt text with AES cipher
func EncryptAES(key, text []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
b := base64.StdEncoding.EncodeToString(text)
cipherText := make([]byte, aes.BlockSize+len(b))
iv := cipherText[:aes.BlockSize]
_, err = io.ReadFull(rand.Reader, iv)
if err == nil {
cfb := cipher.NewCFBEncrypter(block, iv)
cfb.XORKeyStream(cipherText[aes.BlockSize:], []byte(b))
}
return cipherText, err
}
// DecryptAES, decrypt text with AES cipher
func DecryptAES(key, text []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if len(text) < aes.BlockSize {
return nil, errors.New("text too short")
}
iv := text[:aes.BlockSize]
text = text[aes.BlockSize:]
cfb := cipher.NewCFBDecrypter(block, iv)
cfb.XORKeyStream(text, text)
return base64.StdEncoding.DecodeString(string(text))
}