kpg.initialize(elParams);
KeyPair kp = kpg.generateKeyPair();
PGPKeyPair pgpKp = new PGPKeyPair(PGPPublicKey.ELGAMAL_GENERAL , kp.getPublic(), kp.getPrivate(), new Date());
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());
cPk = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.CAST5).setSecureRandom(random));
puK = elGamalKeyPair.getPublicKey();
cPk.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(puK));
cbOut = new ByteArrayOutputStream();
cOut = cPk.open(cbOut, text.length);
cOut.write(text);
cOut.close();
pgpF = new PGPObjectFactory(cbOut.toByteArray());
encList = (PGPEncryptedDataList)pgpF.nextObject();
encP = (PGPPublicKeyEncryptedData)encList.get(0);
pgpPrivKey = elGamalKeyPair.getPrivateKey();
// Note: This is where an exception would be expected if the P size causes problems
clear = encP.getDataStream(new BcPublicKeyDataDecryptorFactory(pgpPrivKey));
ByteArrayOutputStream dec = new ByteArrayOutputStream();