1

您好,我正在搜索免费的 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。不过暂时没什么好说的。

谢谢。

4

1 回答 1

2

您可以使用 CipherOutputStream 和 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) {
    }
}
于 2012-05-18T07:15:59.930 回答