-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFolderEncryptor.java
120 lines (105 loc) · 3.88 KB
/
FolderEncryptor.java
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
package com.example;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Arrays;
public class FolderEncryptor {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
public static void encryptFolder(File folder, String password) {
if (folder.isDirectory()) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
encryptFolder(file, password);
} else {
encryptFile(file, password);
}
}
}
}
}
public static void decryptFolder(File folder, String password) {
if (folder.isDirectory()) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
decryptFolder(file, password);
} else {
decryptFile(file, password);
}
}
}
}
}
private static void encryptFile(File file, String password) {
try {
FileInputStream inputStream = new FileInputStream(file);
byte[] fileBytes = new byte[(int) file.length()];
inputStream.read(fileBytes);
inputStream.close();
byte[] encryptedBytes = encrypt(fileBytes, password);
File encryptedFile = new File(file.getAbsolutePath() + ".hidden");
FileOutputStream outputStream = new FileOutputStream(encryptedFile);
outputStream.write(encryptedBytes);
outputStream.close();
file.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void decryptFile(File file, String password) {
try {
FileInputStream inputStream = new FileInputStream(file);
byte[] fileBytes = new byte[(int) file.length()];
inputStream.read(fileBytes);
inputStream.close();
byte[] decryptedBytes = decrypt(fileBytes, password);
File decryptedFile = new File(file.getAbsolutePath().replace(".hidden", ""));
FileOutputStream outputStream = new FileOutputStream(decryptedFile);
outputStream.write(decryptedBytes);
outputStream.close();
file.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
private static byte[] encrypt(byte[] input, String password) {
try {
Key secretKey = generateKey(password);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(input);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static byte[] decrypt(byte[] input, String password) {
try {
Key secretKey = generateKey(password);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return cipher.doFinal(input);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static Key generateKey(String password) {
try {
byte[] passwordBytes = Arrays.copyOf(password.getBytes(StandardCharsets.UTF_8), 32);
return new SecretKeySpec(passwordBytes, ALGORITHM);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}