Search in sources :

Example 6 with MessageEncodingException

use of org.xipki.scep.exception.MessageEncodingException in project xipki by xipki.

the class PkiMessage method encrypt.

// method encode
private CMSEnvelopedData encrypt(X509Certificate recipient, ASN1ObjectIdentifier encAlgId) throws MessageEncodingException {
    ScepUtil.requireNonNull("recipient", recipient);
    ScepUtil.requireNonNull("encAlgId", encAlgId);
    byte[] messageDataBytes;
    try {
        messageDataBytes = messageData.toASN1Primitive().getEncoded();
    } catch (IOException ex) {
        throw new MessageEncodingException(ex);
    }
    CMSEnvelopedDataGenerator edGenerator = new CMSEnvelopedDataGenerator();
    CMSTypedData envelopable = new CMSProcessableByteArray(messageDataBytes);
    RecipientInfoGenerator recipientGenerator;
    try {
        recipientGenerator = new JceKeyTransRecipientInfoGenerator(recipient);
    } catch (CertificateEncodingException ex) {
        throw new MessageEncodingException(ex);
    }
    edGenerator.addRecipientInfoGenerator(recipientGenerator);
    try {
        OutputEncryptor encryptor = new JceCMSContentEncryptorBuilder(encAlgId).build();
        CMSEnvelopedData pkcsPkiEnvelope = edGenerator.generate(envelopable, encryptor);
        return pkcsPkiEnvelope;
    } catch (CMSException ex) {
        throw new MessageEncodingException(ex);
    }
}
Also used : CMSEnvelopedData(org.bouncycastle.cms.CMSEnvelopedData) CMSProcessableByteArray(org.bouncycastle.cms.CMSProcessableByteArray) CMSEnvelopedDataGenerator(org.bouncycastle.cms.CMSEnvelopedDataGenerator) JceKeyTransRecipientInfoGenerator(org.bouncycastle.cms.jcajce.JceKeyTransRecipientInfoGenerator) RecipientInfoGenerator(org.bouncycastle.cms.RecipientInfoGenerator) CMSTypedData(org.bouncycastle.cms.CMSTypedData) JceCMSContentEncryptorBuilder(org.bouncycastle.cms.jcajce.JceCMSContentEncryptorBuilder) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) MessageEncodingException(org.xipki.scep.exception.MessageEncodingException) JceKeyTransRecipientInfoGenerator(org.bouncycastle.cms.jcajce.JceKeyTransRecipientInfoGenerator) OutputEncryptor(org.bouncycastle.operator.OutputEncryptor) CMSException(org.bouncycastle.cms.CMSException)

Aggregations

MessageEncodingException (org.xipki.scep.exception.MessageEncodingException)6 IOException (java.io.IOException)3 CertificateEncodingException (java.security.cert.CertificateEncodingException)3 CMSException (org.bouncycastle.cms.CMSException)3 CMSProcessableByteArray (org.bouncycastle.cms.CMSProcessableByteArray)3 CMSTypedData (org.bouncycastle.cms.CMSTypedData)3 OperatorCreationException (org.bouncycastle.operator.OperatorCreationException)3 X509Certificate (java.security.cert.X509Certificate)2 CMSAbsentContent (org.bouncycastle.cms.CMSAbsentContent)2 CMSEnvelopedData (org.bouncycastle.cms.CMSEnvelopedData)2 CMSSignedDataGenerator (org.bouncycastle.cms.CMSSignedDataGenerator)2 DefaultSignedAttributeTableGenerator (org.bouncycastle.cms.DefaultSignedAttributeTableGenerator)2 SignerInfoGenerator (org.bouncycastle.cms.SignerInfoGenerator)2 JcaSignerInfoGeneratorBuilder (org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder)2 ContentSigner (org.bouncycastle.operator.ContentSigner)2 BcDigestCalculatorProvider (org.bouncycastle.operator.bc.BcDigestCalculatorProvider)2 JcaContentSignerBuilder (org.bouncycastle.operator.jcajce.JcaContentSignerBuilder)2 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)1 AttributeTable (org.bouncycastle.asn1.cms.AttributeTable)1 ContentInfo (org.bouncycastle.asn1.cms.ContentInfo)1