//
// verify generated signature
//
pgpFact = new PGPObjectFactory(bOut.toByteArray());
PGPCompressedData c1 = (PGPCompressedData)pgpFact.nextObject();
pgpFact = new PGPObjectFactory(c1.getDataStream());
PGPOnePassSignatureList p1 = (PGPOnePassSignatureList)pgpFact.nextObject();
PGPOnePassSignature ops = p1.get(0);
PGPLiteralData p2 = (PGPLiteralData)pgpFact.nextObject();
if (!p2.getModificationTime().equals(testDate))
{
fail("Modification time not preserved");
}
InputStream dIn = p2.getInputStream();
ops.initVerify(pubKey, "BC");
while ((ch = dIn.read()) >= 0)
{
ops.update((byte)ch);
}
PGPSignatureList p3 = (PGPSignatureList)pgpFact.nextObject();
if (!ops.verify(p3.get(0)))
{
fail("Failed generated signature check");
}
//
// test encryption
//
//
// find a key suitable for encryption
//
long pgpKeyID = 0;
PublicKey pKey = null;
Iterator it = pgpPub.getPublicKeys();
while (it.hasNext())
{
PGPPublicKey pgpKey = (PGPPublicKey)it.next();
if (pgpKey.getAlgorithm() == PGPPublicKey.ELGAMAL_ENCRYPT
|| pgpKey.getAlgorithm() == PGPPublicKey.ELGAMAL_GENERAL)
{
pKey = pgpKey.getKey("BC");
pgpKeyID = pgpKey.getKeyID();
if (pgpKey.getBitStrength() != 1024)
{
fail("failed - key strength reported incorrectly.");
}
//
// verify the key
//
}
}
Cipher c = Cipher.getInstance("ElGamal/None/PKCS1Padding", "BC");
c.init(Cipher.ENCRYPT_MODE, pKey);
byte[] in = "hello world".getBytes();
byte[] out = c.doFinal(in);
pgpPrivKey = sKey.getSecretKey(pgpKeyID).extractPrivateKey(pass, "BC");
c.init(Cipher.DECRYPT_MODE, pgpPrivKey.getKey());
out = c.doFinal(out);
if (!areEqual(in, out))
{
fail("decryption failed.");
}
//
// encrypted message
//
byte[] text = { (byte)'h', (byte)'e', (byte)'l', (byte)'l', (byte)'o', (byte)' ', (byte)'w', (byte)'o', (byte)'r', (byte)'l', (byte)'d', (byte)'!', (byte)'\n' };
PGPObjectFactory pgpF = new PGPObjectFactory(encMessage);
PGPEncryptedDataList encList = (PGPEncryptedDataList)pgpF.nextObject();
PGPPublicKeyEncryptedData encP = (PGPPublicKeyEncryptedData)encList.get(0);
InputStream clear = encP.getDataStream(pgpPrivKey, "BC");
pgpFact = new PGPObjectFactory(clear);
c1 = (PGPCompressedData)pgpFact.nextObject();
pgpFact = new PGPObjectFactory(c1.getDataStream());
PGPLiteralData ld = (PGPLiteralData)pgpFact.nextObject();
bOut = new ByteArrayOutputStream();
if (!ld.getFileName().equals("test.txt"))
{
throw new RuntimeException("wrong filename in packet");
}
InputStream inLd = ld.getDataStream();
while ((ch = inLd.read()) >= 0)
{
bOut.write(ch);
}
if (!areEqual(bOut.toByteArray(), text))
{
fail("wrong plain text in decrypted packet");
}
//
// signed and encrypted message
//
pgpF = new PGPObjectFactory(signedAndEncMessage);
encList = (PGPEncryptedDataList)pgpF.nextObject();
encP = (PGPPublicKeyEncryptedData)encList.get(0);
clear = encP.getDataStream(pgpPrivKey, "BC");
pgpFact = new PGPObjectFactory(clear);
c1 = (PGPCompressedData)pgpFact.nextObject();
pgpFact = new PGPObjectFactory(c1.getDataStream());
p1 = (PGPOnePassSignatureList)pgpFact.nextObject();
ops = p1.get(0);