Signin/Signup with: 
Welcome, Guest
Username: Password: Remember me
Questions about Android development and PDF
  • Page:
  • 1

TOPIC:

PDF encryption 7 years 5 months ago #11403

  • miles.
  • miles.'s Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 4
  • Thank you received: 0
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"?

Please Log in or Create an account to join the conversation.

Last edit: by miles..

PDF encryption 7 years 5 months ago #11406

  • Davide
  • Davide's Avatar
  • Offline
  • User is blocked
  • User is blocked
  • Posts: 814
  • Thank you received: 65
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.
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.

Please Log in or Create an account to join the conversation.

Last edit: by Davide.

PDF encryption 7 years 5 months ago #11409

  • miles.
  • miles.'s Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 4
  • Thank you received: 0
This is how I encrypt the file
// 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
// 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.

Please Log in or Create an account to join the conversation.

PDF encryption 7 years 5 months ago #11411

  • Davide
  • Davide's Avatar
  • Offline
  • User is blocked
  • User is blocked
  • Posts: 814
  • Thank you received: 65
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.
// 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:
m_doc.Save();
So, check if you call this code somewhere..

Please Log in or Create an account to join the conversation.

Last edit: by Davide.

PDF encryption 7 years 5 months ago #11412

  • miles.
  • miles.'s Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • Posts: 4
  • Thank you received: 0
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?

Please Log in or Create an account to join the conversation.

PDF encryption 7 years 5 months ago #11413

  • Davide
  • Davide's Avatar
  • Offline
  • User is blocked
  • User is blocked
  • Posts: 814
  • Thank you received: 65
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.

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
Powered by Kunena Forum