fail("failed exception test.", e);
}
try
{
KeyGenerator kg = KeyGenerator.getInstance("DESede", "BC");
try
{
kg.init(Integer.MIN_VALUE, new SecureRandom());
fail("failed exception test - no exception thrown");
}
catch (InvalidParameterException e)
{
// ignore okay
}
catch (Exception e)
{
fail("failed exception test.", e);
}
}
catch (Exception e)
{
fail("unexpected exception.", e);
}
try
{
skF = SecretKeyFactory.getInstance("DESede", "BC");
try
{
skF.translateKey(null);
fail("failed exception test - no exception thrown");
}
catch (InvalidKeyException e)
{
// ignore okay
}
catch (Exception e)
{
fail("failed exception test.", e);
}
}
catch (Exception e)
{
fail("unexpected exception.", e);
}
try
{
byte[] rawDESKey = { (byte)128, (byte)131, (byte)133, (byte)134,
(byte)137, (byte)138, (byte)140, (byte)143 };
SecretKeySpec cipherKey = new SecretKeySpec(rawDESKey, "DES");
Cipher cipher = Cipher.getInstance("DES/CBC/NoPadding", "BC");
try
{
// According specification engineInit(int opmode, Key key,
// SecureRandom random) throws InvalidKeyException if this
// cipher is being
// initialized for decryption and requires algorithm parameters
// that cannot be determined from the given key
cipher.init(Cipher.DECRYPT_MODE, cipherKey, (SecureRandom)null);
fail("failed exception test - no InvalidKeyException thrown");
}
catch (InvalidKeyException e)
{
// ignore
}
}
catch (Exception e)
{
fail("unexpected exception.", e);
}
try
{
byte[] rawDESKey = { -128, -125, -123, -122, -119, -118 };
SecretKeySpec cipherKey = new SecretKeySpec(rawDESKey, "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding", "BC");
try
{
// According specification engineInit(int opmode, Key key,
// SecureRandom random) throws InvalidKeyException if the given
// key is inappropriate for initializing this cipher
cipher.init(Cipher.ENCRYPT_MODE, cipherKey);
fail("failed exception test - no InvalidKeyException thrown");
}
catch (InvalidKeyException e)
{
// ignore
}
}
catch (Exception e)
{
fail("unexpected exception.", e);
}
try
{
byte[] rawDESKey = { -128, -125, -123, -122, -119, -118, -117, -115, -114 };
SecretKeySpec cipherKey = new SecretKeySpec(rawDESKey, "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding", "BC");
try
{
// According specification engineInit(int opmode, Key key,
// SecureRandom random) throws InvalidKeyException if the given
// key is inappropriate for initializing this cipher
cipher.init(Cipher.ENCRYPT_MODE, cipherKey);
fail("failed exception test - no InvalidKeyException thrown");
}
catch (InvalidKeyException e)
{
// ignore
}
}
catch (Exception e)
{
fail("unexpected exception.", e);
}
try
{
byte[] rawDESKey = { (byte)128, (byte)131, (byte)133, (byte)134,
(byte)137, (byte)138, (byte)140, (byte)143 };
SecretKeySpec cipherKey = new SecretKeySpec(rawDESKey, "DES");
Cipher ecipher = Cipher.getInstance("DES/ECB/PKCS5Padding", "BC");
ecipher.init(Cipher.ENCRYPT_MODE, cipherKey);
byte[] cipherText = new byte[0];
try
{
// According specification Method engineUpdate(byte[] input,
// int inputOffset, int inputLen, byte[] output, int
// outputOffset)
// throws ShortBufferException - if the given output buffer is
// too
// small to hold the result
ecipher.update(new byte[20], 0, 20, cipherText);
fail("failed exception test - no ShortBufferException thrown");
}
catch (ShortBufferException e)
{
// ignore
}
}
catch (Exception e)
{
fail("unexpected exception.", e);
}
try
{
byte[] rawDESKey = { (byte)128, (byte)131, (byte)133, (byte)134,
(byte)137, (byte)138, (byte)140, (byte)143 };
SecretKeySpec cipherKey = new SecretKeySpec(rawDESKey, "DES");
Cipher ecipher = Cipher.getInstance("DES/ECB/PKCS5Padding", "BC");
ecipher.init(Cipher.ENCRYPT_MODE, cipherKey);
byte[] cipherText = new byte[0];
try
{
// According specification Method enginedoFinal(byte[] input,
// int inputOffset, int inputLen, byte[] output, int
// outputOffset)
// throws ShortBufferException - if the given output buffer is
// too
// small to hold the result
ecipher.doFinal(new byte[20], 0, 20, cipherText);
fail("failed exception test - no ShortBufferException thrown");
}
catch (ShortBufferException e)
{
// ignore
}
}
catch (Exception e)
{
fail("unexpected exception.", e);
}
try
{
KeyGenerator keyGen = KeyGenerator.getInstance("DES", "BC");
keyGen.init((SecureRandom)null);
// According specification engineGenerateKey() doesn't throw any exceptions.
SecretKey key = keyGen.generateKey();
if (key == null)
{
fail("key is null!");
}
}