解密AES256加密文件并将其保存在手机中

时间:2015-07-08 12:27:16

标签: java android encryption inputstream jncryptor

我正在下载存储在远程服务器上的文件,我尝试使用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();

    }

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

0 个答案:

没有答案
相关问题