-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
gdes.go
301 lines (248 loc) · 8.17 KB
/
gdes.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package gdes provides useful API for DES encryption/decryption algorithms.
package gdes
import (
"bytes"
"crypto/cipher"
"crypto/des"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
)
const (
NOPADDING = iota
PKCS5PADDING
)
// EncryptECB encrypts `plainText` using ECB mode.
func EncryptECB(plainText []byte, key []byte, padding int) ([]byte, error) {
text, err := Padding(plainText, padding)
if err != nil {
return nil, err
}
cipherText := make([]byte, len(text))
block, err := des.NewCipher(key)
if err != nil {
err = gerror.WrapCodef(gcode.CodeInvalidParameter, err, `des.NewCipher failed for key "%s"`, key)
return nil, err
}
blockSize := block.BlockSize()
for i, count := 0, len(text)/blockSize; i < count; i++ {
begin, end := i*blockSize, i*blockSize+blockSize
block.Encrypt(cipherText[begin:end], text[begin:end])
}
return cipherText, nil
}
// DecryptECB decrypts `cipherText` using ECB mode.
func DecryptECB(cipherText []byte, key []byte, padding int) ([]byte, error) {
text := make([]byte, len(cipherText))
block, err := des.NewCipher(key)
if err != nil {
err = gerror.WrapCodef(gcode.CodeInvalidParameter, err, `des.NewCipher failed for key "%s"`, key)
return nil, err
}
blockSize := block.BlockSize()
for i, count := 0, len(text)/blockSize; i < count; i++ {
begin, end := i*blockSize, i*blockSize+blockSize
block.Decrypt(text[begin:end], cipherText[begin:end])
}
plainText, err := UnPadding(text, padding)
if err != nil {
return nil, err
}
return plainText, nil
}
// EncryptECBTriple encrypts `plainText` using TripleDES and ECB mode.
// The length of the `key` should be either 16 or 24 bytes.
func EncryptECBTriple(plainText []byte, key []byte, padding int) ([]byte, error) {
if len(key) != 16 && len(key) != 24 {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "key length error")
}
text, err := Padding(plainText, padding)
if err != nil {
return nil, err
}
var newKey []byte
if len(key) == 16 {
newKey = append([]byte{}, key...)
newKey = append(newKey, key[:8]...)
} else {
newKey = append([]byte{}, key...)
}
block, err := des.NewTripleDESCipher(newKey)
if err != nil {
err = gerror.WrapCodef(gcode.CodeInvalidParameter, err, `des.NewTripleDESCipher failed for key "%s"`, newKey)
return nil, err
}
blockSize := block.BlockSize()
cipherText := make([]byte, len(text))
for i, count := 0, len(text)/blockSize; i < count; i++ {
begin, end := i*blockSize, i*blockSize+blockSize
block.Encrypt(cipherText[begin:end], text[begin:end])
}
return cipherText, nil
}
// DecryptECBTriple decrypts `cipherText` using TripleDES and ECB mode.
// The length of the `key` should be either 16 or 24 bytes.
func DecryptECBTriple(cipherText []byte, key []byte, padding int) ([]byte, error) {
if len(key) != 16 && len(key) != 24 {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "key length error")
}
var newKey []byte
if len(key) == 16 {
newKey = append([]byte{}, key...)
newKey = append(newKey, key[:8]...)
} else {
newKey = append([]byte{}, key...)
}
block, err := des.NewTripleDESCipher(newKey)
if err != nil {
err = gerror.WrapCodef(gcode.CodeInvalidParameter, err, `des.NewTripleDESCipher failed for key "%s"`, newKey)
return nil, err
}
blockSize := block.BlockSize()
text := make([]byte, len(cipherText))
for i, count := 0, len(text)/blockSize; i < count; i++ {
begin, end := i*blockSize, i*blockSize+blockSize
block.Decrypt(text[begin:end], cipherText[begin:end])
}
plainText, err := UnPadding(text, padding)
if err != nil {
return nil, err
}
return plainText, nil
}
// EncryptCBC encrypts `plainText` using CBC mode.
func EncryptCBC(plainText []byte, key []byte, iv []byte, padding int) ([]byte, error) {
block, err := des.NewCipher(key)
if err != nil {
err = gerror.WrapCodef(gcode.CodeInvalidParameter, err, `des.NewCipher failed for key "%s"`, key)
return nil, err
}
if len(iv) != block.BlockSize() {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "invalid iv length")
}
text, err := Padding(plainText, padding)
if err != nil {
return nil, err
}
cipherText := make([]byte, len(text))
encryptor := cipher.NewCBCEncrypter(block, iv)
encryptor.CryptBlocks(cipherText, text)
return cipherText, nil
}
// DecryptCBC decrypts `cipherText` using CBC mode.
func DecryptCBC(cipherText []byte, key []byte, iv []byte, padding int) ([]byte, error) {
block, err := des.NewCipher(key)
if err != nil {
err = gerror.WrapCodef(gcode.CodeInvalidParameter, err, `des.NewCipher failed for key "%s"`, key)
return nil, err
}
if len(iv) != block.BlockSize() {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "iv length invalid")
}
text := make([]byte, len(cipherText))
decrypter := cipher.NewCBCDecrypter(block, iv)
decrypter.CryptBlocks(text, cipherText)
plainText, err := UnPadding(text, padding)
if err != nil {
return nil, err
}
return plainText, nil
}
// EncryptCBCTriple encrypts `plainText` using TripleDES and CBC mode.
func EncryptCBCTriple(plainText []byte, key []byte, iv []byte, padding int) ([]byte, error) {
if len(key) != 16 && len(key) != 24 {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "key length invalid")
}
var newKey []byte
if len(key) == 16 {
newKey = append([]byte{}, key...)
newKey = append(newKey, key[:8]...)
} else {
newKey = append([]byte{}, key...)
}
block, err := des.NewTripleDESCipher(newKey)
if err != nil {
err = gerror.WrapCodef(gcode.CodeInvalidParameter, err, `des.NewTripleDESCipher failed for key "%s"`, newKey)
return nil, err
}
if len(iv) != block.BlockSize() {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "invalid iv length")
}
text, err := Padding(plainText, padding)
if err != nil {
return nil, err
}
cipherText := make([]byte, len(text))
encrypter := cipher.NewCBCEncrypter(block, iv)
encrypter.CryptBlocks(cipherText, text)
return cipherText, nil
}
// DecryptCBCTriple decrypts `cipherText` using TripleDES and CBC mode.
func DecryptCBCTriple(cipherText []byte, key []byte, iv []byte, padding int) ([]byte, error) {
if len(key) != 16 && len(key) != 24 {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "key length invalid")
}
var newKey []byte
if len(key) == 16 {
newKey = append([]byte{}, key...)
newKey = append(newKey, key[:8]...)
} else {
newKey = append([]byte{}, key...)
}
block, err := des.NewTripleDESCipher(newKey)
if err != nil {
err = gerror.WrapCodef(gcode.CodeInvalidParameter, err, `des.NewTripleDESCipher failed for key "%s"`, newKey)
return nil, err
}
if len(iv) != block.BlockSize() {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "invalid iv length")
}
text := make([]byte, len(cipherText))
decrypter := cipher.NewCBCDecrypter(block, iv)
decrypter.CryptBlocks(text, cipherText)
plainText, err := UnPadding(text, padding)
if err != nil {
return nil, err
}
return plainText, nil
}
func PaddingPKCS5(text []byte, blockSize int) []byte {
padding := blockSize - len(text)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(text, padText...)
}
func UnPaddingPKCS5(text []byte) []byte {
length := len(text)
padText := int(text[length-1])
return text[:(length - padText)]
}
func Padding(text []byte, padding int) ([]byte, error) {
switch padding {
case NOPADDING:
if len(text)%8 != 0 {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "invalid text length")
}
case PKCS5PADDING:
return PaddingPKCS5(text, 8), nil
default:
return nil, gerror.NewCodef(gcode.CodeInvalidParameter, `unsupported padding type "%d"`, padding)
}
return text, nil
}
func UnPadding(text []byte, padding int) ([]byte, error) {
switch padding {
case NOPADDING:
if len(text)%8 != 0 {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "invalid text length")
}
case PKCS5PADDING:
return UnPaddingPKCS5(text), nil
default:
return nil, gerror.NewCodef(gcode.CodeInvalidParameter, `unsupported padding type "%d"`, padding)
}
return text, nil
}