【Java密码学】使用BouncyCastle生成数字签名、数字信封

【Java密码学】使用BouncyCastle生成数字签名、数字信封


2024年3月11日发(作者:)

X509Certificate cerx509 = (X509Certificate) cert;

List certList = new ArrayList();

(cerx509);

CMSTypedData msg = (CMSTypedData) new CMSProcessableByteArray(

es(charSet));

Store certs = new JcaCertStore(certList);

CMSSignedDataGenerator gen = new CMSSignedDataGenerator();

ContentSigner sha1Signer = new JcaContentSignerBuilder(

"SHA1withRSA").setProvider("BC").build(prikey);

nerInfoGenerator(new JcaSignerInfoGeneratorBuilder(

new JcaDigestCalculatorProviderBuilder().setProvider("BC")

.build()).build(sha1Signer, cerx509));

tificates(certs);

CMSSignedData sigData = te(msg, true);

return (oded());

} catch (Exception e) {

tackTrace();

return null;

}

}

/**

* 验证数字签名

* @param signedData

* @return

*/

public boolean signedDataVerify(byte[] signedData) {

boolean verifyRet = true;

try {

// 新建PKCS#7签名数据处理对象

CMSSignedData sign = new CMSSignedData(signedData);

// 添加BouncyCastle作为安全提供

vider(new CastleProvider());

// 获得证书信息

Store certs = tificates();

// 获得签名者信息

SignerInformationStore signers = nerInfos();

Collection c = ners();

Iterator it = or();

// 当有多个签名者信息时需要全部验证

while (t()) {

SignerInformation signer = (SignerInformation) ();

// 证书链

Collection certCollection = ches(());

Iterator certIt = or();

X509CertificateHolder cert = (X509CertificateHolder) certIt

.next();

// 验证数字签名

if ((new JcaSimpleSignerInfoVerifierBuilder()

.setProvider("BC").build(cert))) {

verifyRet = true;

} else {

verifyRet = false;

}

}

} catch (Exception e) {

verifyRet = false;

tackTrace();

n("验证数字签名失败");

}

return verifyRet;

}

/**

* 加密数据

* @param srcMsg 源信息

* @param certPath 证书路径


发布者:admin,转转请注明出处:http://www.yc00.com/web/1710123204a1704440.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信