-
Notifications
You must be signed in to change notification settings - Fork 13
/
FileEncryption.go
131 lines (109 loc) · 2.86 KB
/
FileEncryption.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
package FileEncryption
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"errors"
"fmt"
"io"
"os"
"strings"
)
var plaintext []byte
var block cipher.Block
var key []byte
// Ext is the encrypted appended extension
var Ext = ".enc"
func main() {
return
}
// InitializeBlock Sets up the encription with a key
func InitializeBlock(myKey []byte) {
key = myKey
block, _ = aes.NewCipher(key)
}
func initIV() (stream cipher.Stream, iv []byte) {
iv = make([]byte, aes.BlockSize)
_, err := rand.Read(iv)
if err != nil {
fmt.Println("error:", err)
return
}
stream = cipher.NewCTR(block, iv[:])
return stream, iv
}
func initWithIV(myIv []byte) cipher.Stream {
return cipher.NewCTR(block, myIv[:])
}
// Decrypter decryps a file given its filepath
func Decrypter(path string) (err error) {
if block == nil {
return errors.New("Need to Initialize Block first. Call: InitializeBlock(myKey []byte)")
}
inFile, err := os.Open(path)
if err != nil {
fmt.Println("error:", err)
return
}
deobfPath := filenameDeobfuscator(path)
outFile, err := os.OpenFile(deobfPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777)
if err != nil {
return
}
iv := make([]byte, aes.BlockSize)
io.ReadFull(inFile, iv[:])
stream := initWithIV(iv)
inFile.Seek(aes.BlockSize, 0) // Read after the IV
reader := &cipher.StreamReader{S: stream, R: inFile}
if _, err = io.Copy(outFile, reader); err != nil {
fmt.Println(err)
}
inFile.Close()
os.Remove(path)
return
}
// Encrypter encrypts a file given its filepatth
func Encrypter(path string) (err error) {
if block == nil {
return errors.New("Need to Initialize Block first. Call: InitializeBlock(myKey []byte)")
}
inFile, err := os.Open(path)
if err != nil {
fmt.Println(err)
return
}
obfuscatePath := filenameObfuscator(path)
outFile, err := os.OpenFile(obfuscatePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777)
fmt.Println(outFile.Name())
if err != nil {
fmt.Println(err)
return
}
stream, iv := initIV()
outFile.Write(iv)
writer := &cipher.StreamWriter{S: stream, W: outFile}
if _, err = io.Copy(writer, inFile); err != nil {
fmt.Println(err.Error())
}
inFile.Close()
outFile.Close()
os.Remove(path)
return nil
}
func filenameObfuscator(path string) string {
filenameArr := strings.Split(path, string(os.PathSeparator))
filename := filenameArr[len(filenameArr)-1]
path2 := strings.Join(filenameArr[:len(filenameArr)-1], string(os.PathSeparator))
return path2 + string(os.PathSeparator) + filename + Ext
}
func filenameDeobfuscator(path string) string {
//get the path for the output
opPath := strings.Trim(path, Ext)
// Divide filepath
filenameArr := strings.Split(opPath, string(os.PathSeparator))
//Get filename
filename := filenameArr[len(filenameArr)-1]
// get parent dir
path2 := strings.Join(filenameArr[:len(filenameArr)-1], string(os.PathSeparator))
return path2 + string(os.PathSeparator) + filename
}