gpt4 book ai didi

java - IText - 使用 Windows 证书存储签署文档

转载 作者:行者123 更新时间:2023-11-30 03:55:43 24 4
gpt4 key购买 nike

我想使用 itext 和智能卡签署 pdf。如果我将智能卡放入读卡器,Windows 会自动在存储中添加两个证书。我读了 Bruno Lowagie 的书,并尝试制作一个示例,添加图像和其他一些内容:

public static void main(String[] args)
throws GeneralSecurityException, IOException, DocumentException {

BouncyCastleProvider provider = new BouncyCastleProvider();
Security.addProvider(provider);

KeyStore ks = KeyStore.getInstance("Windows-MY", "SunMSCAPI");
//KeyStore ks = KeyStore.getInstance("PKCS11");
ks.load(null, PASSWORD);
String alias = (String) ks.aliases().nextElement();
PrivateKey pk = (PrivateKey) ks.getKey(alias, PASSWORD);
Certificate[] chain = ks.getCertificateChain(alias);

sign(SRC, String.format(DEST, 1), chain, pk, DigestAlgorithms.SHA256,
provider.getName(), CryptoStandard.CMS, "Test 1", "Ghent", "signHere");
sign(SRC, String.format(DEST, 2), chain, pk, DigestAlgorithms.SHA512,
provider.getName(), CryptoStandard.CMS, "Test 2", "Ghent", "signHere");
sign(SRC, String.format(DEST, 3), chain, pk, DigestAlgorithms.SHA256,
provider.getName(), CryptoStandard.CADES, "Test 3", "Ghent", "signHere");
sign(SRC, String.format(DEST, 4), chain, pk, DigestAlgorithms.RIPEMD160,
provider.getName(), CryptoStandard.CADES, "Test 4", "Ghent", "signHere");
}

public static void sign(String src, String dest, Certificate[] chain, PrivateKey pk,
String digestAlgorithm, String provider, CryptoStandard subfilter,
String reason, String location, String fieldToSign)
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(fieldToSign);
appearance.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_FORM_FILLING_AND_ANNOTATIONS);

appearance.setImage(Image.getInstance(Params.imgPath));
appearance.setImageScale(-1);

// 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);

}

但我得到的是 java.security.InvalidKeyException: 提供的 key (sun.security.mscapi.RSAPrivateKey) 不是 RSAPrivateKey 实例

为什么sun.security.mcapi.RSAPrivateKey不是RSAPrivateKey实例?我在这里做错了什么?我在 BouncyCaSTLe jars 1.49 和 1.50 上都有这个错误

最佳答案

问题在于您创建签名时传递的提供程序:

ExternalSignature signature = new PrivateKeySignature(pk, digestAlgorithm, provider);

提供程序必须是 ks.getProvider().getName()(或“SunMSCAPI”),而不是 BouncyCaSTLeProvider。

关于java - IText - 使用 Windows 证书存储签署文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23246548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com