This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes InvalidKeySpecException Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply 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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: InvalidKeySpecException
 
Similar Threads
AES Encryption Service
SSHTools, SFTP, Bouncy Castle, JCE, AES128 CBC and other acronyms. Help me put them together.
exception from tripleDES encryption
Enryption with Java
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8