Microsoft Windows Phone 8.1 support ends (13 Jul 2017)

Microsoft has ended support for Windows Phone 8.1

Questions about Android development and PDF

PDF encryption

More
9 years 10 months ago - 9 years 10 months ago #11403 by miles.
PDF encryption was created by miles.
Hey,

So I am trying to figure out how to do PDFencryption.
What I want to achieve is:
1. Encrypt byte stream and save it locally
2. Decrypt and display
I see there is a method "EncryptAs" so I would imagine I could encrypt and save (replace) existing file on a system, but then how would I open it?
I am also not entirely sure what EncryptAs some parameters mean, like "method" - what int is for what mode? "id"?
Last edit: 9 years 10 months ago by miles..
More
9 years 10 months ago - 9 years 10 months ago #11406 by Davide
Replied by Davide on topic PDF encryption
Hi,
I suggest you to follow this guide that shows how to encrypt a pdf and some further info about the params : www.radaeepdf.com/support/knowledge-base...component&kbartid=29
In your case you should use OpenMem ( www.radaeepdf.com/documentation/javadocs...:A-java.lang.String- ) method instead of Open to open your byte stream document.

Here you find others details about the encrypt method and its params :
www.radaeepdf.com/documentation/javadocs...ring-int-int-byte:A-

I suggest you to get document ID and permissions from the original PDF and use them into the EcryptAs method.
Code:
byte[] mDocId = new byte[32]; System.arraycopy(mDocument.GetID(0), 0, mDocId, 0, 16); System.arraycopy(mDocument.GetID(1), 0, mDocId, 16, 16); mDocument.GetPermission();

To decrypt it you have to enter the password or pass it to the open method.
Last edit: 9 years 10 months ago by Davide.
More
9 years 10 months ago #11409 by miles.
Replied by miles. on topic PDF encryption
This is how I encrypt the file
Code:
// Save and store Document tempdoc = new Document(); tempdoc.OpenStream(new PDFMemStream(bytearray), "userPassword"); tempdoc.EncryptAs(context.getFilesDir().getAbsolutePath()+"/"+filename, "userPassword", "ownerPassword", 4,4,id);

The problem I have above is that If I try to open the file, it will ask just for user password and you can read the PDF. So, I don't think it go encrypted

And that's how I load the file
Code:
// Load Document tempdoc = new Document(); Integer errorCode = 0; errorCode = tempdoc.Open(context.getFilesDir().getAbsolutePath() + "/" + filename, "userPassword"); if (errorCode == 0) errorCode = tempdoc.Open(context.getFilesDir().getAbsolutePath() + "/" + filename, "ownerPassword";

Problem with loading the file is first - a crash with basically no information occurs sometimes when switching between PDFs . And second - when making annotations the PDF file seem to save annotation automatically which undesired behaviour and how do i turn it off? as everytime i open PDF i just want a fresh pdf annotations-free.
More
9 years 10 months ago - 9 years 10 months ago #11411 by Davide
Replied by Davide on topic PDF encryption
Hi,
first of all with the code you sent us, the pdf is well encrypted (with password).
As written in the guide I sent you :

user password: is the public password (shared with authorized users). It's sufficient to open the file with applied permission mask set.
owner password: is the password that allow opening the file without any restriction from the permission mask.

So you can correctly load the pdf with the user password, if the errorCode is 0, means that the pdf is correctly opened, so you don't need to re-open it again.
Code:
// Load Document tempdoc = new Document(); Integer errorCode = 0; errorCode = tempdoc.Open(context.getFilesDir().getAbsolutePath() + "/" + filename, "userPassword"); if (errorCode == 0) //pdf correctly opened

Can you please better explain what does it mean "when switching between PDFs"? Can you reproduce that with the demo project? With which code?

There is no "save automatically" function, the only way to save the pdf is to call:
Code:
m_doc.Save();
So, check if you call this code somewhere..
Last edit: 9 years 10 months ago by Davide.
More
9 years 10 months ago #11412 by miles.
Replied by miles. on topic PDF encryption
I thought encryption means encrypting the bytes making file unusable without decryption algorithm. If PDF is just password protected then it can be brute force unlocked which is not good enough.
So how come using EncryptAs is different from having just a password on PDF? Or this what we call encryption in this case - putting password on?
More
9 years 10 months ago #11413 by Davide
Replied by Davide on topic PDF encryption
Hi,
using the EncryptAs method you can encrypt your pdf in a standard way as writte in the java doc : www.radaeepdf.com/documentation/javadocs...ring-int-int-byte:A-

method - set 3 means using AES 256bits encrypt(Acrobat X), V=5 and R = 6 mode, others AES with V=4 and R=4 mode.

So it's sufficient to have the password to open it, as the decryption is done internally.

If you don't want this kind of encryption, you can implment it as you want, I suggest you to check the PDFAESStream class of the demo project.
Time to create page: 0.435 seconds
Powered by Kunena Forum