Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

java.security.InvalidKeyException: Wrong key size

2,218 views
Skip to first unread message

dmak

unread,
Nov 2, 2004, 3:43:43 PM11/2/04
to
Hi,

I am trying to encrypt a string using MD5. Following is the piece of code.
I get the error "java.security.InvalidKeyException: Wrong key size" at the
following line

DESedeKeySpec desedeKeySpec = new DESedeKeySpec(pwdhash);

Any help is appreciated . Thank U

Code:-
public static void main (String args[]) {
arg = args[0];

try {
password = "secretpassword1!";
MessageDigest md = MessageDigest.getInstance("MD5");
pwdhash = md.digest(password.getBytes());
md = null;
DESedeKeySpec desedeKeySpec = new DESedeKeySpec(pwdhash);
System.out.println("Work");
SecretKeyFactory keyFactory =
SecretKeyFactory.getInstance("DESede");
Key desKey = (Key) keyFactory.generateSecret(desedeKeySpec);
desCipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
desCipher.init(Cipher.ENCRYPT_MODE, desKey);
}
catch(Exception e) {
System.out.println("e "+e);
}

String encr = encrypt(arg);
}


Yasir Khan

unread,
Nov 3, 2004, 12:05:46 AM11/3/04
to
Hi,

Following code generates MD5 message digest of length 128 bits (16 bytes)

> MessageDigest md = MessageDigest.getInstance("MD5");
> pwdhash = md.digest(password.getBytes());

Following code requires at least 192 bits (24 bytes) as input

> DESedeKeySpec desedeKeySpec = new DESedeKeySpec(pwdhash);

As you can see in your code, you are inputting 16 bytes data instead of 24
bytes and it is the actual cause of mentioned problem.

I hope this will help.

Regards,
Yasir

"dmak" <dinak...@nospam.yahoo.com> wrote in message
news:9d1b8fe8af350eb5...@localhost.talkaboutprogramming.com...

Message has been deleted
0 new messages