List of usage examples for org.bouncycastle.asn1.cms EncryptedContentInfo getContentEncryptionAlgorithm
public AlgorithmIdentifier getContentEncryptionAlgorithm()
From source file:es.gob.afirma.applet.CMSInformation.java
License:Open Source License
/** * Obtiene los datos de cifrado usados.// w ww.ja v a 2 s. c om * * @param datos informacion de los datos cifrados sin formatear. * @return informacion de los datos cifrados. */ private static String getEncryptedContentInfo(final EncryptedContentInfo datos) { String info = ""; //$NON-NLS-1$ //especificamos el tipo de contenido if (datos.getContentType().equals(PKCSObjectIdentifiers.encryptedData)) { info = info + TB + AppletMessages.getString("CMSInformation.0") + SP + ENCRYPTED_DATA + CR; //$NON-NLS-1$ } else { info = info + TB + AppletMessages.getString("CMSInformation.0") + SP + datos.getContentType() + CR; //$NON-NLS-1$ } // el algoritmo de cifrado de los datos final AlgorithmIdentifier ai = datos.getContentEncryptionAlgorithm(); AOCipherAlgorithm algorithm = null; final AOCipherAlgorithm[] algos = AOCipherAlgorithm.values(); // obtenemos el algoritmo usado para cifrar la pass for (final AOCipherAlgorithm algo : algos) { if (algo.getOid().equals(ai.getAlgorithm().toString())) { algorithm = algo; } } if (algorithm != null) { info = info + TB + AppletMessages.getString("CMSInformation.17") + SP + algorithm.getName() + CR; //$NON-NLS-1$ } else { info = info + TB + AppletMessages.getString("CMSInformation.18") + SP + ai.getAlgorithm().toString() //$NON-NLS-1$ + CR; } return info; }
From source file:es.gob.afirma.envelopers.cms.CMSDecipherAuthenticatedEnvelopedData.java
License:Open Source License
/** Éste método descifra el contenido de un CMS * AuthenticatedEnvelopedData./* w w w . j a v a2s . c o m*/ * @param cmsData * Datos del tipo AuthenticatedEnvelopedData para obtener los * datos cifrados. * @param keyEntry * Clave privada del certificado usado para descifrar el * contenido. * @return El contenido descifrado del EnvelopedData. * @throws java.io.IOException * Si ocurre algún problema leyendo o escribiendo los * datos * @throws java.security.cert.CertificateEncodingException * Si se produce alguna excepción con los certificados de * firma. * @throws AOException * Cuando ocurre un error durante el proceso de descifrado * (formato o clave incorrecto,...) * @throws AOInvalidRecipientException Cuando se indica un certificado que no está entre los * destinatarios del sobre. * @throws InvalidKeyException Cuando la clave almacenada en el sobre no es válida. * @throws NoSuchPaddingException Cuando no se soporta un tipo de relleno necesario. * @throws NoSuchAlgorithmException Si el JRE no soporta algún algoritmo necesario * @throws BadPaddingException Cuando hay problemas con un relleno de datos. * @throws IllegalBlockSizeException Cuando hay problemas internos con los tamaños de bloque de cifrado. * @throws InvalidAlgorithmParameterException Si no se soporta un parámetro necesario para un algoritmo. */ public static byte[] dechiperAuthenticatedEnvelopedData(final byte[] cmsData, final PrivateKeyEntry keyEntry) throws IOException, CertificateEncodingException, AOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { // Contendra el contenido a tratar. final AuthEnvelopedData authEnvelopedData; final Enumeration<?> elementRecipient; try { final ASN1Sequence contentAuthEnvelopedData = Utils.fetchWrappedData(cmsData); authEnvelopedData = AuthEnvelopedData.getInstance(contentAuthEnvelopedData); elementRecipient = authEnvelopedData.getRecipientInfos().getObjects(); } catch (final Exception ex) { throw new AOException("El fichero no contiene un tipo AuthenticatedEnvelopedData", ex); //$NON-NLS-1$ } final EncryptedKeyDatas encryptedKeyDatas = Utils .fetchEncryptedKeyDatas((X509Certificate) keyEntry.getCertificate(), elementRecipient); // Obtenemos el contenido cifrado final EncryptedContentInfo contenidoCifrado = authEnvelopedData.getAuthEncryptedContentInfo(); // Obtenemos el algoritmo usado para cifrar la clave generada. final AlgorithmIdentifier algClave = contenidoCifrado.getContentEncryptionAlgorithm(); // Asignamos la clave de descifrado del contenido. final KeyAsigned keyAsigned = Utils.assignKey(encryptedKeyDatas.getEncryptedKey(), keyEntry, algClave); // Desciframos el contenido. return Utils.deCipherContent(contenidoCifrado.getEncryptedContent().getOctets(), keyAsigned.getConfig(), keyAsigned.getCipherKey()); }
From source file:es.gob.afirma.envelopers.cms.CMSDecipherEncryptedData.java
License:Open Source License
/** Método principal que descifra datos del tipo de EncryptedData. * @param encryptedData/*from w w w . j a va2s . co m*/ * Datos del tipo CMS EncryptedData. * @param pass * Contraseña o clave que se uso para cifrar los datos. * @return Datos sin encriptar. * @throws AOException * Cuando ocurre un error durante el proceso de descifrado * (formato o clave incorrecto,...) * @throws InvalidKeyException * Cuando se proporciona una clave incorrecta para el * descifrado. * @throws BadPaddingException Cuando hay problemas con un relleno de datos. * @throws IllegalBlockSizeException Cuando hay problemas internos con los tamaños de bloque de cifrado. * @throws InvalidAlgorithmParameterException Si no se soporta un parámetro necesario para un algoritmo. * @throws NoSuchPaddingException Cuando no se soporta un tipo de relleno necesario. * @throws NoSuchAlgorithmException Si el JRE no soporta algún algoritmo necesario * @throws IOException En caso de error en la lectura o tratamiento de datos * @throws InvalidKeySpecException Cuando ocurren problemas relacionados con la estructura interna de las claves */ @SuppressWarnings("unused") byte[] dechiperEncryptedData(final byte[] encryptedData, final String pass) throws AOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, IOException { AlgorithmIdentifier alg = null; EncryptedContentInfo eci = null; // donde se guardara el resultad. final byte[] deciphered; try { final ASN1Sequence contentEncryptedData = Utils.fetchWrappedData(encryptedData); // Obtenemos los datos del encryptedData. final Enumeration<?> e2 = contentEncryptedData.getObjects(); // version e2.nextElement(); // EncryptedContentInfo. donde esta lo que necesitamos. eci = EncryptedContentInfo.getInstance(e2.nextElement()); // Obtenemos el agoritmo de cifrado alg = eci.getContentEncryptionAlgorithm(); // Se intenta obtener el encrypted data. // Si no puede convertirse, dara error. // "EncryptedData EncryptedData" no se usara. solo es para verificar // que es de este tipo. new EncryptedData(eci); } catch (final Exception ex) { throw new AOException("El fichero no contiene un tipo EncryptedData", ex); //$NON-NLS-1$ } // asignamos la clave de descifrado a partir del algoritmo. assignKey(alg, pass); // Obtenemos el contenido cifrado. final byte[] contCifrado = eci.getEncryptedContent().getOctets(); // Desciframos. return Utils.deCipherContent(contCifrado, this.config, this.cipherKey); }
From source file:es.gob.afirma.envelopers.cms.CMSDecipherEnvelopData.java
License:Open Source License
/** Éste método descifra el contenido de un CMS EnvelopedData. * @param cmsData/* ww w .j a v a 2 s .com*/ * Flujo de lectura de los datos que contienen el tipo * EnvelopedData para obtener los datos cifrados. * @param keyEntry * Clave privada del certificado usado para descifrar el * contenido. * @return El contenido descifrado del EnvelopedData. * @throws java.io.IOException * Si ocurre algún problema leyendo o escribiendo los * datos * @throws java.security.cert.CertificateEncodingException * Si se produce alguna excepción con los certificados de * firma. * @throws AOException * Cuando ocurre un error durante el proceso de descifrado * (formato o clave incorrecto,...) * @throws AOInvalidRecipientException * Cuando se indica un certificado que no está entre los * destinatarios del sobre. * @throws InvalidKeyException * Cuando la clave almacenada en el sobre no es válida. * @throws NoSuchPaddingException Cuando no se soporta un tipo de relleno necesario. * @throws NoSuchAlgorithmException Si el JRE no soporta algún algoritmo necesario * @throws BadPaddingException Cuando hay problemas con un relleno de datos. * @throws IllegalBlockSizeException Cuando hay problemas internos con los tamaños de bloque de cifrado. * @throws InvalidAlgorithmParameterException Si no se soporta un parámetro necesario para un algoritmo. */ public static byte[] dechiperEnvelopData(final byte[] cmsData, final PrivateKeyEntry keyEntry) throws IOException, CertificateEncodingException, AOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { // Contendra el contenido a tratar. EnvelopedData enveloped = null; Enumeration<?> elementRecipient; try { // Contenido de EnvelopedData final ASN1Sequence contentEnvelopedData = Utils.fetchWrappedData(cmsData); enveloped = EnvelopedData.getInstance(contentEnvelopedData); elementRecipient = enveloped.getRecipientInfos().getObjects(); } catch (final Exception ex) { throw new AOException("El fichero no contiene un tipo EnvelopedData", ex); //$NON-NLS-1$ } final EncryptedKeyDatas encryptedKeyDatas = Utils .fetchEncryptedKeyDatas((X509Certificate) keyEntry.getCertificate(), elementRecipient); // Obtenemos el contenido cifrado final EncryptedContentInfo contenidoCifrado = enveloped.getEncryptedContentInfo(); // Obtenemos el algoritmo usado para cifrar la clave generada. final AlgorithmIdentifier algClave = contenidoCifrado.getContentEncryptionAlgorithm(); // Asignamos la clave de descifrado del contenido. final KeyAsigned keyAsigned = Utils.assignKey(encryptedKeyDatas.getEncryptedKey(), keyEntry, algClave); // Desciframos el contenido. return Utils.deCipherContent(contenidoCifrado.getEncryptedContent().getOctets(), keyAsigned.getConfig(), keyAsigned.getCipherKey()); }
From source file:es.gob.afirma.envelopers.cms.CMSDecipherSignedAndEnvelopedData.java
License:Open Source License
/** Éste método descifra el contenido de un CMS * SignedAndEnvelopData./*from w w w .j ava 2 s .c o m*/ * @param cmsData * Datos del tipo SignedAndEnvelopData para obtener los datos * cifrados. * @param keyEntry * Clave privada del certificado usado para descifrar el * contenido. * @return El contenido descifrado del SignedAndEnvelopData. * @throws java.io.IOException * Si ocurre algún problema leyendo o escribiendo los * datos * @throws java.security.cert.CertificateEncodingException * Si se produce alguna excepción con los certificados de * firma. * @throws AOException * Cuando ocurre un error durante el proceso de descifrado * (formato o clave incorrecto,...) * @throws AOInvalidRecipientException * Cuando se indica un certificado que no está entre los * destinatarios del sobre. * @throws InvalidKeyException * Cuando la clave almacenada en el sobre no es válida. * @throws NoSuchPaddingException Cuando no se soporta un tipo de relleno necesario. * @throws NoSuchAlgorithmException Si el JRE no soporta algún algoritmo necesario * @throws BadPaddingException Cuando hay problemas con un relleno de datos. * @throws IllegalBlockSizeException Cuando hay problemas internos con los tamaños de bloque de cifrado. * @throws InvalidAlgorithmParameterException Si no se soporta un parámetro necesario para un algoritmo. */ public static byte[] dechiperSignedAndEnvelopData(final byte[] cmsData, final PrivateKeyEntry keyEntry) throws IOException, CertificateEncodingException, AOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { // Contendra el contenido a tratar. SignedAndEnvelopedData sigAndEnveloped = null; Enumeration<?> elementRecipient; try { final ASN1Sequence contentSignedAndEnvelopedData = Utils.fetchWrappedData(cmsData); sigAndEnveloped = SignedAndEnvelopedData.getInstance(contentSignedAndEnvelopedData); elementRecipient = sigAndEnveloped.getRecipientInfos().getObjects(); } catch (final Exception ex) { throw new AOException("El fichero no contiene un tipo SignedAndEnvelopedData", ex); //$NON-NLS-1$ } final EncryptedKeyDatas encryptedKeyDatas = Utils .fetchEncryptedKeyDatas((X509Certificate) keyEntry.getCertificate(), elementRecipient); // Obtenemos el contenido cifrado final EncryptedContentInfo contenidoCifrado = sigAndEnveloped.getEncryptedContentInfo(); // Obtenemos el algoritmo usado para cifrar la clave generada. final AlgorithmIdentifier algClave = contenidoCifrado.getContentEncryptionAlgorithm(); // Asignamos la clave de descifrado del contenido. final KeyAsigned keyAsigned = Utils.assignKey(encryptedKeyDatas.getEncryptedKey(), keyEntry, algClave); // Desciframos el contenido. return Utils.deCipherContent(contenidoCifrado.getEncryptedContent().getOctets(), keyAsigned.getConfig(), keyAsigned.getCipherKey()); }