-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTotallySuspiciousFile.txt
325 lines (284 loc) · 9.85 KB
/
TotallySuspiciousFile.txt
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import java.io.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.util.*;
import java.nio.file.*;
/*
* Cryptr
*
* Cryptr is a java encryption toolset
* that can be used to encrypt/decrypt files
* and keys locally, allowing for files to be
* shared securely over the world wide web
*
* Cryptr provides the following functions:
* 1. Generating a secret key
* 2. Encrypting a file with a secret key
* 3. Decrypting a file with a secret key
* 4. Encrypting a secret key with a public key
* 5. Decrypting a secret key with a private key
*
*/
public class Cryptr {
/**
* Generates an 128-bit AES secret key and writes it to a file
*
* @param secKeyFile name of file to store secret key
*/
static void generateKey(String secKeyFile) throws Exception{
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
SecretKey key = keyGenerator.generateKey();
String keyString = Base64.getEncoder().encodeToString(key.getEncoded());
System.out.println(keyString);
try(FileOutputStream out = new FileOutputStream(secKeyFile)){
byte[] keyb = key.getEncoded();
out.write(keyb);
}
}
/**
* Extracts secret key from a file, generates an
* initialization vector, uses them to encrypt the original
* file, and writes an encrypted file containing the initialization
* vector followed by the encrypted file data
*
* @param originalFile name of file to encrypt
* @param secKeyFile name of file storing secret key
* @param encryptedFile name of file to write iv and encrypted file data
*/
static void encryptFile(String originalFile, String secKeyFile, String encryptedFile) {
byte[] key = null;
try{
key = Files.readAllBytes(Paths.get(secKeyFile));
}
catch(IOException e){
System.out.println("IOException Thrown");
System.exit(0);
}
SecretKeySpec skey = new SecretKeySpec(key, "AES");
byte[] iv = new byte[128/8];
SecureRandom srandom = new SecureRandom();
srandom.nextBytes(iv);
IvParameterSpec ivspec = new IvParameterSpec(iv);
try(FileOutputStream out = new FileOutputStream(encryptedFile);
FileInputStream in = new FileInputStream(originalFile)){
out.write(iv);
Cipher ci = Cipher.getInstance("AES/CBC/PKCS5Padding");
ci.init(Cipher.ENCRYPT_MODE, skey, ivspec);
byte[] ibuf = new byte[1024];
int len;
while((len = in.read(ibuf)) != -1){
byte[] obuf = ci.update(ibuf,0, len);
if(obuf != null)
out.write(obuf);
}
byte[] obuf = ci.doFinal();
if(obuf != null)
out.write(obuf);
in.close();
out.close();
}
catch(Exception e){
System.out.println("you dun goofed");
}
}
/**
* Extracts the secret key from a file, extracts the initialization vector
* from the beginning of the encrypted file, uses both secret key and
* initialization vector to decrypt the encrypted file data, and writes it to
* an output file
*
* @param encryptedFile name of file storing iv and encrypted data
* @param secKeyFile name of file storing secret key
* @param outputFile name of file to write decrypted data to
*/
static void decryptFile(String encryptedFile, String secKeyFile, String outputFile) {
byte[] key = null;
try{
key = Files.readAllBytes(Paths.get(secKeyFile));
}
catch(IOException e){
System.out.println("IOException Thrown");
System.exit(0);
}
SecretKeySpec skey = new SecretKeySpec(key, "AES");
byte[] iv = new byte[128/8];
try(FileInputStream in = new FileInputStream(encryptedFile);
FileOutputStream out = new FileOutputStream(outputFile)){
in.read(iv);
IvParameterSpec ivspec = new IvParameterSpec(iv);
Cipher ci = Cipher.getInstance("AES/CBC/PKCS5Padding");
ci.init(Cipher.DECRYPT_MODE, skey, ivspec);
byte[] ibuf = new byte[1024];
int len;
while((len = in.read(ibuf)) != -1){
byte[] obuf = ci.update(ibuf, 0, len);
if(obuf != null)
out.write(obuf);
}
byte[] obuf = ci.doFinal();
if(obuf != null)
out.write(obuf);
in.close();
out.close();
}
catch(Exception e){
System.out.println("You dun goofed");
}
}
/**
* Extracts secret key from a file, encrypts a secret key file using
* a public Key (*.der) and writes the encrypted secret key to a file
*
* @param secKeyFile name of file holding secret key
* @param pubKeyFile name of public key file for encryption
* @param encKeyFile name of file to write encrypted secret key
*/
static void encryptKey(String secKeyFile, String pubKeyFile, String encKeyFile) {
/*byte[] bytes = null;
X509EncodedKeySpec ks = null;
KeyFactory kf = null;*/
PublicKey pub = null;
try{
byte[] bytes = Files.readAllBytes(Paths.get(pubKeyFile));
X509EncodedKeySpec ks = new X509EncodedKeySpec(bytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
pub = kf.generatePublic(ks);
}
catch(Exception e){
System.out.println("Exception Thrown");
System.exit(0);
}
try(FileInputStream in = new FileInputStream(secKeyFile);
FileOutputStream out = new FileOutputStream(encKeyFile)){
Cipher ci = Cipher.getInstance("RSA/ECB/PKCS1Padding");
ci.init(Cipher.ENCRYPT_MODE, pub);
byte[] ibuf = new byte[1024];
int len;
while((len = in.read(ibuf)) != -1){
byte[] obuf = ci.update(ibuf, 0, len);
if(obuf != null)
out.write(obuf);
}
byte[] obuf = ci.doFinal();
if(obuf != null)
out.write(obuf);
in.close();
out.close();
}
catch(Exception e){
System.out.println("Exception Thrown " + e.toString());
}
}
/**
* Decrypts an encrypted secret key file using a private Key (*.der)
* and writes the decrypted secret key to a file
*
* @param encKeyFile name of file storing encrypted secret key
* @param privKeyFile name of private key file for decryption
* @param secKeyFile name of file to write decrypted secret key
*/
static void decryptKey(String encKeyFile, String privKeyFile, String secKeyFile) {
PrivateKey pvt = null;
try{
byte[] bytes = Files.readAllBytes(Paths.get(privKeyFile));
PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(bytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
pvt = kf.generatePrivate(ks);
}
catch(Exception e){
System.out.println("Exception Thrown");
System.exit(0);
}
try(FileInputStream in = new FileInputStream(encKeyFile);
FileOutputStream out = new FileOutputStream(secKeyFile)){
Cipher ci = Cipher.getInstance("RSA/ECB/PKCS1Padding");
ci.init(Cipher.DECRYPT_MODE, pvt);
byte[] ibuf = new byte[1024];
int len;
while((len = in.read(ibuf)) != -1){
byte[] obuf = ci.update(ibuf, 0, len);
if(obuf != null)
out.write(obuf);
}
byte[] obuf = ci.doFinal();
if(obuf != null)
out.write(obuf);
in.close();
out.close();
}
catch(Exception e){
System.out.println("Exception Thrown " + e.toString());
}
}
/**
* Main Program Runner
*/
public static void main(String[] args) throws Exception{
String func;
if(args.length < 1) {
func = "";
} else {
func = args[0];
}
switch(func)
{
case "generatekey":
if(args.length != 2) {
System.out.println("Invalid Arguments.");
System.out.println("Usage: Cryptr generatekey <key output file>");
break;
}
System.out.println("Generating secret key and writing it to " + args[1]);
generateKey(args[1]);
break;
case "encryptfile":
if(args.length != 4) {
System.out.println("Invalid Arguments.");
System.out.println("Usage: Cryptr encryptfile <file to encrypt> <secret key file> <encrypted output file>");
break;
}
System.out.println("Encrypting " + args[1] + " with key " + args[2] + " to " + args[3]);
encryptFile(args[1], args[2], args[3]);
break;
case "decryptfile":
if(args.length != 4) {
System.out.println("Invalid Arguments.");
System.out.println("Usage: Cryptr decryptfile <file to decrypt> <secret key file> <decrypted output file>");
break;
}
System.out.println("Decrypting " + args[1] + " with key " + args[2] + " to " + args[3]);
decryptFile(args[1], args[2], args[3]);
break;
case "encryptkey":
if(args.length != 4) {
System.out.println("Invalid Arguments.");
System.out.println("Usage: Cryptr encryptkey <key to encrypt> <public key to encrypt with> <encrypted key file>");
break;
}
System.out.println("Encrypting key file " + args[1] + " with public key file " + args[2] + " to " + args[3]);
encryptKey(args[1], args[2], args[3]);
break;
case "decryptkey":
if(args.length != 4) {
System.out.println("Invalid Arguments.");
System.out.println("Usage: Cryptr decryptkey <key to decrypt> <private key to decrypt with> <decrypted key file>");
break;
}
System.out.println("Decrypting key file " + args[1] + " with private key file " + args[2] + " to " + args[3]);
decryptKey(args[1], args[2], args[3]);
break;
default:
System.out.println("Invalid Arguments.");
System.out.println("Usage:");
System.out.println(" Cryptr generatekey <key output file>");
System.out.println(" Cryptr encryptfile <file to encrypt> <secret key file> <encrypted output file>");
System.out.println(" Cryptr decryptfile <file to decrypt> <secret key file> <decrypted output file>");
System.out.println(" Cryptr encryptkey <key to encrypt> <public key to encrypt with> <encrypted key file> ");
System.out.println(" Cryptr decryptkey <key to decrypt> <private key to decrypt with> <decrypted key file>");
}
System.exit(0);
}
}