0

谁能给我一个使用 MSCAPI 和 ITEXT 从 USB 令牌签署 pdf 的简单示例。我不想使用固定的 ocsp 字符串。

我试图遵循这本 数字签名书,但没有奏效。我的代码是:

        LoggerFactory.getInstance().setLogger(new SysoLogger());
        BouncyCastleProvider providerBC = new BouncyCastleProvider();
        Security.addProvider(providerBC);
        SunMSCAPI provdierMSCAP = new SunMSCAPI();
        Security.addProvider(provdierMSCAP);
        KeyStore ks = KeyStore.getInstance("Windows-MY");
        ks.load(null, null);

        String alias = (String)ks.aliases().nextElement();
        PrivateKey pk = (PrivateKey)ks.getKey(alias, null);
        Certificate[] chain = ks.getCertificateChain(alias);


        PdfReader pdfreader = new PdfReader("HelloWorld.pdf");
        FileOutputStream outputFile = new FileOutputStream("Signed.pdf");
        PdfStamper pdfstamper;

        pdfstamper =  PdfStamper.createSignature(pdfreader, outputFile, '\0', null, true);

        PdfSignatureAppearance sap = pdfstamper.getSignatureAppearance();
        sap.setCrypto(pk, chain, null, PdfSignatureAppearance.SELF_SIGNED);
        sap.setReason("Test");
        sap.setLocation("Dhaka");

        sap.setVisibleSignature(new Rectangle(10, 10, 50, 30), 1, "Test");
        pdfstamper.close();

        JOptionPane.showMessageDialog(null, "Successfully Signed");

它给了我以下错误:

        java.lang.NullPointerException

我尝试使用 itext 5.2.1/itextpdf-5.3.5 和 bcmail-jdk16-1.46、bcprov-jdk16-1.46.jar、bctsp-jdk16-1.46.jar。

任何人都可以帮忙吗?请 ................

4

3 回答 3

0

删除 sap.setCrypto 行。你不需要它。

于 2013-06-28T08:58:10.590 回答
0

您正在将空值传递给 load(),试试这个

ks.load(new FileInputStream("certificate_path"), keystore_password.toCharArray());
于 2013-05-15T07:32:23.140 回答
0

在最新版本中,setCrypo 被以下行替换:

ExternalDigest digest = new BouncyCastleDigest();
BouncyCastleProvider provider = new BouncyCastleProvider();
ExternalSignature signature = new PrivateKeySignature(key, DigestAlgorithms.SHA256, provider.getName());
MakeSignature.signDetached(sap, digest, signature, chain,   null, null, null, 0, CryptoStandard.CMS);

您必须将bcprov-jdk15on-1.60.jarbcpkix-jdk15on-1.60.jar放在类路径中。

于 2018-11-28T16:22:00.007 回答