FileDocCategorySizeDatePackage
CMSEnvelopedDataGenerator.javaAPI DocBouncy Castle Crypto API 1.41 (Java 1.5)7411Wed Oct 01 10:55:28 BST 2008org.bouncycastle.cms

CMSEnvelopedDataGenerator

public class CMSEnvelopedDataGenerator extends CMSEnvelopedGenerator
General class for generating a CMS enveloped-data message. A simple example of usage.
CMSEnvelopedDataGenerator fact = new CMSEnvelopedDataGenerator();

fact.addKeyTransRecipient(cert);

CMSEnvelopedData data = fact.generate(content, algorithm, "BC");

Fields Summary
Constructors Summary
public CMSEnvelopedDataGenerator()
base constructor

    
public CMSEnvelopedDataGenerator(SecureRandom rand)
constructor allowing specific source of randomness

param
rand instance of SecureRandom to use

        super(rand);
    
Methods Summary
private CMSEnvelopedDatagenerate(CMSProcessable content, java.lang.String encryptionOID, javax.crypto.KeyGenerator keyGen, java.security.Provider provider)
generate an enveloped object that contains an CMS Enveloped Data object using the given provider and the passed in key generator.

        Provider                encProvider = keyGen.getProvider();
        ASN1EncodableVector     recipientInfos = new ASN1EncodableVector();
        AlgorithmIdentifier     encAlgId;
        SecretKey               encKey;
        ASN1OctetString         encContent;

        try
        {
            Cipher cipher = CMSEnvelopedHelper.INSTANCE.getSymmetricCipher(encryptionOID, encProvider);

            AlgorithmParameters params;
            
            encKey = keyGen.generateKey();
            params = generateParameters(encryptionOID, encKey, encProvider);

            cipher.init(Cipher.ENCRYPT_MODE, encKey, params, rand);

            //
            // If params are null we try and second guess on them as some providers don't provide
            // algorithm parameter generation explicity but instead generate them under the hood.
            //
            if (params == null)
            {
                params = cipher.getParameters();
            }
            
            encAlgId = getAlgorithmIdentifier(encryptionOID, params);

            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
            CipherOutputStream      cOut = new CipherOutputStream(bOut, cipher);

            content.write(cOut);

            cOut.close();

            encContent = new BERConstructedOctetString(bOut.toByteArray());
        }
        catch (InvalidKeyException e)
        {
            throw new CMSException("key invalid in message.", e);
        }
        catch (NoSuchPaddingException e)
        {
            throw new CMSException("required padding not supported.", e);
        }
        catch (InvalidAlgorithmParameterException e)
        {
            throw new CMSException("algorithm parameters invalid.", e);
        }
        catch (IOException e)
        {
            throw new CMSException("exception decoding algorithm parameters.", e);
        }

        Iterator            it = recipientInfs.iterator();

        while (it.hasNext())
        {
            RecipientInf            recipient = (RecipientInf)it.next();

            try
            {
                recipientInfos.add(recipient.toRecipientInfo(encKey, rand, provider));
            }
            catch (IOException e)
            {
                throw new CMSException("encoding error.", e);
            }
            catch (InvalidKeyException e)
            {
                throw new CMSException("key inappropriate for algorithm.", e);
            }
            catch (GeneralSecurityException e)
            {
                throw new CMSException("error making encrypted content.", e);
            }
        }

        EncryptedContentInfo  eci = new EncryptedContentInfo(
                                 PKCSObjectIdentifiers.data,
                                 encAlgId, 
                                 encContent);

        ContentInfo contentInfo = new ContentInfo(
                PKCSObjectIdentifiers.envelopedData,
                new EnvelopedData(null, new DERSet(recipientInfos), eci, null));

        return new CMSEnvelopedData(contentInfo);
    
public CMSEnvelopedDatagenerate(CMSProcessable content, java.lang.String encryptionOID, java.lang.String provider)
generate an enveloped object that contains an CMS Enveloped Data object using the given provider.

        return generate(content, encryptionOID, CMSUtils.getProvider(provider));
    
public CMSEnvelopedDatagenerate(CMSProcessable content, java.lang.String encryptionOID, java.security.Provider provider)
generate an enveloped object that contains an CMS Enveloped Data object using the given provider.

        KeyGenerator keyGen = CMSEnvelopedHelper.INSTANCE.createSymmetricKeyGenerator(encryptionOID, provider);

        keyGen.init(rand);

        return generate(content, encryptionOID, keyGen, provider);
    
public CMSEnvelopedDatagenerate(CMSProcessable content, java.lang.String encryptionOID, int keySize, java.lang.String provider)
generate an enveloped object that contains an CMS Enveloped Data object using the given provider.

        return generate(content, encryptionOID, keySize, CMSUtils.getProvider(provider));
    
public CMSEnvelopedDatagenerate(CMSProcessable content, java.lang.String encryptionOID, int keySize, java.security.Provider provider)
generate an enveloped object that contains an CMS Enveloped Data object using the given provider.

        KeyGenerator keyGen = CMSEnvelopedHelper.INSTANCE.createSymmetricKeyGenerator(encryptionOID, provider);

        keyGen.init(keySize, rand);

        return generate(content, encryptionOID, keyGen, provider);