Posted By:
Eugene_Kuleshov
Posted On:
Thursday, April 17, 2003 06:05 AM
There is nothing wron with DESKeySpec, so you can use that. You can also use the following code as suggested in
Java Almanac.
SecretKey key = new SecretKeySpec(keyBytes, "DES");
If you needed to use a passphrase for your DES encryption, then probably you should consider the following code (also from Java Almanac).
byte[] salt = { (byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32, (byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03};
int iterationCount = 19;
KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
SecretKey key = SecretKeyFactory.getInstance( "PBEWithMD5AndDES").generateSecret(keySpec);
ecipher = Cipher.getInstance(key.getAlgorithm());
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
byte[] utf8 = str.getBytes("UTF8");
byte[] enc = ecipher.doFinal(utf8);
return new sun.misc.BASE64Encoder().encode(enc);