Question

I am using bouncy-castle for achieving SMIME (Public key encryption). To send an encrypted email to multiple recipients I chose this method

  1. Encrypt the email with the random key

  2. Encrypt the random key with each recipients public key and set it to the MIME message. If there are three recipients in the email , the object will have three encrypted random key.

  3. Recipients will identify the suitable encrypted random key, decrypt it using their private key and decrypt the content with the help of the random key.

What bouncy-castle class should I use to encrypt a message with random string. I know below code can be used to encrypt a msg using recipient's certificate.

/* Create the encrypter */
SMIMEEnvelopedGenerator encrypter = new SMIMEEnvelopedGenerator();
encrypter.addKeyTransRecipient((X509Certificate) certChain[0]);

/* Encrypt the MimeMessage*/
MimeBodyPart encryptedPart = encrypter.generate(signedMessage,
SMIMEEnvelopedGenerator.RC2_CBC, "BC");

Can some one post me the code to encrypt a MimeMessage with random key in bouncy-castle?

Was it helpful?

Solution

Got it done. The solution is pretty straight forward.Just add all recipients certificates to the bouncy castle EnvelopedGenerator and encrypt.

/* Create the encrypter */
SMIMEEnvelopedGenerator encrypter = new SMIMEEnvelopedGenerator();
encrypter.addKeyTransRecipient((X509Certificate) recipient1Cert);
encrypter.addKeyTransRecipient((X509Certificate) recipient2Cert);

/* Encrypt the MimeMessage*/
MimeBodyPart encryptedPart = encrypter.generate(signedMessage,
SMIMEEnvelopedGenerator.RC2_CBC, "BC");

while decrypting just provide the recipient cert. If it is one of the encrypting certs, your message will be decrypted successfully.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top