I'm trying to generate a DES key, but the key length I get back is 8 and I
thought it should be 56. Here's my code:

import java.io.*;
import java.util.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

// assume pwd is equal to something like "javapassword"
MessageDigest messagedigest = MessageDigest.getInstance("SHA");
System.out.println("MessageDigest algorithm made via SHA algorithm...");
String msgDigest = new String(messagedigest.digest());
System.out.println("Message digest complete, length is " + (new
Integer(msgDigest.getBytes().length)) );

DESKeySpec dks = new DESKeySpec(msgDigest.getBytes());
System.out.println("DESKeySpec complete...");
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
System.out.println("SecretKeyFactory complete...");
SecretKey key = skf.generateSecret(dks);
System.out.println("Key generation is complete, length is " + (new
Integer(key.getEncoded().length)) );

What happens is that the SHA hash works (I have to duplicate this in Windows
via the CryptoAPI and I've verified that the two hash values are equal).
However, the key length always ends up being 8, when the documentation
states that key lengths should be 56. What ends up happening is that the
first 8 bytes match between my Windows and Java code, but after that nothing

How do I remedy this in my Java code?


Jason Bock