public Object getEntity(Class t, Type gt, Annotation[] ann, PrivateKey pKey, X509Certificate cert) { MimeBodyPart decrypted = null; try { MimeBodyPart encryptedBodyPart = body; SMIMEEnveloped m = new SMIMEEnveloped(encryptedBodyPart); JceKeyTransRecipientId recId = new JceKeyTransRecipientId(cert); RecipientInformationStore recipients = m.getRecipientInfos(); RecipientInformation recipient = recipients.get(recId); JceKeyTransRecipient pKeyRecp = new JceKeyTransEnvelopedRecipient(pKey); decrypted = SMIMEUtil.toMimeBodyPart(recipient.getContent(pKeyRecp)); } catch (Exception e1) { throw new RuntimeException(e1); } return extractEntity(t, gt, ann, decrypted, providers); }
final byte [] aDecryptedData = aRecipient.getContent (new JceKeyTransEnvelopedRecipient (aPrivateKey).setProvider (m_sSecurityProviderName)); if (s_aDumpDecryptedDirectory != null)
envelopedData = ri.getContent(new JceKeyTransEnvelopedRecipient(privateKey)); break;
KeyStore keystore = KeyStore.getInstance("PKCS12", "BC"); keystore.load (new FileInputStream(PATH+"//test.p12"), "testpassword".toCharArray()); PrivateKey privateKey = (PrivateKey)keystore.getKey("testclientcert", "testpassword".toCharArray()); PEMReader pemReader = new PEMReader(new FileReader(filename)); ContentInfo object = (ContentInfo)pemReader.readObject(); CMSEnvelopedDataParser envDataParser = new CMSEnvelopedDataParser(object.getEncoded()); RecipientInformationStore recipients = envDataParser.getRecipientInfos(); Collection envCollection = recipients.getRecipients(); Iterator it = envCollection.iterator(); RecipientInformation recipient = (RecipientInformation) it.next(); byte[] result = recipient.getContent(privateKey, "BC"); String base64Encoded = new String(Base64.encode(result)); System.out.println(base64Encoded);
/** * Gets the content from a recipient. * @param recipientInfo * @param certificateKey * @param certificateKeyProvider * @return * @throws CMSException */ public static byte[] getContent(RecipientInformation recipientInfo, PrivateKey certificateKey, String certificateKeyProvider) throws CMSException { Recipient jceKeyTransRecipient = new JceKeyTransEnvelopedRecipient(certificateKey).setProvider(certificateKeyProvider); return recipientInfo.getContent(jceKeyTransRecipient); } }
/** * Gets the content from a recipient. */ public static byte[] getContent(RecipientInformation recipientInfo, PrivateKey certificateKey, String certificateKeyProvider) throws CMSException { Recipient jceKeyTransRecipient = new JceKeyTransEnvelopedRecipient(certificateKey).setProvider(certificateKeyProvider); return recipientInfo.getContent(jceKeyTransRecipient); }
@Override public void writeFile(OutputStream outputStream) throws IOException { if (currentFile.endsWith(".p7m")) { try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); asicReader.writeFile(byteArrayOutputStream); CMSEnvelopedDataParser cmsEnvelopedDataParser = new CMSEnvelopedDataParser(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())); // expect exactly one recipient Collection<?> recipients = cmsEnvelopedDataParser.getRecipientInfos().getRecipients(); if (recipients.size() != 1) throw new IllegalArgumentException(); // retrieve recipient and decode it RecipientInformation recipient = (RecipientInformation) recipients.iterator().next(); byte[] decryptedData = recipient.getContent(new JceKeyTransEnvelopedRecipient(privateKey).setProvider(BC)); ByteStreams.copy(new ByteArrayInputStream(decryptedData), outputStream); } catch (Exception e) { throw new IOException(e.getMessage(), e); } } else { asicReader.writeFile(outputStream); } }
recipient.getContent(recip); System.out.println("Alg OID: " + m.getEncryptionAlgOID());
public static String decode(byte[] encryptedData, String keyFile) { BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(new File(keyFile))); PEMParser parser = new PEMParser(reader); PEMKeyPair keyPair = (PEMKeyPair)parser.readObject(); AsymmetricKeyParameter privKeyParams = PrivateKeyFactory.createKey(keyPair.getPrivateKeyInfo()); parser.close(); CMSEnvelopedData data = new CMSEnvelopedData(encryptedData); RecipientInformationStore recipients = data.getRecipientInfos(); Iterator it = recipients.getRecipients().iterator(); if (it.hasNext()) { RecipientInformation recipient = (RecipientInformation)it.next(); byte[] recData = recipient.getContent(new BcRSAKeyTransEnvelopedRecipient(privKeyParams)); String decryptedValue = new String(recData); return decryptedValue; } } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) {try {reader.close();} catch (Exception e) {e.printStackTrace();}} } return null; }
private MimeBodyPart decryptPart(MimeBodyPart part) throws Exception { SMIMEEnveloped smimeEnveloped = new SMIMEEnveloped(part); RecipientInformationStore recipients = smimeEnveloped.getRecipientInfos(); RecipientInformation recipient = recipients.get(recId); if (null == recipient) { StringBuilder errorMessage = new StringBuilder(); errorMessage.append("This email wasn't encrypted with \"" + recId.toString() + "\".\n"); errorMessage.append("The encryption recId is: "); for (Object rec : recipients.getRecipients()) { if (rec instanceof RecipientInformation) { RecipientId recipientId = ((RecipientInformation) rec).getRID(); errorMessage.append("\"" + recipientId.toString() + "\"\n"); } } throw new Exception(errorMessage.toString()); } return toMimeBodyPart(recipient.getContent(ks.getKey(decryptionKeyAlias, null), BOUNCY_CASTLE_PROVIDER)); }
RecipientInformation recipient = recipients.get(recId); MimeBodyPart res = SMIMEUtil.toMimeBodyPart(recipient.getContent(pKey, ks.getProvider().getName()));
byte[] decryptedData = recipientInfo.getContent( new BcRSAKeyTransEnvelopedRecipient(PrivateKeyFactory.createKey(PrivateKeyInfo.getInstance(key.getEncoded()))));
byte[] messageData = info.getContent(getKeyTransRecipient()); LOGGER.debug("Finished decoding pkcsPkiEnvelope"); return messageData;
public void writeFile (final OutputStream aOS) throws IOException { if (m_sCurrentFile.endsWith (".p7m")) { try (final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream ()) { m_aAsicReader.writeFile (aBAOS); final CMSEnvelopedDataParser aCMSEnvelopedDataParser = new CMSEnvelopedDataParser (aBAOS.getAsInputStream ()); // expect exactly one recipient final Collection <RecipientInformation> aRecipients = aCMSEnvelopedDataParser.getRecipientInfos () .getRecipients (); if (aRecipients.size () != 1) throw new IllegalArgumentException ("Found not exactly one recipient but " + aRecipients.size ()); // retrieve recipient and decode it final RecipientInformation aRecipientInfo = aRecipients.iterator ().next (); final byte [] aDecryptedData = aRecipientInfo.getContent (new JceKeyTransEnvelopedRecipient (m_aPrivateKey).setProvider (PBCProvider.getProvider ())); AsicUtils.copyStream (new NonBlockingByteArrayInputStream (aDecryptedData), aOS); } catch (final CMSException e) { throw new IOException (e.getMessage (), e); } } else { m_aAsicReader.writeFile (aOS); } }
data = recipient.getContent(key, BC_PROVIDER);
public byte[] decrypt(CMSEnvelopedData envData) throws MessageDecodingException { ScepUtil.requireNonNull("envData", envData); final RecipientInformationStore recipientInfos = envData.getRecipientInfos(); RecipientInformation recipientInfo = null; EnvelopedDataDecryptorInstance decryptor = null; for (EnvelopedDataDecryptorInstance m : decryptors) { recipientInfo = recipientInfos.get(m.getRecipientId()); if (recipientInfo != null) { decryptor = m; break; } } if (recipientInfo == null || decryptor == null) { throw new MessageDecodingException("missing expected key transfer recipient"); } try { return recipientInfo.getContent(decryptor.getRecipient()); } catch (CMSException ex) { throw new MessageDecodingException("could not decrypt the envelopedData"); } }
JceKeyTransEnvelopedRecipient recipient = new JceKeyTransEnvelopedRecipient(keyHolder.getPrivateKey()); strippedMessage = SMIMEUtil.toMimeBodyPart(info.getContent(recipient)); LOGGER.info("Encrypted message decrypted"); } catch (Exception e) {
public static byte[] getEnvelopedData(PdfArray recipients, List<PdfObject> strings, Certificate certificate, Key certificateKey, String certificateKeyProvider) { byte[] envelopedData = null; for (PdfObject recipient : recipients.getElements()) { strings.remove(recipient); try { CMSEnvelopedData data = new CMSEnvelopedData(recipient.getBytes()); final Collection<RecipientInformation> recipientInformations = data.getRecipientInfos().getRecipients(); for (RecipientInformation recipientInfo : recipientInformations) { if (recipientInfo.getRID().match(certificate)) { // OJO... // https://www.bouncycastle.org/docs/pkixdocs1.5on/org/bouncycastle/cms/CMSEnvelopedData.html Recipient rec = new JceKeyTransEnvelopedRecipient( (PrivateKey) certificateKey) .setProvider(certificateKeyProvider); envelopedData = recipientInfo.getContent(rec); // ****************************************************************************** break; } } } catch (Exception f) { throw new ExceptionConverter(f); } } return envelopedData; } }
recip.setContentProvider(CryptoExtensions.getJCEProviderName()); final byte[] decryptedPayload = recipient.getContent(recip); if (LOGGER.isDebugEnabled())
RecipientInformation recipientInfo = data.getRecipientInfos().get(externalDecryptionProcess.getCmsRecipientId()); if (recipientInfo != null) { envelopedData = recipientInfo.getContent(externalDecryptionProcess.getCmsRecipient()); foundRecipient = true;