如何加密和解密android中的pdf,doc文件

时间:2016-10-18 03:55:16

标签: android encryption

我是android新手所以我需要帮助加密和解密文件,并希望在解密后显示在Android设备中。

这里我从网址下载文件并存储在SD卡中,我现在不知道如何加密文件然后存储在SD卡中,文件大小可能超过20MB

代码: -

File downloadFile(String dwnload_file_path) {
    File file = null;
    try {
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        File folder = new File(extStorageDirectory, "SampleFolder");
        folder.mkdir();

        file = new File(folder, dest_file_path);

        try{
            file.createNewFile();
        }catch (IOException e){
            e.printStackTrace();
        }

        URL url = new URL(dwnload_file_path);
        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
        urlConnection.connect();

        InputStream inputStream = urlConnection.getInputStream();
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        int totalSize = urlConnection.getContentLength();

        byte[] buffer = new byte[MEGABYTE];
        int bufferLength = 0;
        while((bufferLength = inputStream.read(buffer))>0 ){
            fileOutputStream.write(buffer, 0, bufferLength);
        }
        fileOutputStream.close();
        //ToastManager.toast(this, "Download Complete. Open PDF Application installed in the device.");
    } catch (final MalformedURLException e) {
        //ToastManager.toast(this, "Some error occured. Press try again.");
    } catch (final IOException e) {
        //ToastManager.toast(this, "Some error occured. Press try again.");
    } catch (final Exception e) {
        //ToastManager.toast(this, "Failed to download image. Please check your internet connection.");
    }
    return file;
}

这里我在android设备中显示文件,但在解密文件后如何显示请帮帮我。

代码: -

File pdfFile = new File(Environment.getExternalStorageDirectory() + "/SampleFolder/" + "Sample."pref.getString(Constants.PrefConstants.PATH_NAME));
            File f = new File(pdfFile.toString());
            if(f.exists()) {
                    Uri path = Uri.fromFile(pdfFile);
                    Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
                    pdfIntent.setDataAndType(path, pref.getString(Constants.PrefConstants.PATH_NAME_APP));
                    //pdfIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP);
                    pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                    startActivity(pdfIntent);
                } else {
                    //uiManager.execute(Constants.Commands.REQGET_INSTRUCTIONS_SCREEN,null);
                    ToastManager.toast(getApplicationContext(), "No data available...");
                }

请帮我解决这些问题。

先谢谢 干杯

1 个答案:

答案 0 :(得分:-1)

您需要使用SecretKeySpec库。 加密方法示例

static void encrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    // Here you read the cleartext.
    FileInputStream fis = new FileInputStream("SampleFolder/yourfilename");
    // This stream write the encrypted text. This stream will be wrapped by another stream.
    FileOutputStream fos = new FileOutputStream("SampleFolder/yourencryptedfilename");

    // Length is 16 byte
    // Careful when taking user input!!! https://stackoverflow.com/a/3452620/1188357
    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
    // Create cipher
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, sks);
    // Wrap the output stream
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    // Write bytes
    int b;
    byte[] d = new byte[8];
    while((b = fis.read(d)) != -1) {
        cos.write(d, 0, b);
    }
    // Flush and close streams.
    cos.flush();
    cos.close();
    fis.close();
}

对于解密方法,请参阅以下链接。 更多详情:How to encrypt file from SD card using AES in Android?