i'm trying to secure my pdf. I have used this code :
stamper.setEncryption(USER.getBytes(), OWNER.getBytes(),
PdfWriter.ALLOW_FILL_IN | PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_ASSEMBLY, PdfWriter.STANDARD_ENCRYPTION_128);
My problem is i don't want to enter password to see the pdf, i just want a password if someone try to modify the pdf. I can't find my solution on forum.
If you don't want to enter a password for viewing, simply use an empty user password.
When opening an encrypted PDF, a PDF viewer is expected to first try to decrypt it using the empty password and ask for the password only if that fails.
Related
I am working on a project which have to access the MySQL database username and password to read and update the user database.
Initially i wrote the username and password of the database directly to my code.
But my teacher asked me to create a prompt box which will take the username and password on 1st run of the program and not again.
So if do that i will not be able to access the database next time.
I was thinking to store that username and password into a local text file.
Is it good idea.
Or there are any good methods to do this type of work?
You can store the information in a properties file (https://docs.oracle.com/javase/tutorial/essential/environment/properties.html), but should use encryption. See: How to encrypt String in Java
In your case, the best way will be storing in .properties file.
And after getting a user input => store to the properties file.
Also, good practice for storing passwords in DB is to use one-way hash. A variety of hash methods is good for this: MD5, SHA-256, etc.
However, it works only for one way. More info here - MD5 algorithm Decryption in java.
And in your case properties file should be enough.
Example for db.properties:
db.username=MyUser
db.password=MyPassword
You can have default values for connection. If user input doesn't match with it just print a warning message with something, like: "DB username or password is incorrect. Try again."
You can use something like JOptionPane for asking from user:
public void start() throws CreateDocumentConfigurationException {
// Custom button text
Object[] options = {"Yes, please", "Use default instead"};
int n = JOptionPane.showOptionDialog(null,
"Would you like to enter DB credentials?",
"DB Question", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
estimateUserInput(n); // process result here. 0 - for entering new one, 1 - for using default
}
You can store the database login information in configuration file.
For Desktop Application,
.properties file can be use
For Web Based Application,
Store the password in context.xml of your Apache Tomcat Server.
I have looked into two libraries for doing this to no success. I am not the most experienced.
PDFBox - I think because it is a secured pdf the PDDocument class was unable to load the fields to fill.
Adobe FDFToolkit - I couldn't get the fields from the file because it was a PDF not an FDF. Not sure how to convert.
iText - org/bouncycastle/asn1/ASN1OctetString error while opening the PDF
I am having trouble getting any of these to work due to the nature of the file. It is a government immigration form which can be found here: https://www.uscis.gov/sites/default/files/files/form/i-589.pdf. Any ideas for working around this?
Your form is encrypted using an owner password. The permissions are set in such a way that they allow form filling, but iText nor PdfBox are currently fine-grained enough to check those permissions: if a PDF is encrypted, you are asked to provide a password.
However, with iText, there is a setting called unethicalreading. See How to decrypt a PDF document with the owner password? in the official documentation:
PdfReader.unethicalreading = true;
By setting this static variable to true, the PDF will be treated as if it weren't encrypted.
I have two secured pdf files. One has a password and the other one is secured but without password. I am using PDF Box.
How can I identify which file has password and which one is secured but without password?
PDF's have two type of encryption -
Owner password - Password set by PDF owner / creator to restrict its usage (e.g. edit, print, copy etc)
User password - Password set to open / view the PDF
PDF can have only owner password or both; but not only user password. In either case the PDF is termed to be encrypted and there is no direct API to distinguish between two kind of encryption.
In case of PDFBox you can use below code snippet to determine if it is encrypted or not; and distinguish whether it has only owner password or both.
PDDocument pdfDoc = PDDocument.load(new File("path/to/pdf"));
boolean hasOwnerPwd = false;
boolean hasUserPwd = false;
if(pdfDoc.isEncrypted()){
hasOwnerPwd = true;
try{
StandardDecryptionMaterial sdm = new StandardDecryptionMaterial(null);
pdfDoc.openProtection(sdm);
hasUserPwd = true;
} catch(Exception e){
// handle exception
}
}
See PDFBox API docs here and here.
EDIT Thanks to Tilman to point out latest code and alternate way to determine / distinguish between two encryption. Updated the code snippet and post accordingly.
Is it possible to set owner password as some value and user password as null or empty while using set encryption method of PdfWriter class?
I tried using code something like this
String OWNER = "test";
PdfWriter.setEncryption(null,OWNER.getBytes(),
PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
I am able to open PDF generated with this code without entering any password.
BUT when I try to open it for editing with Adobe Acrobat, it opens the document in view mode and throws an error "This is secured document. Editing is not permitted."
Screenshot of error: http://dropbox.com/s/1ef551o1z0n9ug1/editerror.jpg
Any idea why this must be occurring? Am I doing something wrong?
On an additional note,
I have generated this new document with
PdfWriter.setEncryption("test1".getBytes(),"test".getBytes(),
PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
Link: http://dropbox.com/s/8jeia7ezervrz18/Test_Success.pdf
I am able to view it after entering password as "test1" and able to edit it with password "test". I am not sure what exactly is going wrong when I pass USER as null in earlier case.
I am using following set of jars in my project
itext-2.1.7.jar
bcmail-jdk14.jar
bcprov-jdk14.jar
private static String user = "";
private static String admin = "ADMIN";
writer.setEncryption(admin.getBytes(), user.getBytes(),
PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
By using the above approach you can set admin password. There might be some problem in your classpath setting. Use Mavel on Gardle for dependencies
This is related to my other question... hope this one has a solution.
The requirement is to display a password-protected PDF in the browser but to pass the User password programatically. I create a PDF using Jasper and set the user password as follows:
exporter.setParameter(JRPdfExporterParameter.USER_PASSWORD, userPassword);
As soon as the PDF is created, it has to be displayed in the screen. While displaying in the browser, the user should not be prompted to key in the password ans hence the password should be supplied by the application However, if the user downloads the PDF and then tries to open it, he should be prompted to enter the password.
[Edit]: I am looking for an approach that does NOT involve licensed tools
You can open a password protected PDF using the PDF.JS library.
PDFJS.getDocument({ url: pdf_url, password: pdf_password }).then(function(pdf_doc) {
// success
}).catch(function(error) {
// incorrect password
// error is an object having 3 properties : name, message & code
});
I've written a blog post on it, also containing a demo. This is the link : http://usefulangle.com/post/22/pdfjs-tutorial-2-viewing-a-password-protected-pdf
I'm not sure whether something of this is possible. On the browser the pdf is opened by a Plugin - usually Adobe Reader plug-in. There are also other makes apart from Adobe Reader. Chrome has it own plugin.
On the browser when it detects any PDF file - the rendering plugin takes over - and this is browser specific. You hardly have any control.
Easy alternative is to show the same content in a web page - probably a modal window if the content is sensitive and give a link to download the password protected pdf file
my 2c
You could checkout PDF.js, an open source client based PDF renderer that also has support for encrypted PDFs.
http://mozilla.github.com/pdf.js/
This means you will have to put your password somewhere in the javascript though, so you will have to disguise it, but it should do the trick :)
You can use pdf.js of mozilla to render password protected PDF. The below url that will prompt for password, until the correct password is given. The password for the pdf is "test".
http://learnnewhere.unaux.com/pdfViewer/passwordviewer.html
Here is the sample code for prompting password
pdfJs.onPassword = function (updatePassword, reason) {
if (reason === 1) { // need a password
var new_password= prompt('Please enter a password:');
updatePassword(new_password);
} else { // Invalid password
var new_password= prompt('Invalid! Please enter a password:');
updatePassword(new_password);
}
};
If you want to close the password prompt on unsuccessful password attempts you can remove the else part(// Invalid password).
You could get the complete code from here https://github.com/learnnewhere/simpleChatApp/tree/master/pdfViewer