This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

InvalidKeySpecException

 
pratik arora
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic