0

我正在下载一个存储在远程服务器上的文件,我尝试使用JNCryptor对其进行解密,一切顺利,除了我下载并存储在手机外部存储中的文件已损坏并且我无法打开它。谁能告诉我我哪里出错了?

我试图从文件中获取 InputStream,对其进行解密,然后将文件保存在外部存储中。

谢谢

这是我的代码:

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();

    }

PS 我在 logCat 中没有收到任何错误或警告。

4

0 回答 0