-
Notifications
You must be signed in to change notification settings - Fork 67
/
cipher.go
336 lines (297 loc) · 8.5 KB
/
cipher.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package dongle
import (
"bytes"
"crypto/cipher"
)
// defines cipher mode enum type.
// 定义分组模式枚举类型
type cipherMode string
// cipher mode constants.
// 分组模式常量
const (
CBC cipherMode = "cbc"
ECB cipherMode = "ecb"
CFB cipherMode = "cfb"
OFB cipherMode = "ofb"
CTR cipherMode = "ctr"
)
// defines cipher padding enum type.
// 定义填充模式枚举类型
type cipherPadding string
// cipher padding constants.
// 填充模式常量
const (
No cipherPadding = "no"
Empty cipherPadding = "empty"
Zero cipherPadding = "zero"
PKCS5 cipherPadding = "pkcs5"
PKCS7 cipherPadding = "pkcs7"
AnsiX923 cipherPadding = "ansi-x-923"
ISO97971 cipherPadding = "iso9797-1"
)
// Cipher defines a Cipher struct.
// 定义 Cipher 结构体
type Cipher struct {
mode cipherMode // 分组模式
padding cipherPadding // 填充模式
key []byte // 密钥
iv []byte // 初始向量
}
// NewCipher returns a new Cipher instance.
// 初始化 Cipher 结构体
func NewCipher() *Cipher {
return &Cipher{
mode: CBC,
padding: PKCS7,
}
}
// SetMode sets mode.
// 设置分组模式
func (c *Cipher) SetMode(mode cipherMode) {
c.mode = mode
}
// SetPadding sets padding.
// 设置填充模式
func (c *Cipher) SetPadding(padding cipherPadding) {
c.padding = padding
}
// SetKey sets key.
// 设置密钥
func (c *Cipher) SetKey(key interface{}) {
c.key = interface2bytes(key)
}
// SetIV sets iv.
// 设置初始向量
func (c *Cipher) SetIV(iv interface{}) {
c.iv = interface2bytes(iv)
}
// EmptyPadding padding with Empty mode.
// 进行空填充
func (c *Cipher) EmptyPadding(src []byte, blockSize int) []byte {
paddingSize := blockSize - len(src)%blockSize
paddingText := bytes.Repeat([]byte(" "), paddingSize)
return append(src, paddingText...)
}
// EmptyUnPadding removes padding with Empty mode.
// 移除空填充
func (c *Cipher) EmptyUnPadding(src []byte) []byte {
return bytes.TrimRight(src, " ")
}
// ZeroPadding padding with Zero mode.
// 进行 0 填充
func (c *Cipher) ZeroPadding(src []byte, blockSize int) []byte {
paddingSize := blockSize - len(src)%blockSize
paddingText := bytes.Repeat([]byte{byte(0)}, paddingSize)
return append(src, paddingText...)
}
// ZeroUnPadding removes padding with Zero mode.
// 移除 0 填充
func (c *Cipher) ZeroUnPadding(src []byte) []byte {
return bytes.TrimRight(src, string([]byte{0}))
}
// ISO97971Padding padding with ISO/IEC 9797-1 mode.
// 进行 ISO/IEC 9797-1 填充
func (c *Cipher) ISO97971Padding(src []byte, blockSize int) []byte {
return c.ZeroPadding(append(src, 0x80), blockSize)
}
// ISO97971UnPadding removes padding with ISO/IEC 9797-1 mode.
// 移除 ISO/IEC 9797-1 填充
func (c *Cipher) ISO97971UnPadding(src []byte) []byte {
dst := c.ZeroUnPadding(src)
return dst[:len(dst)-1]
}
// AnsiX923Padding padding with ANSI X.923 mode.
// 进行 ANSI X.923 填充
func (c *Cipher) AnsiX923Padding(src []byte, blockSize int) []byte {
paddingSize := blockSize - len(src)%blockSize
paddingText := append(bytes.Repeat([]byte{byte(0)}, paddingSize-1), byte(paddingSize))
return append(src, paddingText...)
}
// AnsiX923UnPadding removes padding with ANSI X.923 mode.
// 移除 ANSI X.923 填充
func (c *Cipher) AnsiX923UnPadding(src []byte) []byte {
if len(src) == 0 {
return []byte("")
}
n := len(src) - int(src[len(src)-1])
return src[0:n]
}
// PKCS5Padding padding with PKCS5 mode.
// 进行 PKCS5 填充
func (c *Cipher) PKCS5Padding(src []byte) []byte {
return c.PKCS7Padding(src, 16)
}
// PKCS5UnPadding removes padding with PKCS5 mode.
// 移除 PKCS5 填充
func (c *Cipher) PKCS5UnPadding(src []byte) []byte {
return c.PKCS7UnPadding(src)
}
// PKCS7Padding padding with PKCS7 mode.
// 进行 PKCS7 填充
func (c *Cipher) PKCS7Padding(src []byte, blockSize int) []byte {
paddingSize := blockSize - len(src)%blockSize
paddingText := bytes.Repeat([]byte{byte(paddingSize)}, paddingSize)
return append(src, paddingText...)
}
// PKCS7UnPadding removes padding with PKCS7 mode.
// 移除 PKCS7 填充
func (c *Cipher) PKCS7UnPadding(src []byte) []byte {
if len(src) == 0 {
return []byte("")
}
n := len(src) - int(src[len(src)-1])
return src[0:n]
}
// NewCBCEncrypter encrypts with CBC mode.
// CBC 模式加密
func (c *Cipher) NewCBCEncrypter(src []byte, block cipher.Block) (dst []byte) {
dst = make([]byte, len(src))
cipher.NewCBCEncrypter(block, c.iv).CryptBlocks(dst, src)
return
}
// NewCBCDecrypter decrypts with CBC mode.
// CBC 模式解密
func (c *Cipher) NewCBCDecrypter(src []byte, block cipher.Block) (dst []byte) {
dst = make([]byte, len(src))
cipher.NewCBCDecrypter(block, c.iv).CryptBlocks(dst, src)
return
}
// NewCFBEncrypter encrypts with CFB mode.
// CFB 模式加密
func (c *Cipher) NewCFBEncrypter(src []byte, block cipher.Block) (dst []byte) {
dst = make([]byte, len(src))
cipher.NewCFBEncrypter(block, c.iv).XORKeyStream(dst, src)
return
}
// NewCFBDecrypter decrypts with CFB mode.
// CFB 模式解密
func (c *Cipher) NewCFBDecrypter(src []byte, block cipher.Block) (dst []byte) {
dst = make([]byte, len(src))
cipher.NewCFBDecrypter(block, c.iv).XORKeyStream(dst, src)
return
}
// NewCTREncrypter encrypts with CTR mode.
// CTR 模式加密
func (c *Cipher) NewCTREncrypter(src []byte, block cipher.Block) (dst []byte) {
dst = make([]byte, len(src))
cipher.NewCTR(block, c.iv).XORKeyStream(dst, src)
return
}
// NewCTRDecrypter decrypts with CTR mode.
// CTR 模式解密
func (c *Cipher) NewCTRDecrypter(src []byte, block cipher.Block) (dst []byte) {
dst = make([]byte, len(src))
cipher.NewCTR(block, c.iv).XORKeyStream(dst, src)
return
}
// NewECBEncrypter encrypts with ECB mode.
// ECB 模式加密
func (c *Cipher) NewECBEncrypter(src []byte, block cipher.Block) (dst []byte) {
dst = make([]byte, len(src))
encrypted, blockSize := dst, block.BlockSize()
for len(src) > 0 {
block.Encrypt(encrypted, src[:blockSize])
src = src[blockSize:]
encrypted = encrypted[blockSize:]
}
return
}
// NewECBDecrypter decrypts with ECB mode.
// ECB 模式解密
func (c *Cipher) NewECBDecrypter(src []byte, block cipher.Block) (dst []byte) {
dst = make([]byte, len(src))
decrypted, blockSize := dst, block.BlockSize()
for len(src) > 0 {
block.Decrypt(decrypted, src[:blockSize])
src = src[blockSize:]
decrypted = decrypted[blockSize:]
}
return
}
// NewOFBEncrypter encrypts with OFB mode.
// OFB 模式加密
func (c *Cipher) NewOFBEncrypter(src []byte, block cipher.Block) (dst []byte) {
dst = make([]byte, len(src))
cipher.NewOFB(block, c.iv[:block.BlockSize()]).XORKeyStream(dst, src)
return
}
// NewOFBDecrypter decrypts with OFB mode.
// OFB 模式解密
func (c *Cipher) NewOFBDecrypter(src []byte, block cipher.Block) (dst []byte) {
dst = make([]byte, len(src))
cipher.NewOFB(block, c.iv[:block.BlockSize()]).XORKeyStream(dst, src)
return
}
// Encrypt encrypts with given mode and padding
// 根据指定的分组模式和填充模式进行加密
func (c *Cipher) Encrypt(src []byte, block cipher.Block) (dst []byte, err error) {
mode, padding, size := c.mode, c.padding, block.BlockSize()
dst = []byte("")
if len(src) == 0 {
return
}
switch padding {
case No:
case Empty:
src = c.EmptyPadding(src, size)
case Zero:
src = c.ZeroPadding(src, size)
case PKCS5:
src = c.PKCS5Padding(src)
case PKCS7:
src = c.PKCS7Padding(src, size)
case AnsiX923:
src = c.AnsiX923Padding(src, size)
case ISO97971:
src = c.ISO97971Padding(src, size)
}
switch mode {
case ECB:
dst = c.NewECBEncrypter(src, block)
case CBC:
dst = c.NewCBCEncrypter(src, block)
case CTR:
dst = c.NewCTREncrypter(src, block)
case CFB:
dst = c.NewCFBEncrypter(src, block)
case OFB:
dst = c.NewOFBEncrypter(src, block)
}
return
}
// Decrypt decrypts with given mode and padding.
// 根据指定的分组模式和填充模式进行解密
func (c *Cipher) Decrypt(src []byte, block cipher.Block) (dst []byte, err error) {
mode, padding := c.mode, c.padding
dst = []byte("")
if len(src) == 0 {
return
}
switch mode {
case ECB:
src = c.NewECBDecrypter(src, block)
case CBC:
src = c.NewCBCDecrypter(src, block)
case CTR:
src = c.NewCTRDecrypter(src, block)
case CFB:
src = c.NewCFBDecrypter(src, block)
case OFB:
src = c.NewOFBDecrypter(src, block)
}
switch padding {
case Zero:
return c.ZeroUnPadding(src), nil
case Empty:
return c.EmptyUnPadding(src), nil
case PKCS5:
return c.PKCS5UnPadding(src), nil
case PKCS7:
return c.PKCS7UnPadding(src), nil
case AnsiX923:
return c.AnsiX923UnPadding(src), nil
case ISO97971:
return c.ISO97971UnPadding(src), nil
}
return src, nil
}