0

I'm downloading a file that is stored on a remote server, I try to decrypt it using JNCryptor, and all goes well except that the file I have downloaded and store in the phone external storage is corrupted and I cannot open it. Can anyone tell me where im going wrong?

Im trying to get the InputStream from the file, decrypt it, and save the file on external storage.

Thanks

Here is my code:

private void downloadFile() {
        final String FILE_URL = "https://www.google.com";
        final String PASS = "password";


        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                Log.d(TAG, "starting");


                JNCryptor cryptor = new AES256JNCryptor();

                int count;
                try {
                    URL url = new URL(FILE_URL);
                    URLConnection conection = url.openConnection();
                    conection.connect();

                    // this will be useful so that you can show a tipical 0-100%
                    // progress bar
                    int lenghtOfFile = conection.getContentLength();
                    // download the file
                    InputStream input = new BufferedInputStream(url.openStream(),
                            8192);

                    //decrypt istream
                    byte[] b = null;
                    byte[] data = null;
                    try {
                        b = new byte[input.available()];
                        input.read(b);
                    } catch (IOException e) {
                        Log.i("decrypt error", e.toString());
                    }

                    AES256JNCryptorOutputStream cryptorStream = null;

                    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

                    try {
                        cryptorStream = new AES256JNCryptorOutputStream(byteStream,
                                PASS.toCharArray());

                    } catch (CryptorException e) {
                        e.printStackTrace();
                    }

                    try {
                        cryptorStream.write(b);
                        cryptorStream.flush();
                        cryptorStream.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }

                    byte[] encrypted = byteStream.toByteArray();

                    try {
                        data = cryptor.decryptData(encrypted, PASS.toCharArray());
                        Log.d(TAG, "decrypted");
                    } catch (InvalidHMACException e) {
                        e.printStackTrace();
                    } catch (CryptorException e) {
                        e.printStackTrace();
                    }

                    if (data != null) {
                        Log.d(TAG, "data is ok");
                    }

                    //end decryption

                    // Output stream
                    //test
                    FileOutputStream fos = new FileOutputStream(Environment
                            .getExternalStorageDirectory().toString()
                            + "/temp.zip");
                    fos.write(data);
                    fos.close();
                    Log.d(TAG, "file saved ");

                    input.close();
                    Log.d(TAG, "done");
                } catch (Exception e) {
                    Log.d(TAG, "Error: " + e.getMessage());
                }
                return null;
            }


        }.execute();

    }

P.S. Im not getting any error or warning in logCat.

Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117
  • corrupted that means you're not getting that file from server. – M D Jul 08 '15 at 12:30
  • @MD I'm getting the file, but I cannot open it. Sorry – Darko Petkovski Jul 08 '15 at 12:31
  • If you decrypt and download to file to a PC, or other device, does the file work then? That might help localise the problem to either decryption or transfer. What format is the encrypted file in, bytes or Base64 text? – rossum Jul 08 '15 at 14:13
  • @rossum nope same problem. I've tried opening the file on my pc and on my phone – Darko Petkovski Jul 08 '15 at 14:54
  • If that is the case, then the problem is likely either happening on the server of during the transfer. Make sure that the storage and transfer mechanisms are suitable for byte-data or text as appropriate. Different newline formats can also mess up text data, such as Base64. Can you test the decryption on a local file, rather than one downloaded from the database? – rossum Jul 08 '15 at 16:20

0 Answers0