//
// encrypt
//
ByteArrayOutputStream cbOut = new ByteArrayOutputStream();
PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(SymmetricKeyAlgorithmTags.TRIPLE_DES, new SecureRandom(), "BC");
PGPPublicKey puK = sKey.getSecretKey(pgpKeyID).getPublicKey();
cPk.addMethod(puK);
OutputStream cOut = cPk.open(new UncloseableOutputStream(cbOut), bOut.toByteArray().length);
cOut.write(text);
cOut.close();
pgpF = new PGPObjectFactory(cbOut.toByteArray());
encList = (PGPEncryptedDataList)pgpF.nextObject();
encP = (PGPPublicKeyEncryptedData)encList.get(0);
pgpPrivKey = sKey.getSecretKey(pgpKeyID).extractPrivateKey(pass, "BC");
clear = encP.getDataStream(pgpPrivKey, "BC");
bOut.reset();
while ((ch = clear.read()) >= 0)
{
bOut.write(ch);
}
out = bOut.toByteArray();
if (!areEqual(out, text))
{
fail("wrong plain text in generated packet");
}
//
// use of PGPKeyPair
//
BigInteger g = new BigInteger("153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc", 16);
BigInteger p = new BigInteger("9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b", 16);
KeyPairGenerator kpg = KeyPairGenerator.getInstance("ElGamal", "BC");
ElGamalParameterSpec elParams = new ElGamalParameterSpec(p, g);
kpg.initialize(elParams);
KeyPair kp = kpg.generateKeyPair();
PGPKeyPair pgpKp = new PGPKeyPair(PGPPublicKey.ELGAMAL_GENERAL , kp.getPublic(), kp.getPrivate(), new Date(), "BC");
PGPPublicKey k1 = pgpKp.getPublicKey();
PGPPrivateKey k2 = pgpKp.getPrivateKey();
// Test bug with ElGamal P size != 0 mod 8 (don't use these sizes at home!)
SecureRandom random = new SecureRandom();
for (int pSize = 257; pSize < 264; ++pSize)
{
// Generate some parameters of the given size
AlgorithmParameterGenerator a = AlgorithmParameterGenerator.getInstance("ElGamal", "BC");
a.init(pSize, new SecureRandom());
AlgorithmParameters params = a.generateParameters();
DHParameterSpec elP = (DHParameterSpec)params.getParameterSpec(DHParameterSpec.class);
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ElGamal", "BC");
keyGen.initialize(elP);
// Run a short encrypt/decrypt test with random key for the given parameters
kp = keyGen.generateKeyPair();
PGPKeyPair elGamalKeyPair = new PGPKeyPair(
PublicKeyAlgorithmTags.ELGAMAL_GENERAL, kp, new Date(), "BC");
cPk = new PGPEncryptedDataGenerator(SymmetricKeyAlgorithmTags.CAST5, random, "BC");
puK = elGamalKeyPair.getPublicKey();
cPk.addMethod(puK);
cbOut = new ByteArrayOutputStream();
cOut = cPk.open(cbOut, text.length);
cOut.write(text);
cOut.close();