forked from forgoer/openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes_test.go
executable file
·96 lines (80 loc) · 2.95 KB
/
aes_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
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
package openssl
import (
"testing"
"github.com/stretchr/testify/assert"
"encoding/base64"
)
func TestAes(t *testing.T) {
src := []byte("1234567890_QWERTY")
key := []byte("12345123451234512345123451234512")
dst, err := AesECBEncrypt(src, key, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(base64.StdEncoding.EncodeToString(dst))
assert.Equal(t, base64.StdEncoding.EncodeToString(dst), "MdzAshBM1s7uxcblqWIyTY0s2tcEpGg+OwaQzHMd45o=")
}
func TestAesEncrypt(t *testing.T) {
src := []byte("123456")
// AES-128-ECB, PKCS7_PADDING
key := []byte("1234512345123451")
dst, err := AesECBEncrypt(src, key, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(base64.StdEncoding.EncodeToString(dst))
assert.Equal(t, base64.StdEncoding.EncodeToString(dst), "SpfAShHImQhWjd/21Pgz2Q==")
// AES-192-ECB, PKCS7_PADDING
key = []byte("123451234512345123451234")
dst, err = AesECBEncrypt(src, key, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(base64.StdEncoding.EncodeToString(dst))
assert.Equal(t, base64.StdEncoding.EncodeToString(dst), "vOwA1oZknZ54rgXETVYwMg==")
// AES-256-ECB, PKCS7_PADDING
key = []byte("12345123451234512345123451234512")
dst, err = AesECBEncrypt(src, key, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(base64.StdEncoding.EncodeToString(dst))
assert.Equal(t, base64.StdEncoding.EncodeToString(dst), "F+tlXjWffI4xt656KVwgLg==")
}
func TestAesECBDecrypt(t *testing.T) {
src, err := base64.StdEncoding.DecodeString("SpfAShHImQhWjd/21Pgz2Q==")
assert.NoError(t, err)
// AES-128-ECB, PKCS7_PADDING
key := []byte("1234512345123451")
dst, err := AesECBDecrypt(src, key, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(string(dst))
assert.Equal(t, dst, []byte("123456"))
// AES-192-ECB, PKCS7_PADDING
src, _ = base64.StdEncoding.DecodeString("vOwA1oZknZ54rgXETVYwMg==")
key = []byte("123451234512345123451234")
dst, err = AesECBDecrypt(src, key, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(string(dst))
assert.Equal(t, dst, []byte("123456"))
// AES-256-ECB, PKCS7_PADDING
src, _ = base64.StdEncoding.DecodeString("F+tlXjWffI4xt656KVwgLg==")
key = []byte("12345123451234512345123451234512")
dst, err = AesECBDecrypt(src, key, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(string(dst))
assert.Equal(t, dst, []byte("123456"))
}
func TestAesCBCEncrypt(t *testing.T) {
src := []byte("123456")
iv := []byte("6789067890678906")
// AES-128-ECB, PKCS7_PADDING
key := []byte("1234512345123451")
dst, err := AesCBCEncrypt(src, key, iv, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(base64.StdEncoding.EncodeToString(dst))
assert.Equal(t, base64.StdEncoding.EncodeToString(dst), "0huM2ppRyYZmZnzdoCL/tA==")
}
func TestAesCBCDecrypt(t *testing.T) {
src, err := base64.StdEncoding.DecodeString("0huM2ppRyYZmZnzdoCL/tA==")
assert.NoError(t, err)
iv := []byte("6789067890678906")
// AES-128-ECB, PKCS7_PADDING
key := []byte("1234512345123451")
dst, err := AesCBCDecrypt(src, key, iv, PKCS7_PADDING)
assert.NoError(t, err)
t.Log(string(dst))
assert.Equal(t, dst, []byte("123456"))
}