-
Notifications
You must be signed in to change notification settings - Fork 0
/
xor.go
36 lines (29 loc) · 928 Bytes
/
xor.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
package xor_single
import (
"github.com/adriansr/cryptopals-challenge/text/freq_analysis"
)
func BruteForceXORSingle(cyphertext []byte, frequencyMap freq_analysis.FrequencyMap) (plaintext []byte, key byte, score float64) {
N := len(cyphertext)
buf := make([]byte, N)
best := struct {
key int
str []byte
score float64
}{}
for keyInt := 0; keyInt < 256; keyInt++ {
key := byte(keyInt)
for idx, value := range cyphertext {
buf[idx] = value ^ key
}
score := freq_analysis.ScoreFrequencies(buf, freq_analysis.EnglishRelativeFrequencies)
if score > best.score {
best.score = score
best.str = make([]byte, N)
copy(best.str, buf)
best.key = keyInt
}
// fmt.Printf("debug: <<%s>> key=%02X score=%f\n", terminal.PrettyASCII(plaintext), keyInt, score)
}
// fmt.Printf("Result: <<%s>> key=%02X score=%f\n", best.str, best.key, best.score)
return best.str, byte(best.key), best.score
}