/
breakrepxor.go
139 lines (120 loc) · 3.51 KB
/
breakrepxor.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"flag"
"fmt"
"io/ioutil"
"encoding/base64"
"github.com/wouterd/matasano-go/matasano"
"sort"
"strings"
)
func main() {
flag.Parse()
filename := flag.Arg(0)
if filename == "" {
fmt.Println("Please specify the file to read on the command line.")
return
}
fileContents, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println("Problem reading file", filename, ":", err)
return
}
decodeBuffer := make([]byte, base64.StdEncoding.DecodedLen(len(fileContents)))
nWritten, err := base64.StdEncoding.Decode(decodeBuffer, fileContents)
if err != nil {
fmt.Println("Unable to decode base64 data:", err)
}
encryptedBuffer := decodeBuffer[:nWritten]
keySize := determineBestKeysize(encryptedBuffer)
cypher := resolveBestRepeatingXorKey(encryptedBuffer, keySize)
fmt.Println("For keysize", keySize, "key is '"+string(cypher)+"'")
decrypted := matasano.RepeatingXor(encryptedBuffer, cypher)
fmt.Println("Message:\n", string(decrypted))
}
func resolveBestRepeatingXorKey(data []byte, keysize uint) []byte {
blockLen := uint(len(data)) / keysize
blocks := make([][]byte, keysize)
for block := uint(0) ; block < keysize ; block++ {
blocks[block] = make([]byte, blockLen)
for i := uint(0) ; i < blockLen ; i++ {
blocks[block][i] = data[i*keysize+block]
}
}
key := make([]byte, keysize)
for i := uint(0) ; i < keysize ; i++ {
key[i] = findBestCypher(blocks[i])
}
return key
}
//--- Struct that holds key size statistics and can be sorted by normalized distance
type KeysizeStatistics struct {
keysize uint
normDistance float64
}
type ByNormDistance []KeysizeStatistics
func (this ByNormDistance) Len() int {
return len(this)
}
func (this ByNormDistance) Less(i, j int) bool {
return this[i].normDistance < this[j].normDistance
}
func (this ByNormDistance) Swap(i, j int) {
this[i], this[j] = this[j], this[i]
}
//---------------
func calcNormalizedHemingDistance(a, b []byte) float64 {
distance, err := matasano.HemingDistance(a, b)
if err != nil {
panic(err)
}
return float64(distance) / float64(len(a))
}
func determineBestKeysize(buffer []byte) uint {
minKeySize := uint(2)
maxKeySize := uint(40)
keySizeStats := make([]KeysizeStatistics, maxKeySize-minKeySize+1)
for keysize := minKeySize ; keysize <= maxKeySize ; keysize++ {
normDistance := float64(0)
testChunks := uint(len(buffer)) / 2 / keysize
for i := uint(0) ; i < testChunks ; i++ {
offset := i * keysize * 2
slice1 := buffer[offset:offset+keysize]
slice2 := buffer[offset+keysize:offset+keysize*2]
normDistance += calcNormalizedHemingDistance(slice1, slice2)/float64(testChunks)
keySizeStats[keysize-minKeySize] = KeysizeStatistics{keysize, normDistance}
}
}
sort.Sort(ByNormDistance(keySizeStats))
return keySizeStats[0].keysize
}
type Candidate struct {
englishness float64
phrase string
cypher byte
line int
}
func findBestCypher(bytes []byte) byte {
var best Candidate
for i := 0 ; i < 256 ; i++ {
cypher := byte(i)
decoded := matasano.FixedXorWithSingleByteMask(bytes, cypher)
phrase := string(decoded)
englishness := getLetterDensity(phrase)
if best.englishness < englishness {
best = Candidate{englishness, phrase, cypher, 0}
}
}
return best.cypher
}
func getLetterDensity(phrase string) float64 {
letters := "0123456789abcdefghijklmnopqrstuvwxyz !,.?"
lowerCase := strings.ToLower(phrase)
totalChars := 0
for _, char := range lowerCase {
if strings.ContainsRune(letters, char) {
totalChars++
}
}
return float64(totalChars) / float64(len(phrase))
}