/** * 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; }
/** * 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(); }