-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchallenge2_11.go
43 lines (38 loc) · 914 Bytes
/
challenge2_11.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
package main
import (
"fmt"
"math/rand"
"strings"
"time"
"github.com/robquant/cryptopals/pkg/tools"
)
func randomBytes(n int32) []byte {
res := make([]byte, n)
rand.Read(res)
return res
}
func encryptionOracle(input []byte) []byte {
padded := make([]byte, 0)
padded = append(padded, randomBytes(5+rand.Int31n(6))...)
padded = append(padded, input...)
padded = append(padded, randomBytes(5+rand.Int31n(6))...)
key := randomBytes(16)
if rand.Float64() < 0.5 {
fmt.Println("Choosing ECB")
return tools.EncryptAesECB(padded, key)
} else {
fmt.Println("Choosing CBC")
iv := randomBytes(16)
return tools.EncryptAesCBC(padded, key, iv)
}
}
func main() {
rand.Seed(time.Now().UnixNano())
input := strings.Repeat("A", 43)
encrypted := encryptionOracle([]byte(input))
if tools.CountSameBlocks(encrypted, 16) > 0 {
fmt.Println("Detected ECB")
} else {
fmt.Println("Detected CBC")
}
}