What is wrong in this implementation of itext digi

2019-04-13 09:24发布

问题:

I took a sample code from internet and i am tryign to sign one document.

this is the code

public class SimpleSignature {

    public static final String KEYSTORE = "c:/key/sano";
    public static final char[] PASSWORD = "Chennai".toCharArray();
    public static final String SRC = "c:/itext/unsigned.pdf";
    public static final String DEST = "c:/itext/signed.pdf";


    public void sign(String src, String dest,
            Certificate[] chain, PrivateKey pk, String digestAlgorithm, String provider,
            CryptoStandard subfilter, String reason, String location)
                    throws GeneralSecurityException, IOException, DocumentException {
        // Creating the reader and the stamper
        PdfReader reader = new PdfReader(src);
        FileOutputStream os = new FileOutputStream(dest);
        PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
        // Creating the appearance
        PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
        appearance.setReason(reason);
        appearance.setLocation(location);
        appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
        // Creating the signature
        ExternalDigest digest = new BouncyCastleDigest();
        ExternalSignature signature =
                new PrivateKeySignature(pk, digestAlgorithm, provider);
        MakeSignature.signDetached(appearance, digest, signature, chain,
                null, null, null, 0, subfilter);
    }

    public static void main(String[] args)
            throws GeneralSecurityException, IOException, DocumentException {
        BouncyCastleProvider provider = new BouncyCastleProvider();
        Security.addProvider(provider);
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(new FileInputStream(KEYSTORE), PASSWORD);
        String alias = (String)ks.aliases().nextElement();
        PrivateKey pk = (PrivateKey) ks.getKey(alias, PASSWORD);
        Certificate[] chain = ks.getCertificateChain(alias);
        SimpleSignature app = new SimpleSignature();
        app.sign(SRC, String.format(DEST, 1), chain, pk, DigestAlgorithms.SHA256,
                provider.getName(), CryptoStandard.CMS, "Test 1", "Ghent");
        app.sign(SRC, String.format(DEST, 2), chain, pk, DigestAlgorithms.SHA512,
                provider.getName(), CryptoStandard.CMS, "Test 2", "Ghent");
        app.sign(SRC, String.format(DEST, 3), chain, pk, DigestAlgorithms.SHA256,
                provider.getName(), CryptoStandard.CADES, "Test 3", "Ghent");
        app.sign(SRC, String.format(DEST, 4), chain, pk, DigestAlgorithms.RIPEMD160,
                provider.getName(), CryptoStandard.CADES, "Test 4", "Ghent");
    }
}

When i run this code. I am getting this error.

Exception in thread "main" java.lang.NoSuchMethodError: org.bouncycastle.asn1.ASN1Integer.<init>(J)V
    at com.itextpdf.text.pdf.security.PdfPKCS7.getEncodedPKCS7(PdfPKCS7.java:797)
    at com.itextpdf.text.pdf.security.MakeSignature.signDetached(MakeSignature.java:149)
    at org.allcolor.yahp.sample.SimpleSignature.sign(SimpleSignature.java:45)
    at org.allcolor.yahp.sample.SimpleSignature.main(SimpleSignature.java:62)

Any idea why i am getting this error.

回答1:

I was able to successfully run the sample by using itext5.3.4 with bcprov-jdk15on-1.47.jar



标签: java itext