尝试签署文件时出现以下错误。
线程“主”org.bouncycastle.operator.OperatorCreationException 中的异常:设置异常:java.security.NoSuchAlgorithmException:没有这样的算法:org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder$1 的提供者 SunPKCS11-eToken 的 1.3.14.3.2.26。在 org.bouncycastle.cms.SignerInfoGenerator 的 org.bouncycastle.cms.SignerInfoGenerator 获取(未知来源)。(未知来源)在 org.bouncycastle.cms.SignerInfoGeneratorBuilder.createGenerator(未知来源)在 org.bouncycastle。 cms.SignerInfoGeneratorBuilder.build(Unknown Source) at org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder.build(Unknown Source) at org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder.build(Unknown Source) at testapp.Testapp.setUpProvider(Testapp. java:111) 在 testapp.Testapp。main(Testapp.java:74) 原因:java.security.NoSuchAlgorithmException:没有这样的算法:Sun.security.jca.GetInstance.getService(GetInstance.java:83) 的 sun.security.jca.GetInstance.getService(GetInstance.java:83) 的提供者 SunPKCS11-eToken 的 1.3.14.3.2.26 .security.jca.GetInstance.getInstance(GetInstance.java:202) 在 java.security.Security.getImpl(Security.java:688) 在 java.security.MessageDigest.getInstance(MessageDigest.java:233) 在 org.bouncycastle。 jcajce.ProviderJcaJceHelper.createDigest(Unknown Source) at org.bouncycastle.operator.jcajce.OperatorHelper.createDigest(Unknown Source) ... 还有 9 个 Java 结果:1java:202) at java.security.Security.getImpl(Security.java:688) at java.security.MessageDigest.getInstance(MessageDigest.java:233) at org.bouncycastle.jcajce.ProviderJcaJceHelper.createDigest(Unknown Source) at org .bouncycastle.operator.jcajce.OperatorHelper.createDigest(Unknown Source) ... 还有 9 个 Java 结果:1java:202) at java.security.Security.getImpl(Security.java:688) at java.security.MessageDigest.getInstance(MessageDigest.java:233) at org.bouncycastle.jcajce.ProviderJcaJceHelper.createDigest(Unknown Source) at org .bouncycastle.operator.jcajce.OperatorHelper.createDigest(Unknown Source) ... 还有 9 个 Java 结果:1
这是代码:
InputStream cnfStream = new ByteArrayInputStream(pkcs11config.getBytes());
Provider p = new sun.security.pkcs11.SunPKCS11(cnfStream);
Security.addProvider(p);
KeyStore ks = KeyStore.getInstance("PKCS11", p);
ks.load(null, PASSWORD);
byte[] signedData = sign(data, ks, p);
public static byte[] sign(byte[] data, KeyStore ks, Provider p) throws Exception {
String alias = ks.aliases().nextElement();
List certList = new ArrayList();
CMSTypedData msg = new CMSProcessableByteArray(data); //Data to sign
X509Certificate cert = (X509Certificate) ks.getCertificate(alias);
certList.add(cert); //Adding the X509 Certificate
Store certs = new JcaCertStore(certList);
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
//Initializing the the BC's Signer
ContentSigner sha1Signer = new JcaContentSignerBuilder("SHA1withRSA").setProvider(p).build((PrivateKey)ks.getKey(alias, PASSWORD));
gen.addSignerInfoGenerator(
new JcaSignerInfoGeneratorBuilder(
new JcaDigestCalculatorProviderBuilder().setProvider(p).build())
.build(sha1Signer, cert));
//adding the certificate
gen.addCertificates(certs);
//Getting the signed data
CMSSignedData sigData = gen.generate(msg, false);
return sigData.getEncoded();
}
任何想法?
提前致谢