-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcryto.go
More file actions
54 lines (47 loc) · 1.18 KB
/
cryto.go
File metadata and controls
54 lines (47 loc) · 1.18 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
53
54
// cryto
package utils
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
)
func CheckSum(msg []byte) uint16 {
sum := 0
for n := 0; n < len(msg); n++ {
sum += int(msg[n])
}
//sum = (sum >> 16) + (sum & 0xffff)
//sum += (sum >> 16)
return uint16(^sum)
}
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
}
var aes_cfbiv = []byte{34, 35, 35, 57, 68, 4, 35, 36, 7, 8, 35, 23, 35, 86, 35, 23}
func AesEncrypt(srctext, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
cfb := cipher.NewCFBEncrypter(block, aes_cfbiv)
ciphertext := make([]byte, len(srctext))
cfb.XORKeyStream(ciphertext, srctext)
return ciphertext, nil
}
func AesDecrypt(ciphertext, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
cfb := cipher.NewCFBDecrypter(block, aes_cfbiv)
plaintext := make([]byte, len(ciphertext))
cfb.XORKeyStream(plaintext, ciphertext)
return plaintext, nil
}