This repository has been archived by the owner on May 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common.go
135 lines (112 loc) · 2.76 KB
/
Common.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package goToolSecret
import (
"bytes"
"encoding/base64"
"encoding/hex"
"errors"
"github.com/Deansquirrel/goToolCommon"
)
//将明文加密为Base64格式的密文
//plainText原文
//key密码
func EncryptToBase64Format(plainText string, key string) (string, error) {
sPlain, err := goToolCommon.EncodeGB18030([]byte(key + plainText))
if err != nil {
return "", err
}
sMD5 := goToolCommon.Md5(sPlain)
var bufferResult bytes.Buffer
{
r, err := hex.DecodeString(sMD5)
if err != nil {
return "", err
}
_, err = bufferResult.Write(r)
if err != nil {
return "", err
}
_, err = bufferResult.Write(sPlain)
if err != nil {
return "", err
}
//_, err = bufferResult.Write([]byte(key))
//if err != nil {
// return "", err
//}
//_, err = bufferResult.Write([]byte(plainText))
//if err != nil {
// return "", err
//}
}
resultByte := bufferResult.Bytes()
keyByte := []byte(key)
for index := range resultByte {
resultByte[index] = resultByte[index] ^ (keyByte[index%len(keyByte)])
}
var rndKey [4]byte
for i := 0; i < 4; i++ {
rndKey[i] = uint8(goToolCommon.RandInt(0, 64))
}
for index := range resultByte {
resultByte[index] = resultByte[index] ^ (rndKey[index%4])
}
var bufferFinally bytes.Buffer
{
_, err := bufferFinally.Write(rndKey[0:])
if err != nil {
return "", err
}
_, err = bufferFinally.Write(resultByte)
if err != nil {
return "", err
}
}
return base64.StdEncoding.EncodeToString(bufferFinally.Bytes()), nil
}
//将Base64格式的密文解密
//cipherText密文
//key密码
func DecryptFromBase64Format(cipherText string, key string) (string, error) {
byteCipherText, err := base64.StdEncoding.DecodeString(cipherText)
if err != nil {
//base64解码失败
return "", err
}
rndKey := byteCipherText[0:4]
resultByte := byteCipherText[4:]
for index := range resultByte {
resultByte[index] = resultByte[index] ^ (rndKey[index%4])
}
keyByte := []byte(key)
for index := range resultByte {
resultByte[index] = resultByte[index] ^ (keyByte[index%len(keyByte)])
}
checkKeyByte := resultByte[16 : 16+len(keyByte)]
if !bytes.Equal(keyByte, checkKeyByte) {
return "", errors.New("解密失败。(密码非法)")
}
sMD5Check := resultByte[0:16]
plainByte := resultByte[16+len(keyByte):]
var bufferTemp bytes.Buffer
_, err = bufferTemp.Write(keyByte)
if err != nil {
return "", err
}
_, err = bufferTemp.Write(plainByte)
if err != nil {
return "", err
}
sMD5 := goToolCommon.Md5(bufferTemp.Bytes())
r, err := hex.DecodeString(sMD5)
if err != nil {
return "", err
}
if !bytes.Equal(sMD5Check, r) {
return "", errors.New("解密失败。(校验错误)")
}
rByte, err := goToolCommon.DecodeGB18030(plainByte)
if err != nil {
return "", err
}
return string(rByte), nil
}