aspose file tools*
The moose likes Java in General and the fly likes InvalidKeySpecException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "InvalidKeySpecException" Watch "InvalidKeySpecException" New topic
Author

InvalidKeySpecException

pratik arora
Greenhorn

Joined: Apr 21, 2004
Posts: 1
Hi all,
I am getting the Exception "java.security.spec.InvalidKeySpecException" at "java.security.Key secretKey = factory.generateSecret(keySpec); " when trying to execute the following code where both the parameters are passed in correct format to calculateHMAC method. The KeyFactory used here is com.sun.crypto.provider.DESKeyFactory. Can anybody tell me why this sort of error is thrown?? This code snippet belongs to somebody else whose API's i have to call, so I don't understand the meaning of these 3 lines of code.
SecretKeySpec keySpec = new SecretKeySpec(salt, "HmacSHA1");
SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
java.security.Key secretKey = factory.generateSecret(keySpec);
Can any body explain this to me???

private byte[] calculateHMAC(byte msg[], String user)
{
String METHOD = "calculateHMAC()";
byte salt[] = new byte[0];
try
{
if(msg == null || msg.length < 1)
throw new IllegalArgumentException("byte array either empty or not initialized");
if(user == null)
throw new IllegalArgumentException("no username provided");
byte utf8User[] = user.getBytes("UTF-8");
byte array[] = new byte[msg.length + utf8User.length];
System.arraycopy(msg, 0, array, 0, msg.length);
System.arraycopy(utf8User, 0, array, msg.length, utf8User.length);
salt = retrieveSalt(secureStorageInterface);
if(salt == null || salt.length == 0)
{
PersistLoginMessage.ID0010_SALT_NOT_FOUND.create().log(400, CATEGORY, LOCATION, METHOD);
salt = createNewSalt(secureStorageInterface);
}
SecretKeySpec keySpec = new SecretKeySpec(salt, "HmacSHA1");
SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
java.security.Key secretKey = factory.generateSecret(keySpec);
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(secretKey);
byte abyte0[] = mac.doFinal(array);
return abyte0;
}
catch(Exception e)
{
IDMessage errorMsg = PersistLoginMessage.ID0300_ERROR_CALCULATING_HMAC.create();
CATEGORY.logThrowableT(500, LOCATION, METHOD, errorMsg.formatNonLocalized(), e);
throw new SecurityException(errorMsg.toString());
}
finally
{
for(int n = 0; n < salt.length; n++)
salt[n] = 0;

}
}
Thanks in advance for your help...

Best Regards,
Pratik
 
Don't get me started about those stupid light bulbs.
 
subject: InvalidKeySpecException