forked from dimpeshpanwar/Java-Advance-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileEncryptionUtility.java
More file actions
97 lines (83 loc) · 3.48 KB
/
FileEncryptionUtility.java
File metadata and controls
97 lines (83 loc) · 3.48 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
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Scanner;
public class FileEncryptionUtility {
// Generate AES key
public static SecretKey generateKey(String keyStr) throws Exception {
byte[] keyBytes = keyStr.getBytes("UTF-8");
byte[] key = new byte[16]; // AES 128-bit
int len = Math.min(keyBytes.length, key.length);
System.arraycopy(keyBytes, 0, key, 0, len);
return new SecretKeySpec(key, "AES");
}
// Encrypt file
public static void encryptFile(File inputFile, File outputFile, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
try (FileInputStream fis = new FileInputStream(inputFile);
FileOutputStream fos = new FileOutputStream(outputFile)) {
byte[] buffer = new byte[64];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
byte[] output = cipher.update(buffer, 0, bytesRead);
if (output != null) fos.write(output);
}
byte[] outputBytes = cipher.doFinal();
if (outputBytes != null) fos.write(outputBytes);
}
}
// Decrypt file
public static void decryptFile(File inputFile, File outputFile, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
try (FileInputStream fis = new FileInputStream(inputFile);
FileOutputStream fos = new FileOutputStream(outputFile)) {
byte[] buffer = new byte[64];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
byte[] output = cipher.update(buffer, 0, bytesRead);
if (output != null) fos.write(output);
}
byte[] outputBytes = cipher.doFinal();
if (outputBytes != null) fos.write(outputBytes);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("=== File Encryption/Decryption Utility ===");
System.out.println("1. Encrypt File");
System.out.println("2. Decrypt File");
System.out.print("Choose option: ");
int choice = sc.nextInt();
sc.nextLine(); // consume newline
System.out.print("Enter path of input file: ");
String inputPath = sc.nextLine();
File inputFile = new File(inputPath);
System.out.print("Enter path of output file: ");
String outputPath = sc.nextLine();
File outputFile = new File(outputPath);
System.out.print("Enter secret key (any string): ");
String keyStr = sc.nextLine();
try {
SecretKey key = generateKey(keyStr);
if (choice == 1) {
encryptFile(inputFile, outputFile, key);
System.out.println("File encrypted successfully!");
} else if (choice == 2) {
decryptFile(inputFile, outputFile, key);
System.out.println("File decrypted successfully!");
} else {
System.out.println("Invalid choice!");
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
sc.close();
}
}