-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileEncryptor.java
More file actions
125 lines (108 loc) · 3.77 KB
/
FileEncryptor.java
File metadata and controls
125 lines (108 loc) · 3.77 KB
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
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.security.SecureRandom;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class FileEncryptor {
private static final String KEY_FILE = "Secret.key";
public static void generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256, new SecureRandom());
SecretKey secretKey = keyGen.generateKey();
byte[] keyBytes = secretKey.getEncoded();
FileOutputStream fos = new FileOutputStream("Secret.key");
try {
fos.write(keyBytes);
} catch (Throwable var7) {
try {
fos.close();
} catch (Throwable var6) {
var7.addSuppressed(var6);
}
throw var7;
}
fos.close();
}
public static SecretKey loadKey() throws Exception {
byte[] keyBytes = Files.readAllBytes((new File("Secret.key")).toPath());
return new SecretKeySpec(keyBytes, "AES");
}
public static void encryptFile(String filename, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(1, key);
byte[] fileData = Files.readAllBytes((new File(filename)).toPath());
byte[] encryptedData = cipher.doFinal(fileData);
FileOutputStream fos = new FileOutputStream(filename);
try {
fos.write(encryptedData);
} catch (Throwable var9) {
try {
fos.close();
} catch (Throwable var8) {
var9.addSuppressed(var8);
}
throw var9;
}
fos.close();
}
public static void decryptFile(String filename, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(2, key);
byte[] encryptedData = Files.readAllBytes((new File(filename)).toPath());
byte[] decryptedData = cipher.doFinal(encryptedData);
FileOutputStream fos = new FileOutputStream(filename);
try {
fos.write(decryptedData);
} catch (Throwable var9) {
try {
fos.close();
} catch (Throwable var8) {
var9.addSuppressed(var8);
}
throw var9;
}
fos.close();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter 'E' to encrypt or 'D' to decrypt the file: ");
String choice = scanner.nextLine().trim().toLowerCase();
try {
String filename;
File file;
SecretKey key;
if (choice.equals("e")) {
System.out.print("Enter the file name to encrypt: ");
filename = scanner.nextLine();
file = new File(filename);
if (file.exists()) {
generateKey();
key = loadKey();
encryptFile(filename, key);
System.out.println("File Encrypted Successfully!");
} else {
System.out.println("File not found!");
}
} else if (choice.equals("d")) {
System.out.print("Enter the file name to decrypt: ");
filename = scanner.nextLine();
file = new File(filename);
if (file.exists()) {
key = loadKey();
decryptFile(filename, key);
System.out.println("File Decrypted Successfully!");
} else {
System.out.println("File not found!");
}
} else {
System.out.println("Invalid choice. Use 'E' for encryption or 'D' for decryption.");
}
} catch (Exception var6) {
System.out.println("An error occurred: " + var6.getMessage());
}
}
}