Class keySpec)
throws InvalidKeySpecException
{
if (keySpec == null)
{
throw new InvalidKeySpecException("keySpec parameter is null");
}
if (key == null)
{
throw new InvalidKeySpecException("key parameter is null");
}
if (SecretKeySpec.class.isAssignableFrom(keySpec))
{
return new SecretKeySpec(key.getEncoded(), algName);
}
else if (DESedeKeySpec.class.isAssignableFrom(keySpec))
{
byte[] bytes = key.getEncoded();
try
{
if (bytes.length == 16)
{
byte[] longKey = new byte[24];
System.arraycopy(bytes, 0, longKey, 0, 16);
System.arraycopy(bytes, 0, longKey, 16, 8);
return new DESedeKeySpec(longKey);
}
else
{
return new DESedeKeySpec(bytes);
}
}
catch (Exception e)
{
throw new InvalidKeySpecException(e.toString());
}
}
throw new InvalidKeySpecException("Invalid KeySpec");
}