· 8 years ago · Dec 20, 2017, 08:02 PM
1 private static void decrypt2(FileInputStream fis, FileOutputStream fos) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, IOException, BadPaddingException, IllegalBlockSizeException {
2 byte[] iv = new byte[16];
3 // NEED IV
4 Log.d(TAG, iv.toString());
5
6 SecretKeyFactory factory = SecretKeyFactory.getInstance(factoryKey);
7 KeySpec keySpec = new PBEKeySpec(key.toCharArray());
8 SecretKey temp = factory.generateSecret(keySpec);
9 SecretKey secret = new SecretKeySpec(temp.getEncoded(), "AES");
10 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
11 cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
12 byte[] in = new byte[64];
13 int read;
14 while ((read = fis.read(in)) != -1) {
15 byte[] output = cipher.update(in, 0, read);
16 if (output != null)
17 fos.write(output);
18 }
19
20 byte[] output = cipher.doFinal();
21 if (output != null)
22 fos.write(output);
23 fis.close();
24 fos.flush();
25 fos.close();
26 }