Bouncy Castle类在运行时未找到异常

时间:2016-05-17 08:08:17

标签: itext bouncycastle

尝试添加充气城堡提供商,使用iText 5.5.9对我的pdf进行数字签名。在他们的网站上添加了名为(bcprov-ext-jdk15on-154.jar)的充气城堡提供商java5-8。以下是基于Bruno的使用iText的数字签名白皮书的测试代码

static String keyStore = "F:\\keystores\\ks.jks";
static String src      = "C:\\documenttobesigned.pdf";
static String dest     = "C:\\signedDocument.pdf";

public static void main(String[] args)
{
    try
    {

        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        char[] password = "password111".toCharArray();
        BouncyCastleProvider bouncyCastleCryptoProvider = new BouncyCastleProvider();
        Security.addProvider(bouncyCastleCryptoProvider);
        File file = new File(keyStore);
        InputStream is = new FileInputStream(file);
        ks.load(is, password);
        String alias = (String) ks.aliases().nextElement();
        PrivateKey pk = (PrivateKey) ks.getKey(alias, password);
        Certificate[] chain = ks.getCertificateChain(alias);
        SignHelloWorld app = new KeyStoreTest.SignHelloWorld();
        app.sign(src, String.format(dest, 2), chain, pk, DigestAlgorithms.SHA256, bouncyCastleCryptoProvider.getName(), CryptoStandard.CMS, "Reason", "Chennai");
    }
    catch (KeyStoreException e)
    {
        e.printStackTrace();
    }
    catch (FileNotFoundException fnf)
    {
        fnf.printStackTrace();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

static class SignHelloWorld
{
    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);
    }
}

这是堆栈跟踪

Exception in thread "main" java.lang.NoClassDefFoundError: org/spongycastle/jcajce/provider/digest/MD2$Digest
at test.KeyStoreTest$SignHelloWorld.sign(KeyStoreTest.java:84)
at test.KeyStoreTest.main(KeyStoreTest.java:53)
Caused by: java.lang.ClassNotFoundException:         org.spongycastle.jcajce.provider.digest.MD2$Digest
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more

0 个答案:

没有答案