Java 加密和解密pdf文件的方法

Java 加密和解密pdf文件的方法,java,android,encryption,Java,Android,Encryption,你好,我正在搜索免费的api或一些简单的代码来加密和解密pdf文件。从流下载文件时应进行加密: while ((bufferLength = inputStream.read(buffer)) > 0) { /* * Writes bufferLength characters starting at 0 in buffer to the target *

你好,我正在搜索免费的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。但现在没什么好消息


谢谢。

您可以使用CipherUputStream和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) {
    }
}

谢谢,我将尝试这种方法。现在我在想,加密pdf文件可能是错误的,因为当我需要打开pdf阅读器时,我需要解密,然后文件是不安全的。也许我应该加密内部存储中的文件结构?你可以加密流,所以尝试从PDF获取流并进行加密是的,我现在正在尝试加密流,但我的应用程序PDF阅读器需要file.PDF。所以,如果我从一个流写到另一个文件,那么根设备用户可以访问解密文件?也许你知道需要做什么,根设备用户无法以任何方式获取解密文件?这就带来了安全性,如果黑客碰巧获得了该文件,他仍然可以读取它,因为它已解密。但为了防止其他人读取解密文件,您可以对.pdf文件进行密码保护。