-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
195 lines (152 loc) · 4.92 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
package main
import (
"crypto/rand"
"errors"
"github.com/andlabs/ui"
"io"
"io/ioutil"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"golang.org/x/crypto/pbkdf2"
"crypto/sha512"
)
import _ "github.com/andlabs/ui/winmanifest"
func check(err error) {
if err != nil {
panic(err)
}
}
// getfilename returns the whole filename for the file chosen by the user.
func getfilename(window *ui.Window) string {
filename := ui.OpenFile(window)
return filename
}
// encrypt encrypts the given file. If successful, it returns nil else it returns an error.
func encrypt(filename string, pass string) error {
var key [32]byte
var salt [16]byte
_, err := rand.Read(salt[:]) // reads in a random salt.
check(err)
f, err := ioutil.ReadFile(filename)
if err != nil {
return errors.New("Error: Unable to read the file.")
}
secretbytes := pbkdf2.Key([]byte(pass), salt[:], 3*100000, 32, sha512.New) // generates the key from the password.
copy(key[:], secretbytes[:])
block, err := aes.NewCipher(key[:])
check(err)
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
ciphertext := make([]byte, aes.BlockSize+len([]byte(f)))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], []byte(f))
writef := filename + ".encrypted"
toprepend := append(salt[:], byte(3))
ciphertext = append(toprepend, ciphertext...)
err3 := ioutil.WriteFile(writef, []byte(base64.URLEncoding.EncodeToString(ciphertext)) , 0644)
check(err3)
return nil
}
// decrypt decrypts the given file. If successful, it returns nil else it returns an error.
func decrypt(filename string, pass string) error {
fb, err := ioutil.ReadFile(filename)
if err != nil {
return errors.New("Error: Unable to read the file.")
}
var key [32]byte
var salt [16]byte
temp, _ := base64.URLEncoding.DecodeString(string(fb[:]))
contents := []byte(temp)
copy(salt[:], contents[:16]) // the salt is stored in the 1st 16 bytes of the file.
iters := int(contents[16]) // the number of iterations (* 100000) needed for the pbkdf2 function.
ciphertext := contents[17:] // the rest of the file will be the ciphertext
secretbytes := pbkdf2.Key([]byte(pass), salt[:], iters*100000, 32, sha512.New)
copy(key[:], secretbytes[:])
block, err := aes.NewCipher(key[:])
if err != nil {
panic(err)
}
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
if len(ciphertext) < aes.BlockSize {
panic("ciphertext too short")
}
iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
// XORKeyStream can work in-place if the two arguments are the same.
stream.XORKeyStream(ciphertext, ciphertext)
writef := filename[:len(filename)-10] // removes the .encrypted extension.
err2 := ioutil.WriteFile(writef, []byte(ciphertext), 0644)
check(err2)
return nil
}
// NewWindow genrates the window to be displayed.
func NewWindow() {
filechosen := ui.NewLabel("File Chosen: None")
sep := ui.NewHorizontalSeparator()
filelabel := ui.NewLabel("Choose a file to encrypt/decrypt:")
openbutton := ui.NewButton("Open")
sep2 := ui.NewHorizontalSeparator()
passlabel := ui.NewLabel("Password for encrypting/decrypting:")
passfield := ui.NewPasswordEntry()
encbutton := ui.NewButton("Encrypt")
decbutton := ui.NewButton("Decrypt")
box := ui.NewVerticalBox()
buttonbox := ui.NewHorizontalBox()
box.Append(filechosen, false)
box.Append(sep, false)
box.Append(filelabel, false)
box.Append(openbutton, false)
box.Append(sep2, false)
box.Append(passlabel, false)
box.Append(passfield, false)
buttonbox.Append(encbutton, true)
buttonbox.Append(decbutton, true)
box.Append(buttonbox, false)
buttonbox.SetPadded(true)
box.SetPadded(true)
window := ui.NewWindow("Qencrypt", 300, 150, false)
window.SetChild(box)
var filename string
openbutton.OnClicked(func(*ui.Button) {
filename = getfilename(window)
filechosen.SetText("File Chosen: " + filename)
})
encbutton.OnClicked(func(*ui.Button) {
err := encrypt(filename, passfield.Text())
if err != nil {
ui.MsgBox(window, "Encryption Unsucessful.", err.Error())
} else {
message := "The file " + filename + " has been successfully encrypted. Thank you!"
ui.MsgBox(window, "Encryption Successful!", message)
}
})
decbutton.OnClicked(func(*ui.Button) {
err := decrypt(filename, passfield.Text())
if err != nil {
ui.MsgBox(window, "Decryption Unsuccessful.", err.Error())
} else {
message := "The file " + filename + " has been successfully decrypted."
ui.MsgBox(window, "Decryption Successful!", message)
}
})
window.OnClosing(func(*ui.Window) bool {
ui.Quit()
return true
})
window.Show()
}
func main() {
err := ui.Main(func() {
NewWindow()
})
if err != nil {
panic(err)
}
}