user1112507
user1112507

Reputation: 131

how to decrypt a large file in java / android

main

i am looking for a way to encrypt a file and then decrypt it on an android device. currently my best solution was: encrypt file with openssl then decrypt it in java using the method shown here.

problem: the file is apparently too large (5 MB) and i get an 'out of memory' exception when running on the android emulator.

additional

i would be thankful if you add the following to your answer, though if you have an answer only to the previous section it will be fine:

  1. compression: i am using a zip archive to compress the encrypted file. this has only a minor effect (20% compression on encrypted file vs 80% on non encrypted version of this file). is there a better way to do this?
  2. encryption method: i would like to be able to compress the file using standard linux commands i.e. openssl aes-256-cbc -a -salt -in password.txt -out password.txt.enc
  3. fast over secure: i prefer a fast decryption method, even on the cost of it being not the most secure method.

Upvotes: 1

Views: 979

Answers (1)

dst
dst

Reputation: 3337

Your main issue is that you're trying to keep everything in memory (obviously). I'd recommend some changes in your workflow:

  • Get rid of Base64-encoding your files. There are multiple issues with that, one is that the files are larger than they need to be, one other is that you need to decode your encoding (which will reduce performance, even if only a tiny bit).
  • Do not load your data into memory (byte arrays). You need to address this issue, as that's where your memory issue comes from. Use streams instead of your byte array, so you only hold some chunks of data in memory.
  • As already mentioned in a comment, compress before encryption. Encrypted data should look like random numbers, and random numbers usually can't get compressed well. Note that uncompressing files also impacts your cpu performance (but usually goes along with faster transmissions)

Upvotes: 1

Related Questions