All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.its.ETSIEncryptedDataBuilder Maven / Gradle / Ivy

Go to download

The Bouncy Castle Java APIs for CMS, PKCS, EAC, TSP, CMP, CRMF, OCSP, and certificate generation. This jar contains APIs for JDK 1.5 to JDK 1.8. The APIs can be used in conjunction with a JCE/JCA provider such as the one provided with the Bouncy Castle Cryptography APIs.

There is a newer version: 1.79
Show newest version
package org.bouncycastle.its;

import java.util.ArrayList;
import java.util.List;

import org.bouncycastle.its.operator.ETSIDataEncryptor;
import org.bouncycastle.oer.its.ieee1609dot2.AesCcmCiphertext;
import org.bouncycastle.oer.its.ieee1609dot2.EncryptedData;
import org.bouncycastle.oer.its.ieee1609dot2.SequenceOfRecipientInfo;
import org.bouncycastle.oer.its.ieee1609dot2.SymmetricCiphertext;

public class ETSIEncryptedDataBuilder
{
    private final List recipientInfoBuilders = new ArrayList();

    public ETSIEncryptedDataBuilder()
    {
    }

    public void addRecipientInfoBuilder(ETSIRecipientInfoBuilder recipientInfoBuilder)
    {
        recipientInfoBuilders.add(recipientInfoBuilder);
    }

    public ETSIEncryptedData build(ETSIDataEncryptor encryptor, byte[] content)
    {
        byte[] opaque = encryptor.encrypt(content);
        byte[] key = encryptor.getKey();
        byte[] nonce = encryptor.getNonce();

        SequenceOfRecipientInfo.Builder builder = SequenceOfRecipientInfo.builder();
        for (ETSIRecipientInfoBuilder recipientInfoBuilder : recipientInfoBuilders)
        {
            builder.addRecipients(recipientInfoBuilder.build(key));
        }

        // Encryption goes here

        return new ETSIEncryptedData(EncryptedData.builder()
            .setRecipients(builder.createSequenceOfRecipientInfo())
            .setCiphertext(SymmetricCiphertext.aes128ccm(AesCcmCiphertext.builder()
                .setCcmCiphertext(opaque)
                .setNonce(nonce)
                .createAesCcmCiphertext())).createEncryptedData()
        );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy