· 9 years ago · Jan 04, 2017, 01:10 PM
1import java.nio.file.Files;
2import java.nio.file.Paths;
3import javax.crypto.*;
4
5public class Main {
6
7 public static void main(String[] args) throws Exception {
8 String FileName = "encryptedtext.txt";
9 String FileName2 = "decryptedtext.txt";
10
11 KeyGenerator KeyGen = KeyGenerator.getInstance("AES");
12 KeyGen.init(128);
13
14 SecretKey SecKey = KeyGen.generateKey();
15
16 Cipher AesCipher = Cipher.getInstance("AES");
17
18
19 byte[] byteText = "Your Plain Text Here".getBytes();
20
21 AesCipher.init(Cipher.ENCRYPT_MODE, SecKey);
22 byte[] byteCipherText = AesCipher.doFinal(byteText);
23 Files.write(Paths.get(FileName), byteCipherText);
24
25
26 byte[] cipherText = Files.readAllBytes(Paths.get(FileName));
27
28 AesCipher.init(Cipher.DECRYPT_MODE, SecKey);
29 byte[] bytePlainText = AesCipher.doFinal(cipherText);
30 Files.write(Paths.get(FileName2), bytePlainText);
31 }
32}