List of usage examples for org.bouncycastle.cms.jcajce JceKeyTransEnvelopedRecipient JceKeyTransEnvelopedRecipient
public JceKeyTransEnvelopedRecipient(PrivateKey recipientKey)
From source file:br.ufpb.dicomflow.integrationAPI.mail.impl.SMTPServiceExtractor.java
License:Open Source License
private Multipart getDecryptedContent(Message message, X509Certificate signCert, X509Certificate encryptCert, PrivateKey privateKey) throws OperatorCreationException, Exception { RecipientId recId = new JceKeyTransRecipientId(encryptCert); SMIMEEnveloped m = new SMIMEEnveloped((MimeMessage) message); RecipientInformationStore recipients = m.getRecipientInfos(); RecipientInformation recipient = recipients.get(recId); MimeBodyPart res = SMIMEUtil/*w ww.j a va2s .c o m*/ .toMimeBodyPart(recipient.getContent(new JceKeyTransEnvelopedRecipient(privateKey))); MimeMultipart content = (MimeMultipart) res.getContent(); Multipart decryptedContent = null; if (checkSignature(content, signCert)) { for (int i = 0; i < content.getCount(); i++) { Part part = content.getBodyPart(i); String contentType = part.getContentType(); if (contentType.toLowerCase().startsWith("multipart/mixed")) { decryptedContent = (Multipart) part.getContent(); } } } return decryptedContent; }
From source file:com.itextpdf.kernel.pdf.PdfEncryptor.java
License:Open Source License
/** * Gets the content from a recipient.//from w w w .jav a2s . c o m */ public static byte[] getContent(RecipientInformation recipientInfo, PrivateKey certificateKey, String certificateKeyProvider) throws CMSException { Recipient jceKeyTransRecipient = new JceKeyTransEnvelopedRecipient(certificateKey) .setProvider(certificateKeyProvider); return recipientInfo.getContent(jceKeyTransRecipient); }
From source file:com.maiereni.host.web.util.impl.BouncyCastleEncryptorImpl.java
License:Apache License
public byte[] decryptData(@Nonnull final byte[] encryptedData) throws Exception { CMSEnvelopedData envelopedData = new CMSEnvelopedData(encryptedData); Collection<RecipientInformation> recip = envelopedData.getRecipientInfos().getRecipients(); KeyTransRecipientInformation recipientInfo = (KeyTransRecipientInformation) recip.iterator().next(); JceKeyTransRecipient recipient = new JceKeyTransEnvelopedRecipient(key); return recipientInfo.getContent(recipient); }
From source file:com.silverpeas.util.cryptage.SilverCryptFactoryAsymetric.java
License:Open Source License
public String goUnCrypting(byte[] stringCrypted, String fileName) throws CryptageException { try {//from ww w . ja va 2s. c o m // Chargement de la chaine dchiffrer byte[] pkcs7envelopedData = stringCrypted; // Dchiffrement de la chaine CMSEnvelopedData ced = new CMSEnvelopedData(pkcs7envelopedData); @SuppressWarnings("unchecked") Collection<KeyTransRecipientInformation> recip = ced.getRecipientInfos().getRecipients(); KeyTransRecipientInformation rinfo = recip.iterator().next(); // privatekey est la cl prive permettant de dchiffrer la cl // secrte (symtrique) byte[] contents = rinfo .getContent(new JceKeyTransEnvelopedRecipient(this.getKeys(fileName).getPrivatekey())); return byteArrayToString(contents); } catch (CryptageException e) { throw e; } catch (Exception e) { throw new CryptageException("SilverCryptFactory.goUnCrypting", SilverpeasException.ERROR, "util.UNCRYPT_FAILED", e); } }
From source file:com.zotoh.crypto.CryptoUte.java
License:Open Source License
private static CMSTypedStream smime_decrypt(PrivateKey key, SMIMEEnveloped env) throws MessagingException, GeneralSecurityException, IOException { Recipient r = new JceKeyTransEnvelopedRecipient(key).setProvider(Crypto.getInstance().getProvider()); for (Object obj : env.getRecipientInfos().getRecipients()) { try {//www .jav a 2 s .c o m return ((RecipientInformation) obj).getContentStream(r); } catch (CMSException e) { } } return null; }
From source file:de.mendelson.comm.as2.message.AS2MessageParser.java
/** * Decrypts the data of a message with all given certificates etc * * @param info MessageInfo, the encryption algorith will be stored in the * encryption type of this info//from w w w .ja v a 2 s . com * @param rawMessageData encrypted data, will be decrypted * @param contentType contentType of the data * @param privateKey receivers private key * @param certificate receivers certificate */ public byte[] decryptData(AS2Message message, byte[] data, String contentType, PrivateKey privateKeyReceiver, X509Certificate certificateReceiver, String receiverCryptAlias) throws Exception { AS2MessageInfo info = (AS2MessageInfo) message.getAS2Info(); MimeBodyPart encryptedBody = new MimeBodyPart(); encryptedBody.setHeader("content-type", contentType); encryptedBody.setDataHandler(new DataHandler(new ByteArrayDataSource(data, contentType))); JceKeyTransRecipientId recipientId = new JceKeyTransRecipientId(certificateReceiver); SMIMEEnveloped enveloped = new SMIMEEnveloped(encryptedBody); BCCryptoHelper helper = new BCCryptoHelper(); String algorithm = helper.convertOIDToAlgorithmName(enveloped.getEncryptionAlgOID()); if (algorithm.equals(BCCryptoHelper.ALGORITHM_3DES)) { info.setEncryptionType(AS2Message.ENCRYPTION_3DES); } else if (algorithm.equals(BCCryptoHelper.ALGORITHM_DES)) { info.setEncryptionType(AS2Message.ENCRYPTION_DES); } else if (algorithm.equals(BCCryptoHelper.ALGORITHM_RC2)) { info.setEncryptionType(AS2Message.ENCRYPTION_RC2_UNKNOWN); } else if (algorithm.equals(BCCryptoHelper.ALGORITHM_AES_128)) { info.setEncryptionType(AS2Message.ENCRYPTION_AES_128); } else if (algorithm.equals(BCCryptoHelper.ALGORITHM_AES_192)) { info.setEncryptionType(AS2Message.ENCRYPTION_AES_192); } else if (algorithm.equals(BCCryptoHelper.ALGORITHM_AES_256)) { info.setEncryptionType(AS2Message.ENCRYPTION_AES_256); } else if (algorithm.equals(BCCryptoHelper.ALGORITHM_RC4)) { info.setEncryptionType(AS2Message.ENCRYPTION_RC4_UNKNOWN); } else { info.setEncryptionType(AS2Message.ENCRYPTION_UNKNOWN_ALGORITHM); } RecipientInformationStore recipients = enveloped.getRecipientInfos(); enveloped = null; encryptedBody = null; RecipientInformation recipient = recipients.get(recipientId); if (recipient == null) { //give some details about the required and used cert for the decryption Collection recipientList = recipients.getRecipients(); Iterator iterator = recipientList.iterator(); while (iterator.hasNext()) { RecipientInformation recipientInfo = (RecipientInformation) iterator.next(); if (this.logger != null) { this.logger.log(Level.SEVERE, this.rb.getResourceString("decryption.inforequired", new Object[] { info.getMessageId(), ((KeyTransRecipientId) recipientInfo.getRID()).getIssuer() + ", " + ((KeyTransRecipientId) recipientInfo.getRID()).getSerialNumber() }), info); } } if (this.logger != null) { this.logger.log( Level.SEVERE, this.rb .getResourceString("decryption.infoassigned", new Object[] { info.getMessageId(), receiverCryptAlias, recipientId.getIssuer() + ", " + recipientId.getSerialNumber() }), info); } throw new AS2Exception(AS2Exception.AUTHENTIFICATION_ERROR, "Error decrypting the message: Recipient certificate does not match.", message); } //Streamed decryption. Its also possible to use in memory decryption using getContent but that uses //far more memory. InputStream contentStream = recipient .getContentStream(new JceKeyTransEnvelopedRecipient(privateKeyReceiver).setProvider("BC")) .getContentStream(); //InputStream contentStream = recipient.getContentStream(privateKeyReceiver, "BC").getContentStream(); //threshold set to 20 MB: if the data is less then 20MB perform the operaion in memory else stream to disk DeferredFileOutputStream decryptedOutput = new DeferredFileOutputStream(20 * 1024 * 1024, "as2decryptdata_", ".mem", null); this.copyStreams(contentStream, decryptedOutput); decryptedOutput.flush(); decryptedOutput.close(); contentStream.close(); byte[] decryptedData = null; //size of the data was < than the threshold if (decryptedOutput.isInMemory()) { decryptedData = decryptedOutput.getData(); } else { //data has been written to a temp file: reread and return ByteArrayOutputStream memOut = new ByteArrayOutputStream(); decryptedOutput.writeTo(memOut); memOut.flush(); memOut.close(); //finally delete the temp file boolean deleted = decryptedOutput.getFile().delete(); decryptedData = memOut.toByteArray(); } if (this.logger != null) { this.logger.log(Level.INFO, this.rb.getResourceString("decryption.done.alias", new Object[] { info.getMessageId(), receiverCryptAlias, this.rbMessage.getResourceString("encryption." + info.getEncryptionType()) }), info); } return (decryptedData); }
From source file:de.mendelson.util.security.BCCryptoHelper.java
/** * Used for mendelson rosettaNet/*from www. j a v a2 s . co m*/ */ public MimeBodyPart decrypt(MimeBodyPart part, Certificate cert, Key key) throws GeneralSecurityException, MessagingException, CMSException, IOException, SMIMEException { if (!this.isEncrypted(part)) { throw new GeneralSecurityException( "decrypt: Unable to decrypt - Content-Type indicates data isn't encrypted"); } X509Certificate x509Cert = castCertificate(cert); SMIMEEnveloped envelope = new SMIMEEnveloped(part); RecipientId recipientId = new JceKeyTransRecipientId(x509Cert); RecipientInformation recipient = envelope.getRecipientInfos().get(recipientId); if (recipient == null) { throw new GeneralSecurityException( "decrypt: Unable to decrypt data - wrong key used to decrypt the data."); } else { MimeBodyPart bodyPart = SMIMEUtil.toMimeBodyPart(recipient.getContentStream( new JceKeyTransEnvelopedRecipient(this.getPrivateKey(key)).setProvider("BC"))); return (bodyPart); } }
From source file:de.mendelson.util.security.BCCryptoHelper.java
/** * Decrypts a formerly encrypted stream. An exception will be thrown if * decryption is not possible//from w ww .ja v a 2s . c o m */ public void decryptCMS(InputStream encrypted, OutputStream decrypted, Certificate cert, Key key) throws Exception { BufferedInputStream bufferedEncrypted = new BufferedInputStream(encrypted); BufferedOutputStream bufferedDecrypted = new BufferedOutputStream(decrypted); X509Certificate x509Cert = this.castCertificate(cert); CMSEnvelopedDataParser parser = new CMSEnvelopedDataParser(bufferedEncrypted); RecipientId recipientId = new JceKeyTransRecipientId(x509Cert); RecipientInformation recipient = parser.getRecipientInfos().get(recipientId); if (recipient != null) { CMSTypedStream cmsEncrypted = recipient .getContentStream(new JceKeyTransEnvelopedRecipient(this.getPrivateKey(key)).setProvider("BC")); InputStream encryptedContent = cmsEncrypted.getContentStream(); this.copyStreams(encryptedContent, bufferedDecrypted); bufferedDecrypted.flush(); } else { throw new GeneralSecurityException("Wrong key used to decrypt the data."); } }
From source file:eu.inn.biometric.signature.crypto.BCCryptoProvider.java
License:Open Source License
@Override public byte[] decrypt(byte[] data, PrivateKey key) { try {/*w w w .j a v a2 s .c o m*/ CMSEnvelopedData enveloped = new CMSEnvelopedData(data); for (Object recip : enveloped.getRecipientInfos().getRecipients()) { try { KeyTransRecipientInformation rinfo = (KeyTransRecipientInformation) recip; byte[] decryptedDocument = rinfo.getContent(new JceKeyTransEnvelopedRecipient(key)); return decryptedDocument; } catch (Exception ex) { } } throw new RuntimeException("Cannot decrypt"); } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:mitm.common.security.cms.RecipientInfoImpl.java
License:Open Source License
private Recipient getRecipient(Key key) throws RecipientInfoException { Check.notNull(key, "key"); /*//from w ww . java 2 s . c o m * We currently only support Public/Private keys */ if (!(key instanceof PrivateKey)) { throw new RecipientInfoException("The key is not a PrivateKey"); } JceKeyTransEnvelopedRecipient recipient = new JceKeyTransEnvelopedRecipient((PrivateKey) key); recipient.setProvider(sensitiveProvider); recipient.setContentProvider(nonSensitiveProvider); return recipient; }