forked from r3s/Go-Crypter
/
crypter.go
173 lines (145 loc) · 4.76 KB
/
crypter.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
package main
import (
"fmt"
"os"
"bufio"
"io"
"crypto/aes"
"golang.org/x/crypto/pbkdf2"
"crypto/sha1"
"crypto/cipher"
"crypto/rand"
)
const salt = "qqH+KDE3J6VHw0oGO4ml50Wc3OEvF8 xIr_LPwEQlJ|c%zqknw1zTOmHHIbF"
// Function to encrypt a file
func encryptFile(file, key string) {
fmt.Println("Encrypting...")
//Generate a key of required length using the pbkd2 lib and the input
cipherKey := pbkdf2.Key([]byte(key), []byte(salt), 4096, 32, sha1.New)
// Generate IV using rand lib
iv := make([]byte, aes.BlockSize)
_, err := rand.Read(iv)
// Define a new AES cipher with our generated key
block, err := aes.NewCipher(cipherKey)
HandleError(err, "cipher")
// Open input file to be encrypted
fin, err := os.Open(file)
HandleError(err, "open input file")
defer fin.Close()
//Get input file size
size := FileSize(file)
// Open ouput file
fout, err := os.OpenFile(file+".aes",os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600 )
HandleError(err, "open output file")
defer fout.Close()
// Write the IV at the start of the file
_, err = fout.Write(iv)
HandleError(err, "write to output")
// If file size is greater than 32KB, make a byte buffer of 32KB
// Otherwise, create a buffer of file size
var buf []byte
if(size > (4<<20)){
buf = make([]byte, 32768)
}else{
buf = make([]byte, size)
}
// Loop until we reach end of file
for{
// Read data
res, err := fin.Read(buf)
// If there is any error, exit
if err != nil && err != io.EOF {
panic(err)
}
// If end of file is reached or there is no data to be read, break
if res == 0 || err == io.EOF{
break
}
// Create a byte array for encrypted data
cipherText := make([]byte,len(buf))
// Encrypt the input data
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(cipherText, buf)
//Write the encrypted data to output file
_, err = fout.Write(cipherText)
HandleError(err, "writing cipher block")
}
fmt.Println("Done.")
}
// Function to decrypt a file
func decryptFile(file, key string) {
fmt.Println("Decrypting...")
//Generate a key of required length using the pbkd2 lib and the input
cipherKey := pbkdf2.Key([]byte(key), []byte(salt), 4096, 32, sha1.New)
// Define a new AES cipher with our generated key
block, err := aes.NewCipher(cipherKey)
HandleError(err, "cipher")
// Open input file to be encrypted
fin, err := os.Open(file)
HandleError(err, "open input file")
defer fin.Close()
//Get input file size
size := FileSize(file)
// Open ouput file
fout, err := os.OpenFile(file+".dec",os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600 )
HandleError(err, "open output file")
defer fout.Close()
iv := make([]byte, aes.BlockSize)
_, err = fin.Read(iv)
HandleError(err, "reading iv")
// If file size is greater than 32KB, make a byte buffer of 32KB
// Otherwise, create a buffer of file size
var buf []byte
if(size > (4<<20)){
buf = make([]byte, 32768)
}else{
buf = make([]byte, size)
}
// Loop until we reach end of file
for{
// Read data
res, err := fin.Read(buf)
// If there is any error, exit
if err != nil && err != io.EOF {
panic(err)
}
// If end of file is reached or there is no data to be read, break
if res == 0 || err == io.EOF{
break
}
// Create a byte array for decrypted data
cipherText := make([]byte, len(buf))
// Decrypt the input data
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(cipherText, buf[:res])
//Write the decrypted data to output file
_, err = fout.Write(cipherText)
HandleError(err, "writing cipher block")
}
fmt.Println("Done.")
}
// Main function
func main() {
// If there is no file name in the argument or if the argument is not a file
// Exit with message
if len(os.Args) < 2{
fmt.Println("Usage: crypter <filename>")
os.Exit(1)
}else if !IsFile(os.Args[1]){
fmt.Println("Usage: crypter <filename>")
os.Exit(1)
}
// Get the file name
file := os.Args[1]
// Reader for options
reader := bufio.NewReader(os.Stdin)
fmt.Print("Encrypt(e) or Decrypt(d)? :")
choice, _ := reader.ReadString('\n')
fmt.Print("Enter password : ")
key, _ := reader.ReadString('\n')
if choice[0] == 'e'{
encryptFile(file, key)
}else if choice[0] == 'd'{
decryptFile(file, key)
}
}