-
Notifications
You must be signed in to change notification settings - Fork 0
/
break.go
46 lines (37 loc) · 954 Bytes
/
break.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
package cryptanalysis
import ()
func BreakSingleByteXor(data []byte, chi AlphabetFrequency) (float64, byte, string) {
low := 1000.0
msg := ""
key := byte(0)
// Bruteforce the key by XORing each possible key, analyzing the decrypted
// message, and scoring it. Lowest score wins.
for i := 0; i < 256; i++ {
k := byte(i)
dec := XorArrayByte(data, k)
score := ScoreAlphabet(string(dec), chi)
if score < low {
low = score
msg = string(dec)
key = k
}
}
return low, key, msg
}
func BreakCaesarShift(cipher string, chi AlphabetFrequency) (float64, int, string) {
score := 1000.0
plain := ""
shift := 0
// Bruteforce the shift by rotating the string by each possible value,
// analyzing the rotated string, and scoring it. Lowest score wins.
for i := 1; i < 26; i++ {
p := CaesarShift(cipher, i)
s := ScoreAlphabet(p, chi)
if s < score {
score = s
plain = p
shift = i
}
}
return score, shift, plain
}