java - 编译证书验证程序时出错

标签 java digital-signature pkcs#7

我要编译this IntelliJ IDE 中的程序。但它返回以下三个错误:

1.对于这一行:

CertStore certs = s.getCertificatesAndCRLs("Collection", "BC");

这个错误:

error: cannot find symbol method getCertificatesAndCRLs(String,String)

2.对于这一行:

Collection certCollection = certs.getCertificates(signer.getSID());

这个错误:

error: method getCertificates in class CertStore cannot be applied to given types;
required: CertSelector
found: SignerId
reason: actual argument SignerId cannot be converted to CertSelector by method invocation conversion

3.对于这一行:

if (signer.verify(cert.getPublicKey(), "BC")) verified++;

这个错误:

error: method verify in class SignerInformation cannot be applied to given types;
required: SignerInformationVerifier
found: PublicKey,String
reason: actual and formal argument lists differ in length
<小时/>

这些是我在程序顶部导入的内容:

import java.io.*;
import java.util.*;
import java.security.*;
import java.security.Security;
import java.security.cert.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.cms.*;

我已将 jdk1.7.0_15 库 + bcpkix-jdk15on-151.jarbcprov-jdk16-1.45.jar 添加到我的项目中。

有人可以帮我解决这个问题吗?

最佳答案

请按照以下步骤操作。希望它会对您有所帮助,因为它已经过测试。

步骤01:
从文件读取字节数组或将数据对象转换为字节数组。
示例代码:

FileInputStream fis = new FileInputStream(file);
byte fileContent[] = new byte[(int)file.length()];
fis.read(fileContent);

步骤 02:
从字节数组创建 CMSSignedData 对象。
示例代码:

CMSSignedData data = new CMSSignedData(fileContent);

步骤 03:
从 CMSSignedData 获取 CertStore。
示例代码:

CertStore certs = data.getCertificatesAndCRLs("Collection", "BC");

请务必首先在安全性中添加 BouncyCaSTLeProvider:

Security.addProvider(new BouncyCastleProvider());'

步骤 04:
使用签名者信息获取证书集合。
示例代码:

SignerInformation signer = (SignerInformation) i.next();
Collection<? extends Certificate> certCollection = certs.getCertificates(signer.getSID());

步骤 05:
验证每个证书的签名者的签名。
示例代码:

if (signer.verify(new JcaSimpleSignerInfoVerifierBuilder().setProvider("BC").build(cert)) ) 
{   
     }



为了实现您的目标,请检查您是否已完成上述步骤。

我猜测,要么您从错误的数据输入中生成 CMSSignedData,要么依赖库相互冲突(最可能的原因)。我使用了 bcprov-jdk16-1.46.jarbcmail-jdk16-1.46.jar 来完成所有任务。

关于java - 编译证书验证程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31266416/

相关文章:

Java Apache POI : setting default column style doesnt work for existing values

java - 将实例变量设为私有(private)只会对子类有利吗?

java.util.concurrent.ScheduledExecutorService 运行频率很低

c# - 如何检查智能卡读卡器中的匹配证书?

java - PKCS#7 登录并验证签名

java - 创建 PKCS#7 分离签名

java - 以编程方式验证 HTML 代码

c# - CertEnroll::CX509Enrollment::InstallResponse:找不到对象或属性。 0x80092004 (-2146885628)

java - 用JAVA将签名与原始文件连接起来

ios - 在 iOS 上解密 p7s 文件