/**
   * Decrypts the data from <code>cipher</code> and stores the result in <code>plain</code>, then
   * verifies the signature of the data. If the decryption or verification fails, it throws a <code>
   * GnuPGException</code> .
   *
   * @param <code>cipher</code> holds the data to be decrypted and verified
   * @param <code>plain</code> holds the decrypted data after decryption
   * @see com.freiheit.gnupg.GnuPGData
   */
  public void decryptVerify(GnuPGData cipher, GnuPGData plain) throws GnuPGException {
    if (cipher == null || plain == null) return;

    gpgmeOpDecryptVerify(
        this.getInternalRepresentation(),
        cipher.getInternalRepresentation(),
        plain.getInternalRepresentation());
  }
 /** Imports a Key (private or public). You can supply the key in ASCII armor. */
 public void importKey(File file) throws GnuPGException, IOException {
   GnuPGData keydata = createDataObject(file);
   if (keydata == null) {
     Log.e(TAG, "importkey: parsing key data failed");
     return;
   }
   gpgmeOpImport(getInternalRepresentation(), keydata.getInternalRepresentation());
 }
 /**
  * Verifies a signature.
  *
  * @param signature TODO
  * @param signed TODO
  * @param plain TODO
  */
 public void verify(GnuPGData signature, GnuPGData signed, GnuPGData plain) throws GnuPGException {
   if (signature == null || signed == null || plain == null)
     throw new GnuPGException("Parameters not complete or null.");
   gpgmeOpVerify(
       this.getInternalRepresentation(),
       signature.getInternalRepresentation(),
       signed.getInternalRepresentation(),
       plain.getInternalRepresentation());
 }
  /**
   * Signs the data in <em>plain</em> and stores the result in <em>signature</em>.
   *
   * @param plain data that you want to sign
   * @param signature result of the operation
   */
  public void sign(GnuPGData plain, GnuPGData signature) throws GnuPGException {
    if (plain == null || signature == null)
      throw new GnuPGException("Parameters not complete or null.");

    gpgmeOpSign(
        this.getInternalRepresentation(),
        plain.getInternalRepresentation(),
        signature.getInternalRepresentation());
  }
  public void encryptAndSign(GnuPGKey[] recipients, GnuPGData plain, GnuPGData cipher)
      throws GnuPGException {
    if (hasNoRecipients(recipients) || plain == null || cipher == null)
      throw new GnuPGException("Encryption-Arguments not complete.");

    gpgmeOpEncryptSign(
        this.getInternalRepresentation(),
        getInternalRepresentationFromRecipients(recipients),
        plain.getInternalRepresentation(),
        cipher.getInternalRepresentation());
  }
 /**
  * Encrypt plain text and return ASCII-armored text.
  *
  * @param recipients array of keys to encrypt to
  * @param plain the GnuPGData to be encrypted
  * @return String encrypted data in ASCII-armored text
  */
 public String encryptToAscii(GnuPGKey[] recipients, GnuPGData plain) {
   long l = getInternalRepresentation();
   boolean previous = gpgmeGetArmor(l);
   gpgmeSetArmor(l, true);
   GnuPGData cipher = createDataObject();
   encrypt(recipients, plain, cipher);
   final String ret = cipher.toString();
   cipher.destroy();
   if (previous == false) // maintain the original ASCII-Armor state
   gpgmeSetArmor(l, false);
   return ret;
 }
  /**
   * Decrypts the data from <em>cipher</em> and returns the result.
   *
   * @param cipher the data to be decrypted
   * @return plain the resulting decrypted data
   * @see com.freiheit.gnupg.GnuPGData
   */
  public GnuPGData decrypt(String cipher) throws GnuPGException {
    if (cipher == null || cipher.length() == 0)
      throw new GnuPGException("Encryption arguments not complete.");

    GnuPGData plainData = createDataObject();
    final GnuPGData cipherData = createDataObject(cipher);

    gpgmeOpDecrypt(
        this.getInternalRepresentation(),
        cipherData.getInternalRepresentation(),
        plainData.getInternalRepresentation());
    cipherData.destroy();
    return plainData;
  }
  /**
   * Encrypts the text in <em>plain</em> with the public key of each recipient. The result is
   * returned as GnuPGData.
   *
   * @param recipients array of the keys to encrypt to
   * @param plain the text to be encrypted
   * @return GnuPGData the encrypted data
   * @see com.freiheit.gnupg.GnuPGData
   * @see com.freiheit.gnupg.GnuPGKey
   */
  public GnuPGData encrypt(GnuPGKey[] recipients, String plain) throws GnuPGException {
    if (hasNoRecipients(recipients) || plain == null || plain.equals(""))
      throw new GnuPGException("Encryption arguments not complete.");

    final GnuPGData plainData = createDataObject(plain);
    GnuPGData cipherData = createDataObject();

    gpgmeOpEncrypt(
        this.getInternalRepresentation(),
        getInternalRepresentationFromRecipients(recipients),
        plainData.getInternalRepresentation(),
        cipherData.getInternalRepresentation());
    plainData.destroy();
    return cipherData;
  }
 /**
  * Encrypt plain data and return encrypted data in binary form.
  *
  * @param recipients array of keys to encrypt to
  * @param plain the GnuPGData to be encrypted
  * @return String encrypted data in ASCII-armored text
  */
 public byte[] encryptToBinary(GnuPGKey[] recipients, GnuPGData plain) {
   long l = getInternalRepresentation();
   boolean previous = gpgmeGetArmor(l);
   gpgmeSetArmor(l, true);
   GnuPGData cipher = createDataObject();
   encrypt(recipients, plain, cipher);
   ByteArrayOutputStream baos = new ByteArrayOutputStream(plain.size());
   BufferedOutputStream out = new BufferedOutputStream(baos, 8192);
   try {
     cipher.write(out);
   } catch (IOException e) {
     e.printStackTrace();
   }
   cipher.destroy();
   if (previous == false) // maintain the original ASCII-Armor state
   gpgmeSetArmor(l, false);
   return baos.toByteArray();
 }
  /**
   * Encrypts the data from <em>plain</em> with the public key of each recipient. The result is
   * stored in <em>cipher</em>.
   *
   * @param recipients Array with the public keys of all recipients
   * @param plain text, that should be encrypted
   * @param cipher text, the encrypted plain text after method call
   * @see com.freiheit.gnupg.GnuPGData
   * @see com.freiheit.gnupg.GnuPGKey
   */
  public void encrypt(GnuPGKey[] recipients, GnuPGData plain, GnuPGData cipher)
      throws GnuPGException {
    if (hasNoRecipients(recipients) || plain == null || cipher == null)
      throw new GnuPGException("Encryption arguments not complete.");

    // note that these are pointers to addresses in the javagnupg shared lib
    long context = this.getInternalRepresentation();
    if (gpgmeGetSignersLength(context) == 0)
      gpgmeOpEncrypt(
          context,
          getInternalRepresentationFromRecipients(recipients),
          plain.getInternalRepresentation(),
          cipher.getInternalRepresentation());
    else
      gpgmeOpEncryptSign(
          context,
          getInternalRepresentationFromRecipients(recipients),
          plain.getInternalRepresentation(),
          cipher.getInternalRepresentation());
  }
 /**
  * Export the keys defined by the pattern.
  *
  * @param pattern pattern for the keys
  * @param reserved not used, must be set 0. For later use revered.
  * @param data empty data object. Will be filled with the keys.
  */
 public void export(String pattern, long reserved, GnuPGData data) {
   gpgmeOpExport(getInternalRepresentation(), pattern, 0, data.getInternalRepresentation());
 }
 /** Imports a Key (private or public). You can supply the key in ASCII armor. */
 public void importKey(GnuPGData keydata) throws GnuPGException {
   gpgmeOpImport(getInternalRepresentation(), keydata.getInternalRepresentation());
 }
 /**
  * Encrypt plain text and return encrypted data in binary form.
  *
  * @param recipients array of keys to encrypt to
  * @param plain the plain text to be encrypted
  * @return byte[] encrypted data in binary data
  */
 public byte[] encryptToBinary(GnuPGKey[] recipients, String plain) {
   final GnuPGData plainData = createDataObject(plain);
   final byte[] ret = encryptToBinary(recipients, plainData);
   plainData.destroy();
   return ret;
 }
 /**
  * Encrypt a byte array and return ASCII-armored text.
  *
  * @param recipients array of keys to encrypt to
  * @param plain the byte[] to be encrypted
  * @return String encrypted data in ASCII-armored text
  */
 public String encryptToAscii(GnuPGKey[] recipients, byte[] plain) {
   final GnuPGData plainData = createDataObject(plain);
   final String ret = encryptToAscii(recipients, plainData);
   plainData.destroy();
   return ret;
 }