-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyManager.java
More file actions
35 lines (31 loc) · 1.25 KB
/
KeyManager.java
File metadata and controls
35 lines (31 loc) · 1.25 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
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
public class KeyManager {
private static final String KEY_FILE = "secret.key";
// Generates and saves a new AES key
public static void generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128); // AES-128 encryption
SecretKey key = keyGen.generateKey();
saveKeyToFile(key);
System.out.println("Encryption Key generated and saved as 'secret.key'");
}
// Loads an existing AES key from file
public static SecretKey loadKey() throws Exception {
File keyFile = new File(KEY_FILE);
if (!keyFile.exists()) {
throw new Exception("Key file not found! Generate a key first.");
}
byte[] keyBytes = Files.readAllBytes(keyFile.toPath());
return new javax.crypto.spec.SecretKeySpec(keyBytes, "AES");
}
// Saves the AES key to a file
private static void saveKeyToFile(SecretKey key) throws Exception {
try (FileOutputStream fos = new FileOutputStream(KEY_FILE)) {
fos.write(key.getEncoded());
}
}
}