-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
288 lines (231 loc) · 5.67 KB
/
main.go
File metadata and controls
288 lines (231 loc) · 5.67 KB
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
280
281
282
283
284
285
286
287
288
package main
import (
"io/ioutil"
"bufio"
"os"
)
const key = "abcdefghijklmnop"
func main() {
print("Enter a path directory: ")
var path, command string
var files []string
// Ask folder to user
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
path = scanner.Text()
}
if path[len(path)-1:] != "/" {
path = path + "/"
}
print("Crypt or decrypt? ")
// Ask user the operation to do
if scanner.Scan() {
command = scanner.Text()
}
if command == "crypt" {
files = listFiles(path, true)
cryptFiles(files)
} else if command == "decrypt" {
files = listFiles(path, false)
decryptFiles(files)
} else {
println("Wrong command ! Type \"crypt\" or \"decrypt\".")
}
println(len(files), "files found")
}
/**
Function that cuts some text in multiple blocks
*/
func padding(text string, blockSize int) []string {
array := make([]string, 0)
i := 0
for i < len(text) {
end := i + blockSize
if end > len(text) - 1 {
end = len(text)
}
slice := text[i:end]
array = append(array, slice)
i += blockSize
}
return array
}
func abs(number int) (int) {
if number < 0 {
return -number
}
return number
}
/**
Perform a Cesar Cipher
If the shift is < 0, shifting to the left
Otherwise, shifting to the right
*/
func cesar(text string, shiftNumber int) string {
shift, offset := rune(abs(shiftNumber) % 26), rune(26)
runes := []rune(text)
for index, char := range runes {
if shiftNumber < 0 {
if char >= 'a'+shift && char <= 'z' || char >= 'A'+shift && char <= 'Z' {
char = char - shift
} else if char >= 'a' && char < 'a'+shift || char >= 'A' && char < 'A'+shift {
char = char - shift + offset
}
} else {
if char >= 'a' && char <= 'z'-shift || char >= 'A' && char <= 'Z'-shift {
char = char + shift
} else if char > 'z'-shift && char <= 'z' || char > 'Z'-shift && char <= 'Z' {
char = char + shift - offset
}
}
runes[index] = char
}
return string(runes)
}
/**
Performs a bitwise XOR on each byte of two strings
If the strings have not the same length, we use the smallest size
*/
func xor(first string, second string) (string) {
var length int
if len(first) > len(second) {
length = len(second)
} else {
length = len(first)
}
bytes := make([]byte, length)
for i := 0; i < length; i++ {
bytes[i] = first[i] ^ second[i]
}
return string(bytes)
}
/**
Reverse bitwise XOR
The compute formula was determined with a truth table from the XOR table
*/
func unxor(first string, second string) (string) {
var length int
if len(first) > len(second) {
length = len(second)
} else {
length = len(first)
}
bytes := make([]byte, length)
for i := 0; i < length; i++ {
bytes[i] = (^first[i] & second[i]) | (first[i] & ^second[i])
}
return string(bytes)
}
func encrypt(text string, key string) (string) {
// Cuts text in blocks
paddingArray := padding(text, 16)
encrypted := make([]string, 0)
/**
Crypt each block with the following algorithm:
- Cesar cipher on the key, with a shift corresponding to the counter
- XOR between crypted key and the block
*/
for index, txt := range paddingArray {
cryptedKey := cesar(key, index)
encrypted = append(encrypted, xor(txt, cryptedKey))
}
result := ""
for _, txt := range encrypted {
result += txt
}
return result
}
func decrypt(text string, key string) (string) {
// Cuts text in blocks
paddingArray := padding(text, 16)
decrypted := make([]string, 0)
/**
Decrypt each block with the following algorithm:
- Cesar cipher on the key, with a shift corresponding to the counter
- Reverse the XOR between crypted key and encrypted block, resulting in the decrypted block
*/
for index, txt := range paddingArray {
cryptedKey := cesar(key, index)
decrypted = append(decrypted, unxor(cryptedKey, txt))
}
result := ""
for _, txt := range decrypted {
result += txt
}
return result
}
/**
List all files inside a given path, with files in subdirectories
the `crypt` parameter determines if we list the crypted or decrypted files
crypted files have an `_` at the end
*/
func listFiles(path string, crypt bool) ([]string) {
files := make([]string, 0)
directoryContent, err := ioutil.ReadDir(path)
if err != nil {
println(err.Error())
return files
}
for _, fileInDirectory := range directoryContent {
if !fileInDirectory.IsDir() {
filename := fileInDirectory.Name()
if crypt && filename[len(filename)-1:] != "_" || !crypt && filename[len(filename)-1:] == "_" {
files = append(files, path + fileInDirectory.Name())
}
} else {
newPath := path + fileInDirectory.Name() + "/"
files = append(files, listFiles(newPath, crypt)...)
}
}
return files
}
/**
Crypt a list of files
The crypted files have an `_` at the end
The original files are deleted after
*/
func cryptFiles(files []string) {
for _, file := range files {
content, err := ioutil.ReadFile(file)
if err != nil {
println(err.Error())
continue
}
encryptedContent := encrypt(string(content), key)
err = ioutil.WriteFile(file + "_", []byte(encryptedContent), 0644)
if err != nil {
println(err.Error())
continue
}
err = os.Remove(file)
if err != nil {
println(err.Error())
continue
}
}
}
/**
Decrypt a list of files
The crypted files have an `_` at the end
The crypted files are deleted after
*/
func decryptFiles(files []string) {
for _, file := range files {
content, err := ioutil.ReadFile( file)
if err != nil {
println(err.Error())
continue
}
decryptedContent := decrypt(string(content), key)
err = ioutil.WriteFile(file[0:len(file)-1], []byte(decryptedContent), 0644)
if err != nil {
println(err.Error())
continue
}
err = os.Remove(file)
if err != nil {
println(err.Error())
continue
}
}
}