-
Notifications
You must be signed in to change notification settings - Fork 4
/
des.go
214 lines (172 loc) · 5.6 KB
/
des.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package xcrypto
import (
"crypto/cipher"
"crypto/des"
"encoding/hex"
"github.com/fufuok/utils"
"github.com/fufuok/utils/base58"
)
// DesCBCEnStringHex 加密, ZerosPadding
func DesCBCEnStringHex(s string, key []byte) string {
return hex.EncodeToString(DesCBCEncrypt(false, utils.S2B(s), key))
}
// DesCBCEnPKCS7StringHex 加密, Pkcs7Padding
func DesCBCEnPKCS7StringHex(s string, key []byte) string {
return hex.EncodeToString(DesCBCEncrypt(true, utils.S2B(s), key))
}
// DesCBCEnHex 加密, ZerosPadding
func DesCBCEnHex(b, key []byte) string {
return hex.EncodeToString(DesCBCEncrypt(false, b, key))
}
// DesCBCEnPKCS7Hex 加密, Pkcs7Padding
func DesCBCEnPKCS7Hex(b, key []byte) string {
return hex.EncodeToString(DesCBCEncrypt(true, b, key))
}
// DesCBCDeStringHex 解密, ZerosPadding
func DesCBCDeStringHex(s string, key []byte) string {
return utils.B2S(DesCBCDeHex(s, key))
}
// DesCBCDePKCS7StringHex 解密, Pkcs7Padding
func DesCBCDePKCS7StringHex(s string, key []byte) string {
return utils.B2S(DesCBCDePKCS7Hex(s, key))
}
// DesCBCDeHex 解密, ZerosPadding
func DesCBCDeHex(s string, key []byte) []byte {
if data, err := hex.DecodeString(s); err == nil {
return DesCBCDecrypt(false, data, key)
}
return nil
}
// DesCBCDePKCS7Hex 解密, Pkcs7Padding
func DesCBCDePKCS7Hex(s string, key []byte) []byte {
if data, err := hex.DecodeString(s); err == nil {
return DesCBCDecrypt(true, data, key)
}
return nil
}
// DesCBCEnStringB58 加密, ZerosPadding
func DesCBCEnStringB58(s string, key []byte) string {
return base58.Encode(DesCBCEncrypt(false, utils.S2B(s), key))
}
// DesCBCEnPKCS7StringB58 加密, Pkcs7Padding
func DesCBCEnPKCS7StringB58(s string, key []byte) string {
return base58.Encode(DesCBCEncrypt(true, utils.S2B(s), key))
}
// DesCBCEnB58 加密, ZerosPadding
func DesCBCEnB58(b, key []byte) string {
return base58.Encode(DesCBCEncrypt(false, b, key))
}
// DesCBCEnPKCS7B58 加密, Pkcs7Padding
func DesCBCEnPKCS7B58(b, key []byte) string {
return base58.Encode(DesCBCEncrypt(true, b, key))
}
// DesCBCDeStringB58 解密, ZerosPadding
func DesCBCDeStringB58(s string, key []byte) string {
return utils.B2S(DesCBCDeB58(s, key))
}
// DesCBCDePKCS7StringB58 解密, Pkcs7Padding
func DesCBCDePKCS7StringB58(s string, key []byte) string {
return utils.B2S(DesCBCDePKCS7B58(s, key))
}
// DesCBCDeB58 解密, ZerosPadding
func DesCBCDeB58(s string, key []byte) []byte {
return DesCBCDecrypt(false, base58.Decode(s), key)
}
// DesCBCDePKCS7B58 解密, Pkcs7Padding
func DesCBCDePKCS7B58(s string, key []byte) []byte {
return DesCBCDecrypt(true, base58.Decode(s), key)
}
// DesCBCEnStringB64 加密, ZerosPadding
func DesCBCEnStringB64(s string, key []byte) string {
return utils.B64UrlEncode(DesCBCEncrypt(false, utils.S2B(s), key))
}
// DesCBCEnPKCS7StringB64 加密, Pkcs7Padding
func DesCBCEnPKCS7StringB64(s string, key []byte) string {
return utils.B64UrlEncode(DesCBCEncrypt(true, utils.S2B(s), key))
}
// DesCBCEnB64 加密, ZerosPadding
func DesCBCEnB64(b, key []byte) string {
return utils.B64UrlEncode(DesCBCEncrypt(false, b, key))
}
// DesCBCEnPKCS7B64 加密, Pkcs7Padding
func DesCBCEnPKCS7B64(b, key []byte) string {
return utils.B64UrlEncode(DesCBCEncrypt(true, b, key))
}
// DesCBCDeStringB64 解密, ZerosPadding
func DesCBCDeStringB64(s string, key []byte) string {
return utils.B2S(DesCBCDeB64(s, key))
}
// DesCBCDePKCS7StringB64 解密, Pkcs7Padding
func DesCBCDePKCS7StringB64(s string, key []byte) string {
return utils.B2S(DesCBCDePKCS7B64(s, key))
}
// DesCBCDeB64 解密, ZerosPadding
func DesCBCDeB64(s string, key []byte) []byte {
return DesCBCDecrypt(false, utils.B64UrlDecode(s), key)
}
// DesCBCDePKCS7B64 解密, Pkcs7Padding
func DesCBCDePKCS7B64(s string, key []byte) []byte {
return DesCBCDecrypt(true, utils.B64UrlDecode(s), key)
}
// DesCBCEncrypt AES-CBC 加密
func DesCBCEncrypt(asPKCS7 bool, plaintext, key []byte, ivs ...[]byte) (ciphertext []byte) {
ciphertext, _ = DesCBCEncryptE(asPKCS7, plaintext, key, ivs...)
return
}
// DesCBCDecrypt AES-CBC 解密
func DesCBCDecrypt(asPKCS7 bool, ciphertext, key []byte, ivs ...[]byte) (plaintext []byte) {
plaintext, _ = DesCBCDecryptE(asPKCS7, ciphertext, key, ivs...)
return
}
// DesCBCEncryptE DES-CBC 加密, 密码分组链接模式 (Cipher Block Chaining (CBC))
// key 长度固定为 8
// asPKCS7: false (ZerosPadding), true (Pkcs7Padding)
func DesCBCEncryptE(asPKCS7 bool, plaintext, key []byte, ivs ...[]byte) ([]byte, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
defer func() {
if r := recover(); r != nil {
return
}
}()
bSize := block.BlockSize()
plaintext = Padding(plaintext, bSize, asPKCS7)
// 向量无效时自动为 key[:blockSize]
var iv []byte
if len(ivs) > 0 && len(ivs[0]) == bSize {
iv = ivs[0]
} else {
iv = key[:bSize]
}
ciphertext := make([]byte, len(plaintext))
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext, plaintext)
return ciphertext, nil
}
// DesCBCDecryptE DES-CBC 解密, 密码分组链接模式 (Cipher Block Chaining (CBC))
func DesCBCDecryptE(asPKCS7 bool, ciphertext, key []byte, ivs ...[]byte) ([]byte, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
defer func() {
if r := recover(); r != nil {
return
}
}()
bSize := block.BlockSize()
// 向量无效时自动为 key[:blockSize]
var iv []byte
if len(ivs) > 0 && len(ivs[0]) == bSize {
iv = ivs[0]
} else {
iv = key[:bSize]
}
plaintext := make([]byte, len(ciphertext))
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(plaintext, ciphertext)
plaintext = UnPadding(plaintext, asPKCS7)
return plaintext, nil
}