List of usage examples for org.bouncycastle.asn1.cms EncryptedContentInfo EncryptedContentInfo
public EncryptedContentInfo(ASN1ObjectIdentifier contentType, AlgorithmIdentifier contentEncryptionAlgorithm,
ASN1OctetString encryptedContent)
From source file:cljpdf.text.pdf.PdfPublicKeySecurityHandler.java
License:Mozilla Public License
private DERObject createDERForRecipient(byte[] in, X509Certificate cert) throws IOException, GeneralSecurityException { String s = "1.2.840.113549.3.2"; AlgorithmParameterGenerator algorithmparametergenerator = AlgorithmParameterGenerator.getInstance(s); AlgorithmParameters algorithmparameters = algorithmparametergenerator.generateParameters(); ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream( algorithmparameters.getEncoded("ASN.1")); ASN1InputStream asn1inputstream = new ASN1InputStream(bytearrayinputstream); DERObject derobject = asn1inputstream.readObject(); KeyGenerator keygenerator = KeyGenerator.getInstance(s); keygenerator.init(128);//from w ww. ja v a 2 s . c om SecretKey secretkey = keygenerator.generateKey(); Cipher cipher = Cipher.getInstance(s); cipher.init(1, secretkey, algorithmparameters); byte[] abyte1 = cipher.doFinal(in); DEROctetString deroctetstring = new DEROctetString(abyte1); KeyTransRecipientInfo keytransrecipientinfo = computeRecipientInfo(cert, secretkey.getEncoded()); DERSet derset = new DERSet(new RecipientInfo(keytransrecipientinfo)); AlgorithmIdentifier algorithmidentifier = new AlgorithmIdentifier(new DERObjectIdentifier(s), derobject); EncryptedContentInfo encryptedcontentinfo = new EncryptedContentInfo(PKCSObjectIdentifiers.data, algorithmidentifier, deroctetstring); EnvelopedData env = new EnvelopedData(null, derset, encryptedcontentinfo, null); ContentInfo contentinfo = new ContentInfo(PKCSObjectIdentifiers.envelopedData, env); return contentinfo.getDERObject(); }
From source file:com.itextpdf.kernel.crypto.securityhandler.PubKeySecurityHandler.java
License:Open Source License
private ASN1Primitive createDERForRecipient(byte[] in, X509Certificate cert) throws IOException, GeneralSecurityException { EncryptionUtils.DERForRecipientParams parameters = EncryptionUtils.calculateDERForRecipientParams(in); KeyTransRecipientInfo keytransrecipientinfo = computeRecipientInfo(cert, parameters.abyte0); DEROctetString deroctetstring = new DEROctetString(parameters.abyte1); DERSet derset = new DERSet(new RecipientInfo(keytransrecipientinfo)); EncryptedContentInfo encryptedcontentinfo = new EncryptedContentInfo(PKCSObjectIdentifiers.data, parameters.algorithmIdentifier, deroctetstring); EnvelopedData env = new EnvelopedData(null, derset, encryptedcontentinfo, (ASN1Set) null); ContentInfo contentinfo = new ContentInfo(PKCSObjectIdentifiers.envelopedData, env); return contentinfo.toASN1Primitive(); }
From source file:com.itextpdf.text.pdf.PdfPublicKeySecurityHandler.java
License:Open Source License
private ASN1Primitive createDERForRecipient(byte[] in, X509Certificate cert) throws IOException, GeneralSecurityException { String s = "1.2.840.113549.3.2"; AlgorithmParameterGenerator algorithmparametergenerator = AlgorithmParameterGenerator.getInstance(s); AlgorithmParameters algorithmparameters = algorithmparametergenerator.generateParameters(); ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream( algorithmparameters.getEncoded("ASN.1")); ASN1InputStream asn1inputstream = new ASN1InputStream(bytearrayinputstream); ASN1Primitive derobject = asn1inputstream.readObject(); KeyGenerator keygenerator = KeyGenerator.getInstance(s); keygenerator.init(128);//from ww w . j a v a 2s . c o m SecretKey secretkey = keygenerator.generateKey(); Cipher cipher = Cipher.getInstance(s); cipher.init(1, secretkey, algorithmparameters); byte[] abyte1 = cipher.doFinal(in); DEROctetString deroctetstring = new DEROctetString(abyte1); KeyTransRecipientInfo keytransrecipientinfo = computeRecipientInfo(cert, secretkey.getEncoded()); DERSet derset = new DERSet(new RecipientInfo(keytransrecipientinfo)); AlgorithmIdentifier algorithmidentifier = new AlgorithmIdentifier(new ASN1ObjectIdentifier(s), derobject); EncryptedContentInfo encryptedcontentinfo = new EncryptedContentInfo(PKCSObjectIdentifiers.data, algorithmidentifier, deroctetstring); ASN1Set set = null; EnvelopedData env = new EnvelopedData(null, derset, encryptedcontentinfo, set); ContentInfo contentinfo = new ContentInfo(PKCSObjectIdentifiers.envelopedData, env); return contentinfo.toASN1Primitive(); }
From source file:es.gob.afirma.envelopers.cades.CAdESUtils.java
License:Open Source License
/** Obtiene el contenido de un archivo encriptado * @param file Archivo con los datos/* w w w. j av a2 s . c om*/ * @param config Configuracion de cifrado * @param params Parametros * @param cipher Encriptador */ private static EncryptedContentInfo getEncryptedContentInfo(final byte[] file, final AOCipherConfig config, final AlgorithmParameterSpec params, final Cipher cipher) throws IOException { final byte[] ciphered; try { ciphered = cipher.doFinal(file); } catch (final Exception e) { LOGGER.severe("No se ha podido completar el cifrado, se devolvera null: " + e); //$NON-NLS-1$ return null; } ASN1Encodable asn1Params; if (params != null) { final ASN1InputStream aIn = new ASN1InputStream(cipher.getParameters().getEncoded("ASN.1")); //$NON-NLS-1$ asn1Params = aIn.readObject(); aIn.close(); } else { asn1Params = new DERNull(); } // obtenemos el OID del algoritmo de cifrado final AlgorithmIdentifier encAlgId = new AlgorithmIdentifier( new ASN1ObjectIdentifier(config.getAlgorithm().getOid()), asn1Params); // Obtenemos el identificador final ASN1ObjectIdentifier contentType = PKCSObjectIdentifiers.encryptedData; return new EncryptedContentInfo(contentType, encAlgId, new DEROctetString(ciphered)); }
From source file:es.gob.afirma.envelopers.cms.Utils.java
License:Open Source License
/** Obtiene el contenido de un archivo encriptado. * @param file Archivo con los datos/*from w ww . ja v a2 s . c o m*/ * @param config Configuracion de cifrado * @param params Parametros * @param cipher Encriptador * @return Contenido de un archivo encriptado. * @throws BadPaddingException Cuando hay problemas con un relleno de datos. * @throws IOException Cuando hay problemas con el tratamiento de datos. * @throws IllegalBlockSizeException Cuando hay problemas internos con los tamaños de bloque de cifrado. */ private static EncryptedContentInfo getEncryptedContentInfo(final byte[] file, final AOCipherConfig config, final AlgorithmParameterSpec params, final Cipher cipher) throws IOException, IllegalBlockSizeException, BadPaddingException { ASN1Encodable asn1Params; if (params != null) { final ASN1InputStream aIn = new ASN1InputStream(cipher.getParameters().getEncoded("ASN.1")); //$NON-NLS-1$ asn1Params = aIn.readObject(); aIn.close(); } else { asn1Params = DERNull.INSTANCE; } // obtenemos el OID del algoritmo de cifrado final AlgorithmIdentifier encAlgId = new AlgorithmIdentifier( new ASN1ObjectIdentifier(config.getAlgorithm().getOid()), asn1Params); // Obtenemos el identificador final ASN1ObjectIdentifier contentType = PKCSObjectIdentifiers.encryptedData; return new EncryptedContentInfo(contentType, encAlgId, new DEROctetString(cipher.doFinal(file))); }