-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
279 lines (234 loc) · 6.7 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"os"
"os/exec"
"runtime"
"time"
)
// Quiz represents the structure of the quiz
type Quiz struct {
Title string `json:"title"`
Level string `json:"level"`
Time int `json:"time"`
Questions []Question `json:"questions"`
}
// Question represents a single quiz question
type Question struct {
Question string `json:"question"`
Answers []string `json:"answers"`
Points int `json:"points"`
CorrectAnswer string `json:"correct_answer"`
}
// clearConsole clears the console based on the OS
func clearConsole() {
switch runtime.GOOS {
case "windows":
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
case "linux", "darwin":
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
default:
fmt.Println("Your platform is not supported to clear the terminal.")
}
}
func encryptor(){
//the key should be 32 chars long or string[32]
key := []byte("32-byte-long-key-for-AES-encrypi")
// Encrypting the JSON file
inputFile := "quiz.json"
encryptedFile := "quiz_encrypted.json"
err := Encrypt(inputFile, encryptedFile, key)
if err != nil {
fmt.Println("Error encrypting file:", err)
return
}
fmt.Println("File encrypted successfully.")
}
// createQuiz allows the user to create a new quiz and saves it to a JSON file
func createQuiz() {
var quiz Quiz
fmt.Print("Enter quiz title: ")
fmt.Scanln(&quiz.Title)
fmt.Print("Enter quiz level: ")
fmt.Scanln(&quiz.Level)
fmt.Print("Enter quiz time (seconds): ")
fmt.Scanln(&quiz.Time)
for {
var question Question
fmt.Print("Enter question: ")
fmt.Scanln(&question.Question)
for i := 0; i < 4; i++ {
var answer string
fmt.Printf("Enter answer %d: ", i+1)
fmt.Scanln(&answer)
question.Answers = append(question.Answers, answer)
}
fmt.Print("Enter points for this question: ")
fmt.Scanln(&question.Points)
fmt.Print("Enter the correct answer: ")
fmt.Scanln(&question.CorrectAnswer)
quiz.Questions = append(quiz.Questions, question)
fmt.Print("Add another question? (yes/no): ")
var moreQuestions string
fmt.Scanln(&moreQuestions)
if moreQuestions != "yes" {
break
}
}
data, err := json.MarshalIndent(quiz, "", " ")
if err != nil {
fmt.Println("Error marshalling JSON data:", err)
return
}
err = ioutil.WriteFile("quiz.json", data, 0644)
if err != nil {
fmt.Println("Error writing JSON file:", err)
return
}
fmt.Println("Quiz saved successfully to quiz.json!")
//encryptor() ABU - Last Chpont [day before last exams(BE)] - 8/11/2024
}
func main() {
var mode int
fmt.Println("Quizzle text-based (terminal/command prompt) interactive question and answer program.")
fmt.Println("Choose mode:\n\t 1. Execute Quiz\n\t 2. Create Quiz")
fmt.Scanln(&mode)
switch mode {
case 1:
runQuiz()
case 2:
createQuiz()
default:
fmt.Println("Invalid mode selected. Exiting...")
}
}
func runQuiz(){
fmt.Println("Enter/Paste path to Game File")
var jsonFilePath string
fmt.Scanln(&jsonFilePath)
//decryptor() - ABU [LAST CHECKPOINT] > Tues Jul 9 2024 6:20pm (day before exams)
// Read the JSON file
data, err := ioutil.ReadFile(jsonFilePath)
if err != nil {
fmt.Println("Error reading JSON file:", err)
return
}
// Unmarshal the JSON data into the Quiz struct
var quiz Quiz
err = json.Unmarshal(data, &quiz)
if err != nil {
fmt.Println("Error parsing Game data:", err)
return
}
fmt.Println("initialized quiz")
fmt.Printf("Title: %s\nLevel: %s\nTime: %d seconds\n", quiz.Title, quiz.Level, quiz.Time)
fmt.Println("Press Enter to start the quiz...")
fmt.Scanln()
score := 0
//max score feature added by Abu
maxScore:= 0
// Iterate over each question
for i, question := range quiz.Questions {
clearConsole()
fmt.Printf("Question %d: %s\n", i+1, question.Question)
for j, answer := range question.Answers {
fmt.Printf("%d. %s\n", j+1, answer)
}
fmt.Print("Your answer: ")
var userAnswer int
fmt.Scan(&userAnswer)
if userAnswer > 0 && userAnswer <= len(question.Answers) && question.Answers[userAnswer-1] == question.CorrectAnswer {
fmt.Println("Correct!")
score += question.Points
} else {
fmt.Println("Wrong!")
}
maxScore += question.Points
fmt.Printf("Correct answer: %s\n", question.CorrectAnswer)
time.Sleep(2 * time.Second)
}
clearConsole()
fmt.Printf("Quiz Over!\nYour Score: %d\nMax Score: %d\n", score, maxScore)
}
//encrypt file
func Encrypt(inputFile, outputFile string, key []byte) error {
plaintext, err := ioutil.ReadFile(inputFile)
if err != nil {
return fmt.Errorf("error reading file: %v", err)
}
block, err := aes.NewCipher(key)
if err != nil {
return fmt.Errorf("error creating cipher block: %v", err)
}
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return fmt.Errorf("error generating IV: %v", err)
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
outFile, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("error creating output file: %v", err)
}
defer outFile.Close()
// Encode ciphertext to base64 before writing to file
encoder := base64.NewEncoder(base64.StdEncoding, outFile)
defer encoder.Close()
_, err = encoder.Write(ciphertext)
if err != nil {
return fmt.Errorf("error writing encrypted data: %v", err)
}
return nil
}
func decryptor(){
// Decrypting the encrypted JSON file
key := []byte("32-byte-long-key-for-AES-encrypi")
encryptedFile := "quiz_encrypted.json"
decryptedFile := "quiz_decrypted.json"
err := Decrypt(encryptedFile, decryptedFile, key)
if err != nil {
fmt.Println("Error decrypting file:", err)
return
}
fmt.Println("File decrypted successfully.")
}
// Decrypt decrypts a file that was encrypted using AES with a given key
func Decrypt(inputFile, outputFile string, key []byte) error {
ciphertext, err := ioutil.ReadFile(inputFile)
if err != nil {
return fmt.Errorf("error reading file: %v", err)
}
block, err := aes.NewCipher(key)
if err != nil {
return fmt.Errorf("error creating cipher block: %v", err)
}
if len(ciphertext) < aes.BlockSize {
return fmt.Errorf("encrypted data is too short")
}
iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(ciphertext, ciphertext)
outFile, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("error creating output file: %v", err)
}
defer outFile.Close()
_, err = outFile.Write(ciphertext)
if err != nil {
return fmt.Errorf("error writing decrypted data: %v", err)
}
return nil
}