-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
186 lines (178 loc) · 5.74 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"encoding/base64"
"encoding/hex"
"fmt"
"os"
)
/*
USAGE: use command-line to specify set #, challenge # and any string
arguments e.g for set 1, challenge 1:
./main 1 1 49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d
Challenge-specific file inputs don't need to be specified on the command-line.
*/
func main() {
setNum := os.Args[1]
challengeNum := os.Args[2]
var input string
var secondInput string
if setNum == "1" {
if len(os.Args) > 3 {
input = os.Args[3]
}
if len(os.Args) > 4 {
secondInput = os.Args[4]
}
executeSetOne(challengeNum, input, secondInput)
} else if setNum == "2" {
if len(os.Args) > 3 {
input = os.Args[3]
}
executeSetTwo(challengeNum, input)
} else if setNum == "3" {
if len(os.Args) > 3 {
input = os.Args[3]
}
executeSetThree(challengeNum, input)
} else if setNum == "4" {
if len(os.Args) > 3 {
input = os.Args[3]
}
executeSetFour(challengeNum, input)
}
return
}
func executeSetOne(challengeNum, input string, secondInput string) {
if challengeNum == "1" {
var output string = hexTo64(input)
fmt.Println(output)
} else if challengeNum == "2" {
firstBytes := decodeHex(input)
secondBytes := decodeHex(secondInput)
xorBytes := fixedXOR(firstBytes, secondBytes)
var hexOut string = hex.EncodeToString(xorBytes)
fmt.Println(hexOut)
} else if challengeNum == "3" {
inputBytes := decodeHex(input)
solution := decodeXORCipher(inputBytes)
fmt.Println(string(solution.Candidate))
fmt.Println(solution.Score)
} else if challengeNum == "4" {
solution := computeMeaningfulString()
fmt.Println(string(solution.Candidate))
fmt.Println(solution.Score)
} else if challengeNum == "5" {
var input string = readSmallFile("set1_data/iceicebaby.txt")
var output string = repeatingKeyXOR([]byte(input), "ICE", "hex")
fmt.Println(output)
} else if challengeNum == "6" {
var input string = readSmallFile("set1_data/challenge6.txt")
fileBytes, err := base64.StdEncoding.DecodeString(input)
if err != nil {
panic(err)
}
var keySize int = probKeySize(fileBytes)
fmt.Println(keySize)
var transposedBytes [][]byte = transposeBytes(fileBytes, keySize)
// fmt.Println(transposedBytes)
var solutionKey string
for _, bytes := range transposedBytes {
solution := decodeXORCipher(bytes)
solutionKey += string(rune((solution.Key)))
}
fmt.Println(solutionKey)
var output string = repeatingKeyXOR(fileBytes, solutionKey, "plain")
fmt.Println(output)
} else if challengeNum == "7" {
key := []byte("YELLOW SUBMARINE")
var input string = readSmallFile("set1_data/challenge7.txt")
fileBytes, err := base64.StdEncoding.DecodeString(input)
if err != nil {
panic(err)
}
var output []byte = decryptAes128ECB(fileBytes, key, false)
fmt.Println(string(output))
} else if challengeNum == "8" {
//this challenge is slightly dumb - you have to assume that the example is super contrived to expect one answer
maxRepetitions, cipherLine, idx := identifyAesECB("set1_data/challenge8.txt")
fmt.Printf("Line %d \"%s\" probably enciphered, repetitions: %d\n", idx, cipherLine, maxRepetitions)
}
return
}
func executeSetTwo(challengeNum, input string) {
if challengeNum == "1" {
bytesInput := []byte(input)
var output []byte = padPlaintext(bytesInput, 20)
fmt.Println(output)
} else if challengeNum == "2" {
var input string = readSmallFile("set2_data/challenge10.txt")
fileBytes, err := base64.StdEncoding.DecodeString(input)
if err != nil {
panic(err)
}
key := []byte("YELLOW SUBMARINE")
iv := make([]byte, 16)
var output []byte = decryptAes128CBC(fileBytes, key, iv, false)
fmt.Println(string(output))
} else if challengeNum == "3" {
output, option := randAESEncrypt([]byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
var mode string = identifyMode(output)
if mode == "ECB" && option == 0 {
fmt.Println("True positive")
} else if mode == "ECB" && option == 1 {
fmt.Println("False negative")
} else {
fmt.Println("Not false negative")
}
} else if challengeNum == "4" {
var plaintext64 string = "Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK"
plaintext, err := base64.StdEncoding.DecodeString(plaintext64)
if err != nil {
panic(err)
}
output := oneByteDecryption(plaintext)
fmt.Println(string(output))
} else if challengeNum == "5" {
var adminProfile map[string]string = makeMeAdmin()
fmt.Println(adminProfile)
} else if challengeNum == "6" {
fmt.Println("Reached my wit's end")
} else if challengeNum == "7" {
paddedPlain := "ICE ICE BABY\x04\x04\x04\x04"
fmt.Println(removePKCS7Pad([]byte(paddedPlain)))
badPlain := "ICE ICE BABY\x05\x05\x05\x05"
fmt.Println(removePKCS7Pad([]byte(badPlain)))
} else if challengeNum == "8" {
output := cbcBitFlip()
fmt.Println(string(output))
}
return
}
func executeSetThree(challengeNum, input string) {
if challengeNum == "1" {
output := paddingOracleAttack()
fmt.Println(string(output))
} else if challengeNum == "2" {
nonce := make([]byte, 8)
key := []byte("YELLOW SUBMARINE")
inText, err := base64.StdEncoding.DecodeString(input)
if err != nil {
panic(err)
}
output := cryptAes128CTR(inText, key, nonce)
inText = output
output1 := cryptAes128CTR(inText, key, nonce)
fmt.Println(base64.StdEncoding.EncodeToString(output1))
} else if challengeNum == "3" {
fixedNonceCTR("set3_data/challenge3.txt")
} else if challengeNum == "4" {
fixedNonceCTR("set3_data/challenge4.txt")
}
return
}
func executeSetFour(challengeNum, input string) {
if challengeNum == "1" {
recoverPlaintextCTR("set2_data/challenge10.txt")
}
return
}