• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

InvalidKeySpecException

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't touch me. And dont' touch this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic