-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchal10_test.go
63 lines (51 loc) · 1.21 KB
/
chal10_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
package set2_test
import (
"bytes"
"cryptopals/internal/constants"
"cryptopals/pkg/aes"
"encoding/base64"
"os"
"testing"
)
func TestCBC(t *testing.T) {
size := 16
key := []byte("YELLOW SUBMARINE") // 16-byte key
iv := make([]byte, size) // 16-byte all zeros
pt := []byte("BLUE EYED FISHES") // 16-byte plaintext
ct, err := aes.CBCEncrypt(pt, iv, key, size)
if err != nil {
t.Fatal(err)
return
}
ptTest, err := aes.CBCDecrypt(ct, iv, key, size)
if err != nil {
t.Fatal(err)
return
}
t.Log(string(ptTest))
if !bytes.Equal(pt, ptTest) {
t.Fatal(constants.ErrWrongResult)
}
}
func TestChal10(t *testing.T) {
// read file (base64 encoded)
fileb64, err := os.ReadFile("../../res/set2/10.txt")
if err != nil {
t.Fatal(err)
}
// decode
key := []byte("YELLOW SUBMARINE")
ct := make([]byte, base64.StdEncoding.DecodedLen(len(fileb64)))
base64.StdEncoding.Decode(ct, fileb64)
iv := make([]byte, 16) // 128-bit all zeros
// decrypt
pt, err := aes.CBCDecrypt(ct, iv, key, 16)
if err != nil {
t.Fatal(err)
}
expectedPrefix := "I'm back and I'm ringin' the bell"
if string(pt)[:len(expectedPrefix)] != expectedPrefix {
t.Fatal(constants.ErrWrongResult)
}
t.Log(string(pt))
}