-
Notifications
You must be signed in to change notification settings - Fork 3
/
aes_ecb_pkcs5padding.go
executable file
·145 lines (125 loc) · 3.3 KB
/
aes_ecb_pkcs5padding.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
// Package aesKit
/*
AES/ECB/PKCS5Padding
参考:https://www.cnblogs.com/lavin/p/5373188.html
缺点:代码中有 panic
秘钥:长度必须是 16、24、32 中的一个!
*/
package aesKit
import (
"crypto/aes"
"crypto/cipher"
"github.com/richelieu-yang/chimera/v2/src/core/errorKit"
"github.com/richelieu-yang/chimera/v2/src/core/strKit"
"github.com/richelieu-yang/chimera/v2/src/crypto/base64Kit"
)
// EncryptToString 加密流程: 明文 => 密文 => base64字符串
/*
@param plainText 可以为""
*/
func EncryptToString(plainText, key []byte) (string, error) {
cipherText, err := Encrypt(plainText, key)
if err != nil {
return "", err
}
return base64Kit.EncodeToString(cipherText), nil
}
func Encrypt(plainText, key []byte) (cipherText []byte, err error) {
block, err := aes.NewCipher(key)
if err != nil {
err = errorKit.Wrap(err, "key(%s) is invalid", string(key))
return
}
defer func() {
if obj := recover(); obj != nil {
cipherText = nil
err = errorKit.New(strKit.ToString(obj))
}
}()
ecb := NewECBEncrypter(block)
content := PKCS5Padding(plainText, block.BlockSize())
cipherText = make([]byte, len(content))
ecb.CryptBlocks(cipherText, content)
return
}
func DecryptToString(base64Text, key []byte) (string, error) {
cipherText, err := base64Kit.Decode(base64Text)
if err != nil {
return "", err
}
plainText, err := Decrypt(cipherText, key)
if err != nil {
return "", err
}
return string(plainText), nil
}
func Decrypt(cipherText, key []byte) (plainText []byte, err error) {
block, err := aes.NewCipher(key)
if err != nil {
err = errorKit.Wrap(err, "key(%s) is invalid", string(key))
return
}
defer func() {
if obj := recover(); obj != nil {
plainText = nil
err = errorKit.New(strKit.ToString(obj))
}
}()
blockMode := NewECBDecrypter(block)
plainText = make([]byte, len(cipherText))
blockMode.CryptBlocks(plainText, cipherText)
plainText = PKCS5Unpadding(plainText)
return
}
type ecb struct {
b cipher.Block
blockSize int
}
func newECB(b cipher.Block) *ecb {
return &ecb{
b: b,
blockSize: b.BlockSize(),
}
}
type ecbEncrypter ecb
// NewECBEncrypter returns a BlockMode which encrypts in electronic code book
// mode, using the given Block.
func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
return (*ecbEncrypter)(newECB(b))
}
func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
if len(src)%x.blockSize != 0 {
panic("crypto/cipher: input not full blocks")
}
if len(dst) < len(src) {
panic("crypto/cipher: output smaller than input")
}
for len(src) > 0 {
x.b.Encrypt(dst, src[:x.blockSize])
src = src[x.blockSize:]
dst = dst[x.blockSize:]
}
}
type ecbDecrypter ecb
// NewECBDecrypter returns a BlockMode which decrypts in electronic code book
// mode, using the given Block.
func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
return (*ecbDecrypter)(newECB(b))
}
func (x *ecbDecrypter) BlockSize() int {
return x.blockSize
}
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
if len(src)%x.blockSize != 0 {
panic("crypto/cipher: input not full blocks")
}
if len(dst) < len(src) {
panic("crypto/cipher: output smaller than input")
}
for len(src) > 0 {
x.b.Decrypt(dst, src[:x.blockSize])
src = src[x.blockSize:]
dst = dst[x.blockSize:]
}
}