private void doTestEnc(
int size,
int privateValueSize,
BigInteger g,
BigInteger p)
{
ElGamalParameters dhParams = new ElGamalParameters(p, g, privateValueSize);
ElGamalKeyGenerationParameters ekgParams = new ElGamalKeyGenerationParameters(new SecureRandom(), dhParams);
ElGamalKeyPairGenerator kpGen = new ElGamalKeyPairGenerator();
kpGen.Init(ekgParams);
//
// generate pair
//
AsymmetricCipherKeyPair pair = kpGen.GenerateKeyPair();
ElGamalPublicKeyParameters pu = (ElGamalPublicKeyParameters) pair.Public;
ElGamalPrivateKeyParameters pv = (ElGamalPrivateKeyParameters) pair.Private;
checkKeySize(privateValueSize, pv);
ElGamalEngine e = new ElGamalEngine();
e.Init(true, pu);
if (e.GetOutputBlockSize() != size / 4)
{
Fail(size + " GetOutputBlockSize() on encryption failed.");
}
byte[] message = Hex.Decode("5468697320697320612074657374");
byte[] pText = message;
byte[] cText = e.ProcessBlock(pText, 0, pText.Length);
e.Init(false, pv);
if (e.GetOutputBlockSize() != (size / 8) - 1)
{
Fail(size + " GetOutputBlockSize() on decryption failed.");
}
pText = e.ProcessBlock(cText, 0, cText.Length);
if (!Arrays.AreEqual(message, pText))
{
Fail(size + " bit test failed");
}
e.Init(true, pu);
byte[] bytes = new byte[e.GetInputBlockSize() + 2];
try
{
e.ProcessBlock(bytes, 0, bytes.Length);
Fail("out of range block not detected");
}
catch (DataLengthException)
{
// expected
}
try
{
bytes[0] = (byte)0xff;
e.ProcessBlock(bytes, 0, bytes.Length - 1);
Fail("out of range block not detected");
}
catch (DataLengthException)
{
// expected
}
try
{
bytes[0] = (byte)0x7f;
e.ProcessBlock(bytes, 0, bytes.Length - 1);
}
catch (DataLengthException)
{
Fail("in range block failed");
}
}