2

I have an application in which I am using the code to decrypt the file which is already encrypted. The file location is "/mnt/sdcard/myfolder/test.mp4". The size of test.mp4 file is approx 20MB.

When I am using the following code to decrypt the encrypted files of small size, the files are successfully decrypted but when I am trying to decrypt the large video files, an exception of outOfMemoryException is occured.

Here is the code :

FileOutputStream fos = new FileOutputStream(outFilePath);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] keyBytes= new byte[16];
        //byte[] b= key.getBytes(Charset.forName("UTF-8"));
        byte[] b= key.getBytes("UTF-8");
        Log.i("b",""+b);
        int len= b.length;
        Log.i("len",""+len);
        if (len > keyBytes.length) len = keyBytes.length;
        System.arraycopy(b, 0, keyBytes, 0, len);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
        cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);

        byte[] results = new byte[cipher.getOutputSize(abc.length)];

        try
        {
            Log.i("output size:", ""+cipher.getOutputSize(abc.length));
            ***results = cipher.doFinal(abc);***
        }
        catch (Exception e) {
            // TODO: handle exception
            Log.e("EXCEPTION:", e.getMessage());
        }
        fos.write(results);

NOTE: byte[] abc = new byte[64]; contains the input byte array.

Maddy Sharma
  • 4,870
  • 2
  • 34
  • 40

1 Answers1

2

From your question, or at least from the code you posted, there is nothing that would couse OutOfMemoryException, especially since array abc is only 64 bytes long. But you said you get the exception when working with large files. So my inference,

Somewhere in your code (not in posted part), you are trying to read full file into any array, or trying to hold it in array. Android does impose a memory limit on application (16 MB for most devices), this limit includes the memory used for UI elements. So there is not much memory there for you to play with.

Now ideally, what you should do is to create a decrypt block, that works with streams. CipherInputStream does looks promising. And this stackoverflow thread, might be of interest if you are thinking of using CipherInputStream.

Community
  • 1
  • 1
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • Can you plz tell Me if there is any solution for this? – Maddy Sharma Dec 04 '12 at 11:40
  • @Maddy again, nothing in your posted code that would cause OutOfMemory exception. If you want to read a big video file and decrypt, your only option is to use streams. There is no way you can read entire bytes from the file and store it in an array. Have you seen the stack overflow thread I posted. There they are reading a creating a bitmap after decrypting.. You might have to do something like that.. – Krishnabhadra Dec 04 '12 at 11:46
  • 2
    Yeah.., I got it.. Are u trying to say like that : Is this link helpful according to u?http://stackoverflow.com/questions/9496447/encryption-of-video-files – Maddy Sharma Dec 04 '12 at 11:56
  • oops I haven't seen that before.. I think that might work. good luck – Krishnabhadra Dec 04 '12 at 12:00
  • I telling u that I am not able to create decrypted large file although small size file working fine with that code. – Maddy Sharma Dec 04 '12 at 12:02
  • The above is not meet maa requirements dear. I have already encrypted vedio files. I decrypt these files and after decryption trying to store in results(byte[] variable) byte array as I show here, but I got OutOfMemoryException when I am using this line : results = cipher.doFinal(abc); – Maddy Sharma Dec 04 '12 at 12:10
  • Okay, but how many bytes do you need to store in the byte array – Krishnabhadra Dec 04 '12 at 12:13
  • 12-04 17:53:07.324: E/dalvikvm-heap(12174): Out of memory on a 20789406-byte allocation. – Maddy Sharma Dec 04 '12 at 12:23
  • Are u there...?? Can u plz help for further? – Maddy Sharma Dec 04 '12 at 12:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20568/discussion-between-maddy-and-krishnabhadra) – Maddy Sharma Dec 04 '12 at 12:50