I have an encryption / decryption mechanism for uploaded files in my application.
This mechanism works in all Android devices and versions up to version 5.0-lollipop.
Here is the decryption process:
cipher.init(Cipher.DECRYPT_MODE, key); fileInputStream = new FileInputStream(file); cipherInputStream = new CipherInputStream(fileInputStream, cipher); byte[] fileByte = new byte[(int) file.length()]; int j = cipherInputStream.read(fileByte); return fileByte;
the cipher and key were created earlier and are used in the whole application:
key = new SecretKeySpec(keyValue, "AES"); try { cipher = Cipher.getInstance("AES"); } catch (Exception e) { e.printStackTrace(); }
When I decrypt a file of about 200,000 bytes in android 5.0, j (variable before returning) is about 8000, which is much lower than 200000, and in older versions for Android it is exactly equal to the decrypted file length.
I found that the problem is decryption. Because I can encrypt the file in android 5.0 and decrypt it in older versions of Android, but not vice versa. However, I am sending the encryption process:
cipher.init(Cipher.ENCRYPT_MODE, AESutil.key); cipherOutputStream = new CipherOutputStream(output, cipher); byte data[] = new byte[1024]; int count; while ((count = input.read(data)) != -1) { cipherOutputStream.write(data, 0, count); }
Thanks in advance
source share