· 5 years ago · Feb 01, 2020, 08:52 PM
1 //proposed function for decryption
2 public static void decryptAESCBC(
3 String inputFile,
4 String outputFile,
5 byte[] key,
6 byte[] initialIV)
7 throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException,
8 InvalidAlgorithmParameterException, IllegalBlockSizeException, ShortBufferException, BadPaddingException,
9 IOException {
10
11 //decrypt the input file using AES in CBC
12 //the file was encrypted without using padding - didn't need it
13
14 FileOutputStream fos = new FileOutputStream(outputFile);
15 byte[] content = Files.readAllBytes(Paths.get(inputFile));
16
17 Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
18 IvParameterSpec ivSpec = new IvParameterSpec(initialIV);
19 SecretKey secret = new SecretKeySpec(key, "AES");
20 cipher.init(Cipher.DECRYPT_MODE, secret, ivSpec);
21
22 byte[] output = cipher.doFinal(content);
23 fos.write(output);
24 }
25
26......................
27decryptAESCBC("EncryptedMessage.cipher", "OriginalMessage.txt", AES_KEY, IV);
28......................