这种itext数字签名的实现有什么问题? [英] What is wrong in this implementation of itext digital signing?

查看:106
本文介绍了这种itext数字签名的实现有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从网上拿了一个示例代码,我尝试签署一份文件。

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

这是代码

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.

推荐答案

我能够使用itext5.3.4和bcprov-jdk15on-1.47.jar成功运行样本

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

这篇关于这种itext数字签名的实现有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆