Example usage for org.bouncycastle.asn1.cms EncryptedData EncryptedData

List of usage examples for org.bouncycastle.asn1.cms EncryptedData EncryptedData

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.cms EncryptedData EncryptedData.

Prototype

public EncryptedData(EncryptedContentInfo encInfo, ASN1Set unprotectedAttrs) 

Source Link

Usage

From source file:es.gob.afirma.envelopers.cades.CAdESEncryptedData.java

License:Open Source License

/** Método principal que genera la firma de tipo EncryptedData.
 * @param data/*from ww  w .  j a v a  2  s .c  om*/
 *        Datos a cifrar.
 * @param digAlg
 *        ALgoritmo para realizar el Digest.
 * @param config
 *        Configuración del algoritmo para firmar.
 * @param pass
 *        Cadena que se usar paa cifrar los datos.
 * @param dataType
 *        Identifica el tipo del contenido a firmar.
 * @return la firma de tipo EncryptedData.
 * @throws java.security.NoSuchAlgorithmException
 *         Si no se soporta alguno de los algoritmos de firma o huella
 *         digital
 * @throws IOException */
static byte[] genEncryptedData(final byte[] data, final String digAlg, final AOCipherConfig config,
        final String pass, final String dataType) throws NoSuchAlgorithmException, AOException, IOException {

    // Asignamos la clave de cifrado
    final SecretKey cipherKey = CAdESUtils.assignKey(config, pass);

    // Datos previos útiles
    final String digestAlgorithm = AOSignConstants.getDigestAlgorithmName(digAlg);

    // generamos el contenedor de cifrado
    final EncryptedContentInfo encInfo;
    try {
        // 3. ENCRIPTEDCONTENTINFO
        encInfo = CAdESUtils.getEncryptedContentInfo(data, config, cipherKey);
    } catch (final Exception ex) {
        throw new AOException("Error durante el proceso de cifrado", ex); //$NON-NLS-1$
    }

    // 4. ATRIBUTOS
    // obtenemos la lista de certificados
    final ASN1Set unprotectedAttrs = SigUtils.getAttributeSet(
            new AttributeTable(CAdESUtils.initContexExpecific(digestAlgorithm, data, dataType, null)));

    // construimos el Enveloped Data y lo devolvemos
    return new ContentInfo(PKCSObjectIdentifiers.encryptedData, new EncryptedData(encInfo, unprotectedAttrs))
            .getEncoded(ASN1Encoding.DER);

}

From source file:es.gob.afirma.envelopers.cms.CMSEncryptedData.java

License:Open Source License

/** Método principal que genera la firma de tipo EncryptedData.
 * @param data//from www. ja  va 2s.  c o  m
 *        Datos que queremos envolver.
 * @param digAlg
 *        Algoritmo para realizar el Digest.
 * @param config
 *        Configuración del algoritmo para cifrar.
 * @param cipherKey
 *        Clave de cifrado.
 * @param dataType
 *        Identifica el tipo del contenido a firmar.
 * @param uatrib
 *        Conjunto de atributos no firmados.
 * @return la firma de tipo EncryptedData.
 * @throws java.security.NoSuchAlgorithmException
 *         Si no se soporta alguno de los algoritmos de firma o huella
 *         digital
 * @throws IOException
 *         Cuando se produce algun error al codificar los datos.
 */
static byte[] genEncryptedData(final byte[] data, final String digAlg, final AOCipherConfig config,
        final Key cipherKey, final String dataType, final Map<String, byte[]> uatrib)
        throws NoSuchAlgorithmException, IOException {

    // Datos previos &uacute;tiles
    final String digestAlgorithm = AOSignConstants.getDigestAlgorithmName(digAlg);

    // generamos el contenedor de cifrado
    EncryptedContentInfo encInfo = null;
    try {
        // 3. ENCRIPTEDCONTENTINFO
        encInfo = Utils.getEncryptedContentInfo(data, cipherKey, config);
    } catch (final Exception ex) {
        Logger.getLogger("es.gob.afirma").severe("Error durante el proceso cifrado: " + ex); //$NON-NLS-1$ //$NON-NLS-2$
    }

    // 4. ATRIBUTOS
    // obtenemos la lista de certificados
    ASN1Set unprotectedAttrs = null;
    unprotectedAttrs = Utils.generateSignerInfo(digestAlgorithm, data, dataType, uatrib);

    // construimos el Enveloped Data y lo devolvemos
    return new ContentInfo(PKCSObjectIdentifiers.encryptedData, new EncryptedData(encInfo, unprotectedAttrs))
            .getEncoded(ASN1Encoding.DER);
}