/* Open the keystore */
KeyStore keystore = KeyStore.getInstance("PKCS12", "BC");
keystore.load(new FileInputStream(pkcs12Path), "".toCharArray());
Certificate[] chain = keystore.getCertificateChain(keyalias);
/* Get the private key to sign the message with */
PrivateKey privateKey = (PrivateKey) keystore.getKey(keyalias, keyPwd.toCharArray());
if (privateKey == null) {
throw new Exception("cannot find private key for alias: " + keyalias);
}
/* Create the message to sign and encrypt */
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, new MyAuthenticator(emailAddress, emailPwd));
MimeMessage body = new MimeMessage(session);
body.setFrom(new InternetAddress(emailAddress));
if (toAdd != null) {
body.addRecipients(Message.RecipientType.TO, parseEmailAdd(toAdd));
}
if (ccAdd != null) {
body.addRecipients(Message.RecipientType.CC, parseEmailAdd(ccAdd));
}
if (bccAdd != null) {
body.addRecipients(Message.RecipientType.BCC, parseEmailAdd(bccAdd));
}
body.setSubject(subject);
body.setContent(textContent, "text/plain");
body.saveChanges();
/* Create the SMIMESignedGenerator */
SMIMECapabilityVector capabilities = new SMIMECapabilityVector();
capabilities.addCapability(SMIMECapability.dES_EDE3_CBC);
capabilities.addCapability(SMIMECapability.rC2_CBC, 128);
capabilities.addCapability(SMIMECapability.dES_CBC);
/* Add the list of certs to the generator */
List certList = new ArrayList();
certList.add(chain[0]);
Store certs = new JcaCertStore(certList);
signer.addCertificates(certs);
/* Sign the message */
MimeMultipart mm = signer.generate(body);
MimeMessage signedMessage = new MimeMessage(session);
/* Set all original MIME headers in the signed message */
Enumeration headers = body.getAllHeaderLines();
while (headers.hasMoreElements()) {
signedMessage.addHeaderLine((String) headers.nextElement());
}
/* Set the content of the signed message */
signedMessage.setContent(mm);
signedMessage.saveChanges();
/* Create the encrypter */
SMIMEEnvelopedGenerator encrypter = new SMIMEEnvelopedGenerator();
addRecipientInfoGenerator(encrypter, toAdd);//
addRecipientInfoGenerator(encrypter, ccAdd);
addRecipientInfoGenerator(encrypter, bccAdd);
/* Encrypt the message */
MimeBodyPart encryptedPart = encrypter.generate(signedMessage, new JceCMSContentEncryptorBuilder(CMSAlgorithm.RC2_CBC).setProvider("BC").build());
/*
* Create a new MimeMessage that contains the encrypted and signed
* content
*/
ByteArrayOutputStream out = new ByteArrayOutputStream();
encryptedPart.writeTo(out);
MimeMessage encryptedMessage = new MimeMessage(session, new ByteArrayInputStream(out.toByteArray()));
/* Set all original MIME headers in the encrypted message */
headers = body.getAllHeaderLines();
while (headers.hasMoreElements()) {
String headerLine = (String) headers.nextElement();
/*
* Make sure not to override any content-* headers from the
* original message
*/
if (!Strings.toLowerCase(headerLine).startsWith("content-")) {
encryptedMessage.addHeaderLine(headerLine);
}
}
// Before .NET Framework 4.7.2
using (RSA rsa = RSA.Create())
{
rsa.ImportParameters(rsaParameters);
// Other code to execute using the RSA instance.
}
采用類似如下所示的代碼:
// Starting with .NET Framework 4.7.2
using (RSA rsa = RSA.Create(rsaParameters))
{
// Other code to execute using the rsa instance.
}