Skip to content

Commit 19021da

Browse files
committed
修复aes中base64解析错误问题
1 parent 642055a commit 19021da

File tree

2 files changed

+57
-85
lines changed

2 files changed

+57
-85
lines changed

util/aes/ecb.go

Lines changed: 48 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,108 +5,78 @@ import (
55
"crypto/aes"
66
"crypto/cipher"
77
"encoding/base64"
8+
"fmt"
9+
"strings"
810

911
"github.com/juju/errors"
1012
)
1113

12-
//Decrypt 解密.
13-
func Decrypt(crypted string, key []byte) ([]byte, error) {
14-
raw, err := base64.StdEncoding.DecodeString(crypted)
15-
if err != nil {
16-
return nil, errors.Trace(err)
14+
func byteKey(key string) []byte {
15+
for i := 16; i < 33; i += 8 {
16+
key += strings.Repeat("\x00", i-len(key))
17+
if len(key) == i {
18+
break
19+
}
1720
}
21+
return []byte(key)
22+
}
1823

19-
block, err := aes.NewCipher(key)
24+
// Encrypt aes 加密.
25+
func Encrypt(encodeStr string, key string) (string, error) {
26+
encodeBytes := []byte(encodeStr)
27+
encodeKey := byteKey(key)
28+
block, err := aes.NewCipher(encodeKey)
2029
if err != nil {
21-
return nil, errors.Trace(err)
30+
return "", errors.Trace(err)
2231
}
32+
blockSize := block.BlockSize()
33+
encodeBytes = pkcs5Padding(encodeBytes, blockSize)
34+
blockMode := cipher.NewCBCEncrypter(block, encodeKey[:blockSize])
35+
crypted := make([]byte, len(encodeBytes))
36+
blockMode.CryptBlocks(crypted, encodeBytes)
2337

24-
blockMode := NewECBDecrypter(block)
25-
data := make([]byte, len(raw))
26-
blockMode.CryptBlocks(data, raw)
27-
data = pkcs5UnPadding(data)
28-
29-
return data, nil
38+
return base64.URLEncoding.EncodeToString(crypted), nil
3039
}
3140

32-
//Encrypt 加密.
33-
func Encrypt(src string, key []byte) (string, error) {
34-
block, err := aes.NewCipher(key)
41+
// Decrypt aes 解密.
42+
func Decrypt(decodeStr string, key string) (string, error) {
43+
decodeKey := byteKey(key)
44+
decodeBytes, err := base64.URLEncoding.DecodeString(decodeStr)
3545
if err != nil {
36-
return "", err
46+
return "", errors.Trace(err)
47+
}
48+
block, err := aes.NewCipher(decodeKey)
49+
if err != nil {
50+
return "", errors.Trace(err)
51+
}
52+
blockSize := block.BlockSize()
53+
blockMode := cipher.NewCBCDecrypter(block, decodeKey[:blockSize])
54+
origData := make([]byte, len(decodeBytes))
55+
fmt.Printf("ori:%d, dec:%d\n", len(origData), len(decodeBytes))
56+
blockMode.CryptBlocks(origData, decodeBytes)
57+
origData, err = pkcs5UnPadding(origData)
58+
if err != nil {
59+
return "", errors.Trace(err)
3760
}
3861

39-
ecb := NewECBEncrypter(block)
40-
content := pkcs5Padding([]byte(src), block.BlockSize())
41-
buf := make([]byte, len(content))
42-
ecb.CryptBlocks(buf, content)
43-
44-
return base64.StdEncoding.EncodeToString(buf), nil
62+
return string(origData), nil
4563
}
4664

65+
// pkcs5Padding pkcs5 添加数据.
4766
func pkcs5Padding(ciphertext []byte, blockSize int) []byte {
4867
padding := blockSize - len(ciphertext)%blockSize
4968
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
5069
return append(ciphertext, padtext...)
5170
}
5271

53-
func pkcs5UnPadding(origData []byte) []byte {
72+
// pkcs5UnPadding pkcs5 删除数据.
73+
func pkcs5UnPadding(origData []byte) ([]byte, error) {
5474
length := len(origData)
55-
// 去掉最后一个字节 unpadding 次
56-
unpadding := int(origData[length-1])
57-
return origData[:(length - unpadding)]
58-
}
59-
60-
type ecb struct {
61-
b cipher.Block
62-
blockSize int
63-
}
64-
65-
func newECB(b cipher.Block) *ecb {
66-
return &ecb{
67-
b: b,
68-
blockSize: b.BlockSize(),
69-
}
70-
}
7175

72-
type ecbEncrypter ecb
73-
74-
// NewECBEncrypter returns a BlockMode which encrypts in electronic code book mode, using the given Block.
75-
func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
76-
return (*ecbEncrypter)(newECB(b))
77-
}
78-
func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
79-
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
80-
if len(src)%x.blockSize != 0 {
81-
panic("crypto/cipher: input not full blocks")
76+
if length <= 0 {
77+
return nil, errors.New("pkcs5Padding len(origData) <= 0 error")
8278
}
83-
if len(dst) < len(src) {
84-
panic("crypto/cipher: output smaller than input")
85-
}
86-
for len(src) > 0 {
87-
x.b.Encrypt(dst, src[:x.blockSize])
88-
src = src[x.blockSize:]
89-
dst = dst[x.blockSize:]
90-
}
91-
}
9279

93-
type ecbDecrypter ecb
94-
95-
// NewECBDecrypter returns a BlockMode which decrypts in electronic code book mode, using the given Block.
96-
func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
97-
return (*ecbDecrypter)(newECB(b))
98-
}
99-
func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
100-
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
101-
if len(src)%x.blockSize != 0 {
102-
panic("crypto/cipher: input not full blocks")
103-
}
104-
if len(dst) < len(src) {
105-
panic("crypto/cipher: output smaller than input")
106-
}
107-
for len(src) > 0 {
108-
x.b.Decrypt(dst, src[:x.blockSize])
109-
src = src[x.blockSize:]
110-
dst = dst[x.blockSize:]
111-
}
80+
unpadding := int(origData[length-1])
81+
return origData[:(length - unpadding)], nil
11282
}

util/aes/ecb_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
package aes
22

33
import (
4+
"github.com/juju/errors"
45
"testing"
56
)
67

78
func TestAes(t *testing.T) {
89
src := "test"
9-
key := []byte("dbsjdcom\x00\x00\x00\x00\x00\x00\x00\x00")
10+
key := "1qaz@WSX"
1011

1112
crypted, err := Encrypt(src, key)
1213
if err != nil {
13-
t.Fatalf("AesEncrypt error:%s", err.Error())
14+
t.Fatalf("AesEncrypt error:%s", errors.ErrorStack(err))
1415
}
15-
deSrc, err := Decrypt(crypted, []byte(key))
16+
t.Logf("dest:%v", crypted)
17+
deSrc, err := Decrypt(crypted, key)
1618
if err != nil {
1719
t.Fatalf("AesDecrypt error:%s", err.Error())
1820
}
19-
t.Logf("desc:%v", deSrc)
21+
t.Logf("desc:%s", deSrc)
2022
}
2123

2224
func TestDesPanic(t *testing.T) {
23-
key := []byte("dbsjdcom\x00\x00\x00\x00\x00\x00\x00\x00")
24-
crypted := "fDmxIdK9p3oEyQoL1Bwz4Fakia3Y4Qn1SF8podapMFU="
25-
deSrc, err := Decrypt(crypted, []byte(key))
25+
key := "1qaz@WSX"
26+
crypted := "hX0Y5u-EkggQstomvCXgQw=="
27+
deSrc, err := Decrypt(crypted, key)
2628
if err != nil {
2729
t.Logf("err:%v", err)
2830
}

0 commit comments

Comments
 (0)