· 6 years ago · Feb 10, 2019, 03:42 AM
1import java.security.*;
2import javax.crypto.*;
3import javax.crypto.spec.*;
4import java.io.*;
5
6// First argument: decryption key
7// Second argument: file input path
8// Third argument: file output path
9
10public class Main {
11 public static void main(String[] args) throws InvalidAlgorithmParameterException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException {
12 System.out.println("Processing file: " + args[1]);
13
14 String key = args[0];
15 File fileIn = new File(args[1]), fileOut = new File(args[2]);
16
17 Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
18 SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
19 cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(new byte[16]));
20
21 InputStream in = new FileInputStream(fileIn);
22 byte[] encrypted = new byte[(int)fileIn.length()];
23 in.read(encrypted);
24 in.close();
25
26 byte[] decrypted = cipher.doFinal(encrypted);
27 OutputStream out = new FileOutputStream(fileOut);
28 out.write(decrypted);
29 out.close();
30
31 System.out.println("Done: outputted file to " + args[2]);
32 }
33}