-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileEncryptorDecryptor.java
More file actions
54 lines (43 loc) · 1.96 KB
/
FileEncryptorDecryptor.java
File metadata and controls
54 lines (43 loc) · 1.96 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
import java.io.*;
import java.util.Scanner;
public class FileEncryptorDecryptor {
private static final int SHIFT_KEY = 3; // Simple Caesar cipher shift
public static void encryptFile(String inputFile, String outputFile) throws IOException {
processFile(inputFile, outputFile, SHIFT_KEY);
}
public static void decryptFile(String inputFile, String outputFile) throws IOException {
processFile(inputFile, outputFile, -SHIFT_KEY);
}
private static void processFile(String inputFile, String outputFile, int shift) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
int character;
while ((character = reader.read()) != -1) {
writer.write(character + shift);
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the file path: ");
String inputFile = scanner.nextLine();
System.out.print("Enter output file path: ");
String outputFile = scanner.nextLine();
System.out.print("Do you want to encrypt or decrypt? (e/d): ");
char choice = scanner.next().charAt(0);
try {
if (choice == 'e') {
encryptFile(inputFile, outputFile);
System.out.println("File encrypted successfully.");
} else if (choice == 'd') {
decryptFile(inputFile, outputFile);
System.out.println("File decrypted successfully.");
} else {
System.out.println("Invalid choice. Please enter 'e' for encryption or 'd' for decryption.");
}
} catch (IOException e) {
System.out.println("Error processing file: " + e.getMessage());
}
scanner.close();
}
}