mimemultipart java_Java MimeMultipart.writeTo方法代码示例

import javax.mail.internet.MimeMultipart; //导入方法依赖的package包/类

public void sign(X509Certificate cert, PrivateKey privateKey, String digestAlg) throws SFRMException {

try {

/* Create the SMIMESignedGenerator */

SMIMECapabilityVector capabilities = new SMIMECapabilityVector();

capabilities.addCapability(SMIMECapability.dES_EDE3_CBC);

capabilities.addCapability(SMIMECapability.rC2_CBC, 128);

capabilities.addCapability(SMIMECapability.dES_CBC);

SMIMESignedGenerator signer = new SMIMESignedGenerator();

signer.setContentTransferEncoding("binary");

String signerDigestAlg = "";

if (digestAlg.equalsIgnoreCase(ALG_SIGN_MD5))

signerDigestAlg = "MD5withRSA";

else if (digestAlg.equalsIgnoreCase(ALG_SIGN_SHA1))

signerDigestAlg = "SHA1withRSA";

else

throw new SFRMException("Encryption algorihtm error - " + digestAlg);

signer.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder()

.setProvider(SECURITY_PROVIDER)

.build(signerDigestAlg, privateKey, cert));

/* Add the list of certs to the generator */

ArrayList certList = new ArrayList();

certList.add(cert);

CertStore certs = CertStore.getInstance("Collection",

new CollectionCertStoreParameters(certList), "BC");

// signer.addCertificatesAndCRLs(certs);

signer.addCertificates(new JcaCertStore(certList));

/* Sign the body part */

MimeMultipart mm = signer.generate(bodyPart);

InternetHeaders headers = new InternetHeaders();

headers.setHeader("Content-Type", mm.getContentType());

ByteArrayOutputStream baos = new ByteArrayOutputStream();

mm.writeTo(baos);

this.bodyPart = new MimeBodyPart(headers, baos.toByteArray());

this.setIsSigned(true);

} catch (org.bouncycastle.mail.smime.SMIMEException ex) {

throw new SFRMException("Unable to sign body part", ex.getUnderlyingException());

} catch (Exception e) {

throw new SFRMException("Unable to sign body part", e);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值