Search in sources :

Example 1 with KeyTransRecipientInformation

use of org.bouncycastle.cms.KeyTransRecipientInformation in project tutorials by eugenp.

the class BouncyCastleCrypto method decryptData.

public static byte[] decryptData(final byte[] encryptedData, final PrivateKey decryptionKey) throws CMSException {
    byte[] decryptedData = null;
    if (null != encryptedData && null != decryptionKey) {
        CMSEnvelopedData envelopedData = new CMSEnvelopedData(encryptedData);
        Collection<RecipientInformation> recip = envelopedData.getRecipientInfos().getRecipients();
        KeyTransRecipientInformation recipientInfo = (KeyTransRecipientInformation) recip.iterator().next();
        JceKeyTransRecipient recipient = new JceKeyTransEnvelopedRecipient(decryptionKey);
        decryptedData = recipientInfo.getContent(recipient);
    }
    return decryptedData;
}
Also used : JceKeyTransRecipient(org.bouncycastle.cms.jcajce.JceKeyTransRecipient) CMSEnvelopedData(org.bouncycastle.cms.CMSEnvelopedData) KeyTransRecipientInformation(org.bouncycastle.cms.KeyTransRecipientInformation) RecipientInformation(org.bouncycastle.cms.RecipientInformation) KeyTransRecipientInformation(org.bouncycastle.cms.KeyTransRecipientInformation) JceKeyTransEnvelopedRecipient(org.bouncycastle.cms.jcajce.JceKeyTransEnvelopedRecipient)

Example 2 with KeyTransRecipientInformation

use of org.bouncycastle.cms.KeyTransRecipientInformation in project serverless by bluenimble.

the class DecryptDocument method main.

public static void main(String[] args) throws IOException, CertificateException, UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchProviderException, CertStoreException, CMSException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException {
    CMSEnvelopedData ced = new CMSEnvelopedData(new FileInputStream("ToBeDecrypted.pk7"));
    Collection<?> recip = ced.getRecipientInfos().getRecipients();
    KeyTransRecipientInformation rinfo = (KeyTransRecipientInformation) recip.iterator().next();
    // privatekey est la cl� priv�e permettant de d�chiffrer la cl� secr�te (sym�trique)
    // "2[$0wUOS";
    String password = "bspass";
    // "thawte freemail member's thawte consulting (pty) ltd. id";
    String alias = "bscert";
    KeyInformation keyInfo = ReadPKCS12.read(new FileInputStream("files/test.p12"), password, alias);
    byte[] contents = rinfo.getContent(new JceKeyTransEnvelopedRecipient(keyInfo.getPrivateKey()).setProvider("BC"));
    FileOutputStream envfos = new FileOutputStream("Decrypted.txt");
    envfos.write(contents);
    envfos.close();
}
Also used : CMSEnvelopedData(org.bouncycastle.cms.CMSEnvelopedData) FileOutputStream(java.io.FileOutputStream) KeyTransRecipientInformation(org.bouncycastle.cms.KeyTransRecipientInformation) JceKeyTransEnvelopedRecipient(org.bouncycastle.cms.jcajce.JceKeyTransEnvelopedRecipient) FileInputStream(java.io.FileInputStream) KeyInformation(com.bluenimble.platform.crypto.tests.KeyInformation)

Aggregations

CMSEnvelopedData (org.bouncycastle.cms.CMSEnvelopedData)2 KeyTransRecipientInformation (org.bouncycastle.cms.KeyTransRecipientInformation)2 JceKeyTransEnvelopedRecipient (org.bouncycastle.cms.jcajce.JceKeyTransEnvelopedRecipient)2 KeyInformation (com.bluenimble.platform.crypto.tests.KeyInformation)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 RecipientInformation (org.bouncycastle.cms.RecipientInformation)1 JceKeyTransRecipient (org.bouncycastle.cms.jcajce.JceKeyTransRecipient)1