-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileEncryptor.java.
More file actions
57 lines (47 loc) · 2.28 KB
/
Copy pathFileEncryptor.java.
File metadata and controls
57 lines (47 loc) · 2.28 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
// File Name: FileEncryptor.java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileEncryptor {
public static void main(String[] args) {
// A simple secret key. You can change this to any character.
char secretKey = 'K';
// The name of the file you want to encrypt.
// It must be in the same folder as your project.
String originalFilePath = "my_secret_message.txt";
// Names for the output files.
String encryptedFilePath = "encrypted_file.dat";
String decryptedFilePath = "decrypted_message.txt";
try {
System.out.println("Starting encryption...");
encryptFile(originalFilePath, encryptedFilePath, secretKey);
System.out.println("File encrypted successfully! Output: " + encryptedFilePath);
System.out.println("\nStarting decryption...");
// We use the SAME function to decrypt! That's the magic of XOR.
encryptFile(encryptedFilePath, decryptedFilePath, secretKey);
System.out.println("File decrypted successfully! Output: " + decryptedFilePath);
} catch (IOException e) {
System.out.println("An error occurred. Make sure '" + originalFilePath + "' exists in your project folder.");
e.printStackTrace();
}
}
/**
* Reads a file, encrypts/decrypts its content using a key, and writes to a new file.
* @param sourcePath Path of the input file.
* @param destinationPath Path for the output file.
* @param key The secret key for XOR operation.
* @throws IOException If there's an error reading or writing files.
*/
public static void encryptFile(String sourcePath, String destinationPath, char key) throws IOException {
try (FileInputStream inputStream = new FileInputStream(sourcePath);
FileOutputStream outputStream = new FileOutputStream(destinationPath)) {
int byteRead;
while ((byteRead = inputStream.read()) != -1) {
// This is the core encryption/decryption logic!
// The ^ symbol is the bitwise XOR operator.
int encryptedByte = byteRead ^ key;
outputStream.write(encryptedByte);
}
}
}
}