-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypt_test.go
91 lines (72 loc) · 2.39 KB
/
crypt_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
package cryptanalysis
import (
"bytes"
"testing"
)
func TestEncryptXor(t *testing.T) {
key := []byte("ABC")
data := []byte("TESTDATA")
cipher := EncryptXor(data, key)
res := []byte{21, 7, 16, 21, 6, 2, 21, 3}
if bytes.Compare(cipher, res) != 0 {
t.Error("Expected", res, "got", cipher)
}
}
func TestEncryptEcb(t *testing.T) {
key := []byte("YELLOW SUBMARINE")
plain := []byte("This is a test of ECB.")
expected := []byte{0xf6, 0xd6, 0xbb, 0xa9, 0xf4, 0x88, 0xc9, 0xe2, 0xbd,
0xa5, 0x04, 0x27, 0x38, 0x28, 0x11, 0x2f, 0x8e, 0x81, 0x4d, 0xd8, 0xee,
0xe7, 0xf3, 0x2c, 0x74, 0x9c, 0x7b, 0x62, 0xad, 0xfb, 0x19, 0xe4}
cipher, err := EncryptEcb(plain, key)
if err != nil {
t.Error(err)
}
if bytes.Compare(cipher, expected) != 0 {
t.Error("Expected", expected, "got", cipher)
}
}
func TestDecryptEcb(t *testing.T) {
key := []byte("YELLOW SUBMARINE")
cipher := []byte{0xf6, 0xd6, 0xbb, 0xa9, 0xf4, 0x88, 0xc9, 0xe2, 0xbd,
0xa5, 0x04, 0x27, 0x38, 0x28, 0x11, 0x2f, 0x8e, 0x81, 0x4d, 0xd8, 0xee,
0xe7, 0xf3, 0x2c, 0x74, 0x9c, 0x7b, 0x62, 0xad, 0xfb, 0x19, 0xe4}
expected := []byte("This is a test of ECB.\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a")
plain, err := DecryptEcb(cipher, key)
if err != nil {
t.Error(err)
}
if bytes.Compare(plain, expected) != 0 {
t.Error("Expected", expected, "got", plain)
}
}
func TestEncryptCbc(t *testing.T) {
key := []byte("YELLOW SUBMARINE")
iv := make([]byte, 16)
plain := []byte("This is a test of CBC.")
expected := []byte{0xf6, 0xd6, 0xbb, 0xa9, 0xf4, 0x88, 0xc9, 0xe2, 0xbd,
0xa5, 0x04, 0x27, 0x38, 0x28, 0x11, 0x2f, 0xe8, 0xf6, 0x27, 0xc3, 0xf4,
0x7f, 0x38, 0xad, 0xbf, 0xa0, 0xd8, 0xbe, 0x5a, 0x02, 0x54, 0xde}
cipher, err := EncryptCbc(plain, key, iv)
if err != nil {
t.Error(err)
}
if bytes.Compare(cipher, expected) != 0 {
t.Error("Expected", expected, "got", cipher)
}
}
func TestDecryptCbc(t *testing.T) {
key := []byte("YELLOW SUBMARINE")
iv := make([]byte, 16)
cipher := []byte{0xf6, 0xd6, 0xbb, 0xa9, 0xf4, 0x88, 0xc9, 0xe2, 0xbd,
0xa5, 0x04, 0x27, 0x38, 0x28, 0x11, 0x2f, 0xe8, 0xf6, 0x27, 0xc3, 0xf4,
0x7f, 0x38, 0xad, 0xbf, 0xa0, 0xd8, 0xbe, 0x5a, 0x02, 0x54, 0xde}
expected := []byte("This is a test of CBC.\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a\x0a")
plain, err := DecryptCbc(cipher, key, iv)
if err != nil {
t.Error(err)
}
if bytes.Compare(plain, expected) != 0 {
t.Error("Expected", expected, "got", plain)
}
}