-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathaescbc_test.go
50 lines (40 loc) · 1.01 KB
/
aescbc_test.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
package weixin
import (
"crypto/aes"
"encoding/base64"
"testing"
)
func TestAESCBCDecrypt(t *testing.T) {
aesKey := []byte("0123456789abcdef0123456789abcdef")
src := "我们都是好孩子"
b64Enc := "MTp5u8m7i4zMqJFlSo1QBIUn+iASUNmd+Co9u0y4Y5w="
enc, _ := base64.StdEncoding.DecodeString(b64Enc)
actual, err := AESCBCDecrypt(enc, aesKey, aesKey[:aes.BlockSize])
if err != nil {
t.Log(err)
t.FailNow()
}
if string(actual) != src {
t.Logf("expect %s, but get %s", src, actual)
t.FailNow()
}
}
func TestAESCBCEncryptAndDecrypt(t *testing.T) {
aesKey := []byte("0123456789abcdef0123456789abcdef")
src := "我们都是好孩子"
enc, err := AESCBCEncrypt([]byte(src), aesKey, aesKey[:aes.BlockSize])
if err != nil {
t.Log(err)
t.FailNow()
}
t.Logf("%s", base64.StdEncoding.EncodeToString(enc))
actual, err := AESCBCDecrypt(enc, aesKey, aesKey[:aes.BlockSize])
if err != nil {
t.Log(err)
t.FailNow()
}
if string(actual) != src {
t.Logf("expect %s, but get %s", src, actual)
t.FailNow()
}
}