[ jovan1982 @ 09.07.2012. 09:42 ] @
Zdravo! Pisem desktop aplikaciju(koju cu po uspesnom zavrsetku da konvertujem u web aplet) za potpisivanje pdf-a koristeci itext biblioteku. Evo koda /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javapotpisivanje7; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.*; import com.itextpdf.text.pdf.security.PdfPKCS7; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.security.*; import java.security.cert.CertificateException; import java.util.Calendar; import java.util.Enumeration; import java.util.HashMap; import javax.security.cert.X509Certificate; /** * * @author Jovo */ public class JavaPotpisivanje7 { /** * @param args the command line arguments */ public static void main(String[] args) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, DocumentException, SignatureException, InvalidKeyException, NoSuchProviderException { KeyStore ks = KeyStore.getInstance("Windows-MY"); ks.load(null, null) ; //deo koji dodajem //dodajem Enumeration en = ks.aliases() ; String alias = (String)ks.aliases().nextElement(); PrivateKey key = (PrivateKey)ks.getKey(alias, "password".toCharArray()); java.security.cert.Certificate[] chain = ks.getCertificateChain(alias); PdfReader reader = new PdfReader("Za Potpis.pdf"); FileOutputStream fout = new FileOutputStream("signed.pdf"); PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0',null,true); PdfSignatureAppearance sap = stp.getSignatureAppearance(); sap.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, null); sap.setLayer2Text("Ovde ce da bude potpis.\n\nDate: some date"); Calendar cal = Calendar.getInstance(); PdfDictionary dic = new PdfDictionary(); dic.put(PdfName.FT, PdfName.SIG); dic.put(PdfName.FILTER, new PdfName("SAFE.PPKSF")); dic.put(PdfName.SUBFILTER, new PdfName("adbe.pkcs7.detached")); dic.put(PdfName.M, new PdfDate(cal)); //dic.put(PdfName.NAME, new PdfString(PdfPKCS7.getSubjectFields((X509Certificate)chain[0]).getField("CN"))); sap.setCryptoDictionary(dic); HashMap exc = new HashMap(); exc.put(PdfName.CONTENTS, new Integer(0x2502)); sap.preClose(exc); //PdfPKCS7 pk7 = new PdfPKCS7(key, chain, null, "SHA1", null, false); //PdfPKCS7 pk7 = new PdfPKCS7(key, chain, null, "SHA1", null, false); //PdfPKCS7 pk7 = new PdfPKCS7(null, chain, null, "SHA1", true); //PdfPKCS7 pk7 = new PdfPKCS7(key, chain, null, "SHA1", null, false); //dodajem //PdfPKCS7 pk7 = new PdfPKCS7(key, chain, alias, alias, true); PdfPKCS7 pk7 = new PdfPKCS7(key, chain, "SHA1", null, false); //dodajem MessageDigest messageDigest = MessageDigest.getInstance("SHA1"); byte buf[] = new byte[8192]; int n; InputStream inp = sap.getRangeStream(); while ((n = inp.read(buf)) > 0) { messageDigest.update(buf, 0, n); } byte hash[] = messageDigest.digest(); byte sh[] = pk7.getAuthenticatedAttributeBytes(hash, cal, hash, null, true); pk7.update(sh, 0, sh.length); PdfDictionary dic2 = new PdfDictionary(); byte sg[] = pk7.getEncodedPKCS7(hash, cal); byte out[] = new byte[0x2500 / 2]; System.arraycopy(sg, 0, out, 0, sg.length); dic2.put(PdfName.CONTENTS, new PdfString(out).setHexWriting(true)); sap.close(dic2); } } SVE U SVEMU,PO POKRETANJU APLIKACIJE KREIRA SE NOVO POLJE ALI SE NE POTPISE-POZOVE SE I KARTICA I TRAZI SE PIN BROJ. dic.put(PdfName.NAME, new PdfString(PdfPKCS7.getSubjectFields((X509Certificate)chain[0]).getField("CN")));//OVAJ DEO MI PRAVI PROBLEME PRETPOSTAVLJAM DA DEO KOJI PRAVI PROBLEME KOJI JE U OVOM MOMENTU POD // GDE BI TREBALO DA SE POZIVA SAMO POLJE KADA SE SKINE KOMENTAR PRIJAVLJUJE SE GRESKA. U DELU KeyStore ks = KeyStore.getInstance("Windows-MY"); ks.load(null, null) ; TAKODJE MOZE BITI PROBLEM... AKO NEKO ZNA ZASTO I KAKO DA RESIM OVAJ PROBLEM BILO BI SUPER. ZANIMA ME DA LI SAM NA DOBROM PUTU. |