hi ashwin , i have written a java code to add digital signature to pdf file
.

could u please help me out ....it compiles fine but while running its giving
an exception


C:\excel>java pdfsign1
java.io.IOException: Invalid keystore format
        at sun.security.provider.JavaKeyStore.engineLoad(Unknown Source)
        at sun.security.provider.JavaKeyStore$JKS.engineLoad(Unknown Source)
        at java.security.KeyStore.load(Unknown Source)
        at pdfsign1.main(pdfsign1.java:23)



i am creating a key with name "my_private_key.pfx" specified in the program
using the command
keytool -genkey -keyalg RSA -alias myname -keypass password -keystore
my_private_key.pfx

import com.lowagie.text.pdf.*;
import com.lowagie.text.Rectangle;
import java.security.*;
import java.io.*;
import java.awt.*;
import java.lang.*;
import javax.net.ssl.*;
import javax.net.ssl.KeyManager;
import javax.naming.*;
import javax.naming.Context;

public class pdfsign1{
  public static void main(String args[]) throws IOException{
try {
//KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
//KeyStore ks = KeyStore.getInstance("pkcs12");
//ks.load(new FileInputStream("D:\\pdf\\pdf2\\digital\\my_private_key.pfx"),
"my_password".toCharArray());
//kmf.init(ks, "my_password".toCharArray());

SSLContext ctx=SSLContext.getInstance("SSL");
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream("c:\\excel\\export.cer"),
"my_password".toCharArray());
kmf.init(ks, "my_password".toCharArray());
ctx.init(kmf.getKeyManagers(),null,null);

//initSSLContext(kmf.getKeyManagers());
//KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
//ks.load(new FileInputStream("keystore.ks"), "my_password".toCharArray());
String alias = (String)ks.aliases().nextElement();
PrivateKey key = (PrivateKey)ks.getKey(alias,"my_password".toCharArray());
java.security.cert.Certificate[] chain = ks.getCertificateChain(alias);
PdfReader reader = new PdfReader("original.pdf");
FileOutputStream fout = new FileOutputStream("signed.pdf");
PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0');
PdfSignatureAppearance sap = stp.getSignatureAppearance();
//sap.setCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
sap.setCrypto(key, chain, null, PdfSignatureAppearance.SELF_SIGNED);
sap.setReason("I'm the author");
sap.setLocation("Lisbon");
// comment next line to have an invisible signature
sap.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, null);
stp.close();
  }
catch(Exception e) {e.printStackTrace();}
}
}
------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables 
unlimited royalty-free distribution of the report engine 
for externally facing server and web deployment. 
http://p.sf.net/sfu/businessobjects
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to