-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb2.java
More file actions
45 lines (36 loc) · 1.67 KB
/
Copy pathb2.java
File metadata and controls
45 lines (36 loc) · 1.67 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
import Krypto.Aead;
import Krypto.AeadConfig;
import Krypto.KeysetHandle;
import Krypto.aead.AeadKeyTemplates;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.file.Files;
public class class2 {
public static void main(String[] args) throws Exception {
AeadConfig.register();
String resourcesPath = "path/to/your/resources/";
KeysetHandle keysetHandle = KeysetHandle.generateNew(AeadKeyTemplates.AES256_GCM);
Aead aead = keysetHandle.getPrimitive(Aead.class);
File inputFile = new File(resourcesPath + "input/plainTextFile.txt");
File encryptedFile = new File(resourcesPath + "output/cipherTextFile.txt");
encryptFile(aead, inputFile, encryptedFile);
File decryptedFile = new File(resourcesPath + "output/plainTextDecFile.txt");
decryptFile(aead, encryptedFile, decryptedFile);
System.out.println("Encryption and decryption completed successfully.");
}
private static void encryptFile(Aead aead, File inputFile, File outputFile) throws Exception {
byte[] plaintext = Files.readAllBytes(inputFile.toPath());
byte[] ciphertext = aead.encrypt(plaintext, null);
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
fos.write(ciphertext);
}
}
private static void decryptFile(Aead aead, File inputFile, File outputFile) throws Exception {
byte[] ciphertext = Files.readAllBytes(inputFile.toPath());
byte[] decryptedText = aead.decrypt(ciphertext, null);
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
fos.write(decryptedText);
}
}
}