java - android加密和解密pdf文件的方法

标签 java android encryption

您好,我正在搜索免费的 api 或一些简单的代码来加密和解密 pdf 文件。从流中下载文件时应进行加密:

            while ((bufferLength = inputStream.read(buffer)) > 0) {

                /*
                 * Writes bufferLength characters starting at 0 in buffer to the target
                 * 
                 * buffer the non-null character array to write. 0 the index
                 * of the first character in buffer to write. bufferLength the maximum
                 * number of characters to write.
                 */
                fileOutput.write(buffer, 0, bufferLength);


            }

并在需要用pdf阅读器打开时解密。也许有一些信息、代码或免费的 Api? 有人做过这样的事吗?

我给自己找了一些代码和 api。但目前没有什么好处。

谢谢。

最佳答案

您可以使用 CipherOuputStream 和 CipherInputStream 来尝试:

byte[] buf = new byte[1024];

加密:

public void encrypt(InputStream in, OutputStream out) {
    try {
        // Bytes written to out will be encrypted
        out = new CipherOutputStream(out, ecipher);

        // Read in the cleartext bytes and write to out to encrypt
        int numRead = 0;
        while ((numRead = in.read(buf)) >= 0) {
            out.write(buf, 0, numRead);
        }
        out.close();
    } catch (java.io.IOException e) {
    }
}

解密:

public void decrypt(InputStream in, OutputStream out) {
    try {
        // Bytes read from in will be decrypted
        in = new CipherInputStream(in, dcipher);

        // Read in the decrypted bytes and write the cleartext to out
        int numRead = 0;
        while ((numRead = in.read(buf)) >= 0) {
            out.write(buf, 0, numRead);
        }
        out.close();
    } catch (java.io.IOException e) {
    }
}

关于java - android加密和解密pdf文件的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10648210/

相关文章:

java - 使用 Spring 和 HTML5 瓶颈搜索/清理本地视频

java - 如何从内部匹配值获取节点键

java - Android 应用程序自动化脚本 : Through Appium, Selenium WebDriver,Java:NoSuchElementFoundException

algorithm - 为什么需要大量随机性才能进行有效加密?

调用方法时出现 Java ClassCastException

Java:间隔中的随机长值

java - 列表到 Java 8 中的 BiMap

android - 如何在android中为wav文件添加回声效果?

php - MYSQL注入(inject)和md5加密

Java - Cipher 自定义提供程序