Ejemplo n.º 1
0
  /**
   * 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;
  }
Ejemplo n.º 2
0
  /**
   * 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());
  }