aspose file tools*
The moose likes Other Open Source Projects and the fly likes iText: Digital signature Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "iText: Digital signature" Watch "iText: Digital signature" New topic
Author

iText: Digital signature

Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
It seems you're back to a point where this isn't even working for a desktop app? So the problem is not with making it work in a web app, but in getting the fundamentals right?

If you have a problem, then please describe the problem. Whatever steps you tried to follow is irrelevant (and just clutters up this thread) if you don't tell us what, specifically, the problem is, and what, specifically, you did when you encountered it. "it is giving input not an X.509 certificate" is, I'm afraid, useless as a problem description.
rakhi sinha
Ranch Hand

Joined: Mar 26, 2012
Posts: 147
Tim Moores wrote:It seems you're back to a point where this isn't even working for a desktop app? So the problem is not with making it work in a web app, but in getting the fundamentals right?

If you have a problem, then please describe the problem. Whatever steps you tried to follow is irrelevant (and just clutters up this thread) if you don't tell us what, specifically, the problem is, and what, specifically, you did when you encountered it. "it is giving input not an X.509 certificate" is, I'm afraid, useless as a problem description.





i have created the foobar certificate with the help of itext tutorial but how to import root certificate and how to certify foobar certificate from certifying authority..and then how to use keys in the certificate for digital signature in pdf ......i am pasting code here please tell me the error so that i can solve my problem ...i dont know but it may be the problem related to pkcs format..




import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.*;
import org.bouncycastle.jce.provider.symmetric.SEED;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.DocumentException;


public class digital extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);

}
private static final long serialVersionUID = 1L;

KeyStore ks= null;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

Document document = new Document();


try {
try {
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("/resources/a.pdf"));
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ks = KeyStore.getInstance(KeyStore.getDefaultType());
} catch (KeyStoreException ex) {
Logger.getLogger(SEED.class.getName()).log(Level.SEVERE, null, ex);
}
try {
String f = getServletContext().getRealPath("/resources/.keystore");

ks.load(
new FileInputStream(f),"abcdef".toCharArray());
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(SEED.class.getName()).log(Level.SEVERE, null, ex);
} catch (CertificateException ex) {
Logger.getLogger(SEED.class.getName()).log(Level.SEVERE, null, ex);
}
PrivateKey key= null;
try {
key = (PrivateKey) ks.getKey("foobar", "abcdef".toCharArray());
} catch (KeyStoreException ex) {
Logger.getLogger(SEED.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(SEED.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnrecoverableKeyException ex) {
Logger.getLogger(SEED.class.getName()).log(Level.SEVERE, null, ex);
}
Certificate[] chain= null;
try {
chain = ks.getCertificateChain("foobar");
} catch (KeyStoreException ex) {
Logger.getLogger(SEED.class.getName()).log(Level.SEVERE, null, ex);
}
PdfReader reader = new PdfReader(getServletContext().getRealPath("/resources/a.pdf"));
FileOutputStream os = new FileOutputStream("/resources/Signed.pdf");
PdfStamper stamper = null;
try {
stamper = PdfStamper.createSignature(reader, os, '\0');
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setCrypto(key, chain, null,PdfSignatureAppearance.SELF_SIGNED);
appearance.setReason("It's personal.");
appearance.setLocation("Foobar");
appearance.setVisibleSignature(new Rectangle(160,732,232,780), 1, null);
try {
stamper.close();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}

}


and i am getting following error.
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.io.FileNotFoundException: \resources\a.pdf (The system cannot find the path specified)
java.io.FileOutputStream.open(Native Method)
java.io.FileOutputStream.<init>(Unknown Source)
java.io.FileOutputStream.<init>(Unknown Source)
archana.digital.doGet(digital.java:44)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.


--------------------------------------------------------------------------------

Apache Tomcat/6.0.16




In tomcat there is no full stack trace .log folder is empty
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
You're continuously ignoring my suggestions. Here it is, once more:
Tim Moores wrote:This part of the question still stands:
Or do you really have a root directory called "resources" on your disk?

It's the FileOutputStream handling where the exception occurs, after all.


You did note that the exception happens not when the code is trying to access any certificate, but when it tries to deal with the PDF file, right? And you understand the difference between a directory sitting at the root of a web app and a directory sitting at the root of the file system, yes? And that consequently there are differences between how files are handled by the file access classes in java.io and the way getRealPath works in a servlet environment? I'm asking because it seems that you're still confused about these differences. Nothing I see about the exception suggest to me that the current problem has anything to do with certificates.
rakhi sinha
Ranch Hand

Joined: Mar 26, 2012
Posts: 147
Tim Moores wrote:You're continuously ignoring my suggestions. Here it is, once more:
Tim Moores wrote:This part of the question still stands:
Or do you really have a root directory called "resources" on your disk?

It's the FileOutputStream handling where the exception occurs, after all.


You did note that the exception happens not when the code is trying to access any certificate, but when it tries to deal with the PDF file, right? And you understand the difference between a directory sitting at the root of a web app and a directory sitting at the root of the file system, yes? And that consequently there are differences between how files are handled by the file access classes in java.io and the way getRealPath works in a servlet environment? I'm asking because it seems that you're still confused about these differences. Nothing I see about the exception suggest to me that the current problem has anything to do with certificates.



i am using eclipse .i have created resources folder under the web-content .......
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
In other words, the answer to my question "Or do you really have a root directory called "resources" on your disk?" is no, you do not. And my suspicion that you do not understand the difference between a web app root and a file system root is actually correct.

In short: the path you're passing to FileOutputStream does not exist, just like the error message tells you. You need to use getRealPath to construct the proper path.
rakhi sinha
Ranch Hand

Joined: Mar 26, 2012
Posts: 147
Tim Moores wrote:In other words, the answer to my question "Or do you really have a root directory called "resources" on your disk?" is no, you do not. And my suspicion that you do not understand the difference between a web app root and a file system root is actually correct.

In short: the path you're passing to FileOutputStream does not exist, just like the error message tells you. You need to use getRealPath to construct the proper path.


but i am using getrealpath in my program....
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Not where the exception is thrown. You haven't even tried to figure out in which instruction the exception happens, have you?

By the way, you should drop the habit of quoting back entire posts - it really serves no purpose but to make the thread harder to follow. Quoting is for extracting specific pieces of a previous post that you're responding to.
rakhi sinha
Ranch Hand

Joined: Mar 26, 2012
Posts: 147
Tim Moores wrote:Not where the exception is thrown. You haven't even tried to figure out in which instruction the exception happens, have you?

By the way, you should drop the habit of quoting back entire posts - it really serves no purpose but to make the thread harder to follow. Quoting is for extracting specific pieces of a previous post that you're responding to.



can you help me out to find out where the exception is thrown......
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
I already mentioned that twice. And I addressed all the other problems you had along the way twice each at least. As I said - you don't seem to be paying attention to what I post. Which makes me wonder: why do you come back here asking for more advice if you don't heed the advice you already got?
 
 
subject: iText: Digital signature