Discussion:
java.security.InvalidKeyException: Wrong key size
(too old to reply)
dmak
2004-11-02 20:43:43 UTC
Permalink
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
2004-11-03 05:05:46 UTC
Permalink
Hi,

Following code generates MD5 message digest of length 128 bits (16 bytes)
Post by dmak
MessageDigest md = MessageDigest.getInstance("MD5");
pwdhash = md.digest(password.getBytes());
Following code requires at least 192 bits (24 bytes) as input
Post by dmak
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
Post by dmak
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);
}
s***@gmail.com
2020-03-09 16:55:13 UTC
Permalink
Post by dmak
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);
}
wtf

Loading...