-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathecb_test.go
62 lines (57 loc) · 1.6 KB
/
ecb_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
51
52
53
54
55
56
57
58
59
60
61
62
package ecb
import (
"encoding/base64"
"testing"
)
type encryptionTest struct {
plaintext string //原始文本
key string //加密Key
encrypted string //加密后的Base64结果字符串
}
var tests = []encryptionTest{
{"hello", "0123456789abcdef", "Z0x+8454yr2c7JwSWCOmOQ=="},
{"hello world", "0123456789abcdef", "gWm+1O9JqIdFWcWyANqt5w=="},
{"this is a long plaintext just full of [47]byte", "0123456789abcdef", "wz1xzURMAtqs4+XRwt+WwPPQnJFPrhvvwKCLcG0uOU0m9kNYO3WtUXigVLKbPAlh"},
{"this is a long plaintext just full of [47]byte", "here is a random key of 32 bytes", "8EgQ3BMV15kUVhaOpR76FDb6HheuBH6+wV+vfaM7SHVvQM0ntLZhD4Np2MMi04ST"},
}
func TestAesEncrypt(t *testing.T) {
for _, pair := range tests {
plaintext := PKCS7Padding([]byte(pair.plaintext), 16) //BlockSize为16
encrypted,err := AesEncrypt(plaintext, []byte(pair.key))
if err != nil {
t.Error(err.Error())
}
str := base64.StdEncoding.EncodeToString(encrypted)
if pair.encrypted != str {
t.Error(
"for: ", pair.plaintext,
"expected: ", pair.encrypted,
"got: ", str,
)
}
}
}
func TestAesDecrypt(t *testing.T) {
for _, pair := range tests {
str, err := base64.StdEncoding.DecodeString(pair.encrypted)
if err != nil {
t.Error(
"For: ", pair.encrypted,
"base64Decode Err: ", err,
)
} else {
dectypted,err := AesDecrypt(str, []byte(pair.key))
if err != nil {
t.Error(err.Error())
}
dectypted = PKCS7UnPadding(dectypted)
if pair.plaintext != string(dectypted) {
t.Error(
"For: ", pair.encrypted,
"Expected: ", pair.plaintext,
"Got: ", dectypted,
)
}
}
}
}