-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileEncryptor.java
More file actions
23 lines (20 loc) · 893 Bytes
/
FileEncryptor.java
File metadata and controls
23 lines (20 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import java.io.*;
public class FileEncryptor {
public static void encryptFile(String inputFile, String 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);
CipherOutputStream cos = new CipherOutputStream(fos, cipher)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
cos.write(buffer, 0, bytesRead);
}
}
System.out.println("Encryption successful! Encrypted file saved as " + outputFile);
}
}