-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxorcrack.go
67 lines (56 loc) · 1.11 KB
/
xorcrack.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
package main
type CommonCharacterTable map[byte]byte
var englishMostCommon CommonCharacterTable = CommonCharacterTable{
' ': 13,
'e': 12,
't': 11,
'a': 10,
'o': 9,
'i': 8,
'n': 7,
's': 6,
'h': 5,
'r': 4,
'd': 3,
'l': 2,
'u': 1,
'E': 12,
'T': 11,
'A': 10,
'O': 9,
'I': 8,
'N': 7,
'S': 6,
'H': 5,
'R': 4,
'D': 3,
'L': 2,
'U': 1,
}
func checkKey(scoringTable CommonCharacterTable, inputByte []byte, key byte) ([]byte, int) {
outputByte := make([]byte, len(inputByte))
score := 0
for i, value := range inputByte {
decodedValue := value ^ key
outputByte[i] = decodedValue
value, exists := scoringTable[decodedValue]
if exists {
score += int(value)
}
}
return outputByte, score
}
func CheckAllCombinationOfSingleKey(input []byte) ([]byte, byte, int) {
var bestKey byte
var bestOutput []byte
scoreOfBestKey := 0
for key := byte(0); key < 255; key++ {
outputByte, currentScore := checkKey(englishMostCommon, input, key)
if currentScore > scoreOfBestKey {
bestKey = key
bestOutput = outputByte
scoreOfBestKey = currentScore
}
}
return bestOutput, bestKey, scoreOfBestKey
}