KeyGenerator keyGen;
SecureRandom rand;
Cipher in = null;
Cipher out = null;
CipherInputStream cIn;
CipherOutputStream cOut;
ByteArrayInputStream bIn;
ByteArrayOutputStream bOut;
rand = new FixedSecureRandom();
try
{
String baseAlgorithm;
int index = algorithm.indexOf('/');
if (index > 0)
{
baseAlgorithm = algorithm.substring(0, index);
}
else
{
baseAlgorithm = algorithm;
}
keyGen = KeyGenerator.getInstance(baseAlgorithm, "BC");
if (!keyGen.getAlgorithm().equals(baseAlgorithm))
{
fail("wrong key generator returned!");
}
keyGen.init(rand);
key = keyGen.generateKey();
in = Cipher.getInstance(algorithm, "BC");
out = Cipher.getInstance(algorithm, "BC");
if (!in.getAlgorithm().startsWith(baseAlgorithm))
{
fail("wrong cipher returned!");
}
if (algorithm.startsWith("RC2"))
{
out.init(Cipher.ENCRYPT_MODE, key, rc2Spec, rand);
}
else if (algorithm.startsWith("RC5"))
{
if (algorithm.startsWith("RC5-64"))
{
out.init(Cipher.ENCRYPT_MODE, key, rc564Spec, rand);
}
else
{
out.init(Cipher.ENCRYPT_MODE, key, rc5Spec, rand);
}
}
else
{
out.init(Cipher.ENCRYPT_MODE, key, rand);
}
}
catch (Exception e)
{
fail("" + algorithm + " failed initialisation - " + e.toString(), e);
}
//
// grab the iv if there is one
//
try
{
if (algorithm.startsWith("RC2"))
{
in.init(Cipher.DECRYPT_MODE, key, rc2Spec);
}
else if (algorithm.startsWith("RC5"))
{
if (algorithm.startsWith("RC5-64"))
{
in.init(Cipher.DECRYPT_MODE, key, rc564Spec, rand);
}
else
{
in.init(Cipher.DECRYPT_MODE, key, rc5Spec, rand);
}
}
else
{
byte[] iv;
iv = out.getIV();
if (iv != null)
{
try
{
byte[] nIv = new byte[iv.length - 1];
in.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(nIv));
fail("failed to pick up short IV");
}
catch (InvalidAlgorithmParameterException e)
{
// ignore - this is what we want...
}
IvParameterSpec spec;
spec = new IvParameterSpec(iv);
in.init(Cipher.DECRYPT_MODE, key, spec);
}
else
{
in.init(Cipher.DECRYPT_MODE, key);
}
}
}
catch (Exception e)
{
fail("" + algorithm + " failed initialisation - " + e.toString());
}
//
// encryption pass
//
bOut = new ByteArrayOutputStream();
cOut = new CipherOutputStream(bOut, out);
try
{
for (int i = 0; i != input.length / 2; i++)
{
cOut.write(input[i]);
}
cOut.write(input, input.length / 2, input.length - input.length / 2);
cOut.close();
}
catch (IOException e)
{
fail("" + algorithm + " failed encryption - " + e.toString());
}