/
encrypt_special.go
175 lines (134 loc) · 3.76 KB
/
encrypt_special.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
package crypto
import (
"io"
"errors"
"crypto/aes"
"crypto/rand"
"crypto/cipher"
)
var (
AesCFB = TypeMultiple.Generate()
AesECB = TypeMultiple.Generate()
)
func init() {
TypeMultiple.Names().Add(AesCFB, func() string {
return "AesCFB"
})
TypeMultiple.Names().Add(AesECB, func() string {
return "AesECB"
})
}
type EncryptAesCFB struct {}
// 加密
// Encrypt
func (this EncryptAesCFB) Encrypt(origData []byte, opt IOption) ([]byte, error) {
key := opt.Key()
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
encrypted := make([]byte, aes.BlockSize + len(origData))
iv := encrypted[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(encrypted[aes.BlockSize:], origData)
return encrypted, nil
}
// 解密
// Decrypt
func (this EncryptAesCFB) Decrypt(encrypted []byte, opt IOption) ([]byte, error) {
key := opt.Key()
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if len(encrypted) < aes.BlockSize {
err := errors.New("ciphertext too short")
return nil, err
}
iv := encrypted[:aes.BlockSize]
encoded := encrypted[aes.BlockSize:]
dst := make([]byte, len(encoded))
cipher.NewCFBDecrypter(block, iv).XORKeyStream(dst, encoded)
return dst, nil
}
func init() {
UseEncrypt.Add(AesCFB, func() IEncrypt {
return EncryptAesCFB{}
})
}
// 特殊的 AesCFB 组合模式
// 也可以使用: MultipleBy(AesCFB)
// AesCFB Encrypt type
// and can use MultipleBy(AesCFB)
func (this Cryptobin) AesCFB() Cryptobin {
this.multiple = AesCFB
return this
}
// ===================
type EncryptAesECB struct {}
func (this EncryptAesECB) Encrypt(origData []byte, opt IOption) ([]byte, error) {
key := opt.Key()
cipher, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
length := (len(origData) + aes.BlockSize) / aes.BlockSize
plain := make([]byte, length*aes.BlockSize)
copy(plain, origData)
pad := byte(len(plain) - len(origData))
for i := len(origData); i < len(plain); i++ {
plain[i] = pad
}
encrypted := make([]byte, len(plain))
// 分组分块加密
for bs, be := 0, cipher.BlockSize(); bs <= len(origData); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
}
return encrypted, nil
}
func (this EncryptAesECB) Decrypt(encrypted []byte, opt IOption) ([]byte, error) {
key := opt.Key()
cipher, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
decrypted := make([]byte, len(encrypted))
// 分组分块解密
for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
}
trim := 0
if len(decrypted) > 0 {
trim = len(decrypted) - int(decrypted[len(decrypted)-1])
}
return decrypted[:trim], nil
}
func init() {
UseEncrypt.Add(AesECB, func() IEncrypt {
return EncryptAesECB{}
})
}
// 特殊的 AesECB 组合模式
// 也可以使用: MultipleBy(AesECB)
// AesECB Encrypt type
// and can use MultipleBy(AesECB)
func (this Cryptobin) AesECB() Cryptobin {
this.multiple = AesECB
return this
}
// ===================
// AesECB key 处理
// AesECB Generate Key
func AesECBGenerateKey(key []byte) (genKey []byte) {
genKey = make([]byte, 16)
copy(genKey, key)
for i := 16; i < len(key); {
for j := 0; j < 16 && i < len(key); j, i = j+1, i+1 {
genKey[j] ^= key[i]
}
}
return genKey
}