Exemplo n.º 1
0
  /**
   * Decrypts an entity with the provided certificate's private key.
   *
   * @param encryptedEntity The entity that will be decrypted.
   * @param decryptingCertificate The certificate whose private key will be used to decrypt the
   *     message.
   * @return A MimeEntity containing the decrypted part.
   */
  public MimeEntity decrypt(MimeEntity encryptedEntity, X509CertificateEx decryptingCertificate) {
    if (encryptedEntity == null || decryptingCertificate == null) {
      throw new IllegalArgumentException();
    }

    if (!decryptingCertificate.hasPrivateKey()) {
      throw new IllegalArgumentException("Certificate has no private key");
    }

    encryptedEntity.verifyContentType(SMIMEStandard.EncryptedContentTypeHeaderValue);
    encryptedEntity.verifyTransferEncoding(MimeStandard.TransferEncodingBase64);

    Collection<X509CertificateEx> certs = new ArrayList<X509CertificateEx>();
    certs.add(decryptingCertificate);

    MimeEntity retVal = this.decrypt(encryptedEntity, certs);

    //
    // And turn the decrypted bytes back into an entity
    //
    return retVal;
  }