// encrypt - with stream close
//
ByteArrayOutputStream cbOut = new ByteArrayOutputStream();
PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(PGPEncryptedData.CAST5).setSecureRandom(new SecureRandom()));
cPk.addMethod(new BcPBEKeyEncryptionMethodGenerator(pass));
OutputStream cOut = cPk.open(new UncloseableOutputStream(cbOut), bOut.toByteArray().length);
cOut.write(bOut.toByteArray());
cOut.close();
out = decryptMessage(cbOut.toByteArray(), cDate);
if (!areEqual(out, text))
{
fail("wrong plain text in generated packet");
}
//
// encrypt - with generator close
//
cbOut = new ByteArrayOutputStream();
cPk = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(PGPEncryptedData.CAST5).setSecureRandom(new SecureRandom()));
cPk.addMethod(new BcPBEKeyEncryptionMethodGenerator(pass));
cOut = cPk.open(new UncloseableOutputStream(cbOut), bOut.toByteArray().length);
cOut.write(bOut.toByteArray());
cPk.close();
out = decryptMessage(cbOut.toByteArray(), cDate);
if (!areEqual(out, text))
{
fail("wrong plain text in generated packet");
}
//
// encrypt - partial packet style.
//
SecureRandom rand = new SecureRandom();
byte[] test = new byte[1233];
rand.nextBytes(test);
bOut = new ByteArrayOutputStream();
comData = new PGPCompressedDataGenerator(
PGPCompressedData.ZIP);
comOut = comData.open(bOut);
lData = new PGPLiteralDataGenerator();
ldOut = lData.open(new UncloseableOutputStream(comOut),
PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, TEST_DATE,
new byte[16]);
ldOut.write(test);
ldOut.close();
comOut.close();
cbOut = new ByteArrayOutputStream();
cPk = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(PGPEncryptedData.CAST5).setSecureRandom(rand));
cPk.addMethod(new BcPBEKeyEncryptionMethodGenerator(pass));
cOut = cPk.open(new UncloseableOutputStream(cbOut), new byte[16]);
cOut.write(bOut.toByteArray());
cOut.close();
out = decryptMessage(cbOut.toByteArray(), TEST_DATE);
if (!areEqual(out, test))
{
fail("wrong plain text in generated packet");
}
//
// with integrity packet
//
cbOut = new ByteArrayOutputStream();
cPk = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(PGPEncryptedData.CAST5).setWithIntegrityPacket(true).setSecureRandom(rand));
cPk.addMethod(new BcPBEKeyEncryptionMethodGenerator(pass));
cOut = cPk.open(new UncloseableOutputStream(cbOut), new byte[16]);
cOut.write(bOut.toByteArray());
cOut.close();
out = decryptMessage(cbOut.toByteArray(), TEST_DATE);
if (!areEqual(out, test))
{
fail("wrong plain text in generated packet");
}
//
// decrypt with buffering
//
out = decryptMessageBuffered(cbOut.toByteArray(), TEST_DATE);
if (!areEqual(out, test))
{
fail("wrong plain text in buffer generated packet");
}
//
// sample message
//
PGPObjectFactory pgpFact = new PGPObjectFactory(testPBEAsym);
PGPEncryptedDataList enc = (PGPEncryptedDataList)pgpFact.nextObject();
PGPPBEEncryptedData pbe = (PGPPBEEncryptedData)enc.get(1);
InputStream clear = pbe.getDataStream(new BcPBEDataDecryptorFactory("password".toCharArray(), new BcPGPDigestCalculatorProvider()));
pgpFact = new PGPObjectFactory(clear);
PGPLiteralData ld = (PGPLiteralData)pgpFact.nextObject();
bOut = new ByteArrayOutputStream();
InputStream unc = ld.getInputStream();
int ch;
while ((ch = unc.read()) >= 0)
{
bOut.write(ch);
}
if (!areEqual(bOut.toByteArray(), Hex.decode("5361742031302e30322e30370d0a")))
{
fail("data mismatch on combined PBE");
}
//
// with integrity packet - one byte message
//
byte[] msg = new byte[1];
bOut = new ByteArrayOutputStream();
comData = new PGPCompressedDataGenerator(
PGPCompressedData.ZIP);
lData = new PGPLiteralDataGenerator();
comOut = comData.open(new UncloseableOutputStream(bOut));
ldOut = lData.open(
new UncloseableOutputStream(comOut),
PGPLiteralData.BINARY,
PGPLiteralData.CONSOLE,
msg.length,
cDate);
ldOut.write(msg);
ldOut.close();
comOut.close();
cbOut = new ByteArrayOutputStream();
cPk = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(PGPEncryptedData.CAST5).setWithIntegrityPacket(true).setSecureRandom(rand));
cPk.addMethod(new BcPBEKeyEncryptionMethodGenerator(pass));
cOut = cPk.open(new UncloseableOutputStream(cbOut), new byte[16]);
cOut.write(bOut.toByteArray());