Reputation: 4401
Hello i am searching free api or some easy code to encrypt and decrypt pdf files. Encryption should be done on downloading file from a stream:
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);
}
And decrypt when need to open with pdf reader. Maybe there are some info, code or free Api for this ? Someone had done something like this ?
I found myself some code and api. But nothing good for now.
Thanks.
Upvotes: 1
Views: 4555
Reputation: 9914
You can try like this using CipherOuputStream and CipherInputStream:
byte[] buf = new byte[1024];
Encryption:
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) {
}
}
Decryption:
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) {
}
}
Upvotes: 2