org.bouncycastle.cms.CMSEnvelopedData Java Examples

The following examples show how to use org.bouncycastle.cms.CMSEnvelopedData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BouncyCastleCrypto.java    From tutorials with MIT License 6 votes vote down vote up
public static byte[] encryptData(final byte[] data, X509Certificate encryptionCertificate) throws CertificateEncodingException, CMSException, IOException {
    byte[] encryptedData = null;
    if (null != data && null != encryptionCertificate) {
        CMSEnvelopedDataGenerator cmsEnvelopedDataGenerator = new CMSEnvelopedDataGenerator();
        JceKeyTransRecipientInfoGenerator jceKey = new JceKeyTransRecipientInfoGenerator(encryptionCertificate);
        cmsEnvelopedDataGenerator.addRecipientInfoGenerator(jceKey);
        CMSTypedData msg = new CMSProcessableByteArray(data);
        OutputEncryptor encryptor = new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES128_CBC).setProvider("BC").build();
        CMSEnvelopedData cmsEnvelopedData = cmsEnvelopedDataGenerator.generate(msg, encryptor);
        encryptedData = cmsEnvelopedData.getEncoded();
    }
    return encryptedData;
}
 
Example #2
Source File: EnvelopedDataDecryptor.java    From xipki with Apache License 2.0 5 votes vote down vote up
public byte[] decrypt(CMSEnvelopedData envData) throws MessageDecodingException {
  Args.notNull(envData, "envData");
  final RecipientInformationStore recipientInfos = envData.getRecipientInfos();
  RecipientInformation recipientInfo = null;
  EnvelopedDataDecryptorInstance decryptor = null;
  for (EnvelopedDataDecryptorInstance m : decryptors) {
    recipientInfo = recipientInfos.get(m.getRecipientId());
    if (recipientInfo != null) {
      decryptor = m;
      break;
    }
  }

  if (recipientInfo == null || decryptor == null) {
    throw new MessageDecodingException("missing expected key transfer recipient");
  }

  try {
    return recipientInfo.getContent(decryptor.getRecipient());
  } catch (CMSException ex) {
    throw new MessageDecodingException("could not decrypt the envelopedData");
  }
}
 
Example #3
Source File: BouncyCastleCrypto.java    From tutorials with MIT License 5 votes vote down vote up
public static byte[] decryptData(final byte[] encryptedData, final PrivateKey decryptionKey) throws CMSException {
    byte[] decryptedData = null;
    if (null != encryptedData && null != decryptionKey) {
        CMSEnvelopedData envelopedData = new CMSEnvelopedData(encryptedData);
        Collection<RecipientInformation> recip = envelopedData.getRecipientInfos().getRecipients();
        KeyTransRecipientInformation recipientInfo = (KeyTransRecipientInformation) recip.iterator().next();
        JceKeyTransRecipient recipient = new JceKeyTransEnvelopedRecipient(decryptionKey);
        decryptedData = recipientInfo.getContent(recipient);
    }
    return decryptedData;
}