-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileEncryptionDecryption.java
More file actions
61 lines (45 loc) · 1.83 KB
/
Copy pathFileEncryptionDecryption.java
File metadata and controls
61 lines (45 loc) · 1.83 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
import java.io.*;
import java.util.Scanner;
public class FileEncryptionDecryption {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("File Encryption/Decryption");
System.out.print("Enter E for Encryption or D for Decryption: ");
char choice = sc.next().toUpperCase().charAt(0);
sc.nextLine(); // Consume newline
System.out.print("Enter input file name (e.g., input.txt): ");
String inputFile = sc.nextLine();
System.out.print("Enter output file name (e.g., output.txt): ");
String outputFile = sc.nextLine();
try {
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
String line;
while ((line = reader.readLine()) != null) {
String result = "";
for (int i = 0; i < line.length(); i++) {
char ch = line.charAt(i);
if (choice == 'E') {
result += (char) (ch + 3); // Encrypt
} else if (choice == 'D') {
result += (char) (ch - 3); // Decrypt
}
}
writer.write(result);
writer.newLine();
}
reader.close();
writer.close();
if (choice == 'E') {
System.out.println("File encrypted successfully!");
} else if (choice == 'D') {
System.out.println("File decrypted successfully!");
} else {
System.out.println("Invalid choice!");
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
sc.close();
}
}