您好,我正在搜索免费的 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/