-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge8.go
48 lines (43 loc) · 1004 Bytes
/
challenge8.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
package set1
import (
"bytes"
"fmt"
"os"
"strings"
"github.com/alokmenghrajani/go-cryptopals/utils"
)
func Challenge8() {
utils.PrintTitle(1, 8)
file, err := os.ReadFile("set1/8.txt")
utils.PanicOnErr(err)
inputs := strings.Split(string(file), "\n")
bestScore := -1
bestInput := ""
for i := 0; i < len(inputs); i++ {
score := aesEcb(utils.HexToByteSlice(inputs[i]))
if score > bestScore {
bestScore = score
bestInput = inputs[i]
}
}
fmt.Println(bestInput)
fmt.Println()
}
// evaluates liklihood of buffer being encrypted with aesEcb and returns a score.
// Higher score means higher liklihood of being aesEcb.
func aesEcb(buf []byte) int {
// check that we have a multiple of 16 bytes, which is AES' block size.
if len(buf)%16 != 0 {
return 0
}
// count how many duplicate blocks we find
score := 1
for i := 0; i < len(buf); i += 16 {
for j := i + 16; j < len(buf); j += 16 {
if bytes.Equal(buf[i:i+16], buf[j:j+16]) {
score++
}
}
}
return score
}