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);
}
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...