Problem when using DES algorithm

I have run the following code in Java 1.4 and it works well and when I execute on Java 1.5 it gives me following error:

Code:

class encryption{

public encryption(){

KeyGenerator keyGen = KeyGenerator.getInstance("DES");

SecretKey desSecretKey = keyGen.generateKey();

InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(KEY_PROPERTIES_FILE);

PropertyResourceBundle propBundle = new PropertyResourceBundle(stream);

stream.close();

KEYGEN_STR = (String)propBundle.handleGetObject("KEY");

}

public Key getKey(){

byte[] bytes = getbytes(KEYGEN_STR);

DESKeySpec pass = new DESKeySpec(bytes);

SecretKeyFactory sKeyFactory = SecretKeyFactory.getInstance("DES-CBC-MD5");

SecretKey sKey = sKeyFactory.generateSecret(pass);

return sKey;

}

public String encrypt(String str){

try

{

// Get secret key

Key key = getKey();

ecipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

ecipher.init(Cipher.ENCRYPT_MODE, key);

byte[] enc = ecipher.doFinal((new String(sourceStr)).getBytes("UTF-8"));

// Encode bytes to base64 to get a string

return new sun.misc.BASE64Encoder().encode(enc);

}

catch(Exception ex)

{

ex.printStackTrace();

}

return null;

}

Error is:java.security.InvalidKeyException: Wrong key size

at javax.crypto.spec.DESKeySpec.<init>(DashoA12275)

at javax.crypto.spec.DESKeySpec.<init>(DashoA12275)

at net.zycomm.siteDetails.source.Encryption.getKey(Encryption.java:82)

at net.zycomm.siteDetails.source.Encryption.encrypt(Encryption.java:123)

at net.zycomm.siteDetails.source.useEncryption.main(useEncryption.java:6)

java.security.NoSuchAlgorithmException: Cannot find any provider supporting DES/ECB/PKCS5Padding

at javax.crypto.Cipher.getInstance(DashoA12275)

at net.zycomm.siteDetails.source.Encryption.encrypt(Encryption.java:125)

at net.zycomm.siteDetails.source.useEncryption.main(useEncryption.java:6)

[2124 byte] By [sruthima] at [2007-10-3 11:51:39]
# 1

Three things

1) I don't see what the lines KeyGenerator keyGen = KeyGenerator.getInstance("DES");

SecretKey desSecretKey = keyGen.generateKey();

in the constructor or doing! As far as I can see they are never used.

2) You don't provide the source code for method getbytes()

3) The key type "DES-CBC-MD5" is a new one on me. I can find this using Google but it does not seem to be a JCE related feature. Can you provide a reference?

sabre150a at 2007-7-15 14:25:45 > top of Java-index,Security,Cryptography...
# 2

Hi,

Let me explain the three things first:

1)The constructor loads the properties file and reads the key.We can also directly read from the file.

2)The implementation of getBytes() is this ..Sorry I did not paste it completely:

private byte[] getbytes(String str)

{

ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();

StringTokenizer sTokenizer = new StringTokenizer(str, "-", false);

while(sTokenizer.hasMoreTokens())

{

try

{

byteOutputStream.write(sTokenizer.nextToken().getBytes());

}

catch(IOException ex)

{

}

}

byteOutputStream.toByteArray();

return byteOutputStream.toByteArray();

}

3)The des-cbc-md5 is an alias for DES in java5.0;

Checkout this-http://java.sun.com/javase/6/docs/technotes/guides/security/jgss/jgss-features.html

BTW this code works very well in JDK 1.4,but Its giving this error in 1.5;

I have another news in this:Do we need to register with any provider explicitly to use this alogorithm.I am confused after going thru web about this.

sruthima at 2007-7-15 14:25:45 > top of Java-index,Security,Cryptography...
# 3

Hey! I got the answer.I need to register with a provider and I downloaded one from "bouncycastle" and registered in my program and it works fine now.But I need one clarification here: I have done it on JDK1.4 without any provider and it works fine and when I do it on jdk1.5 its giving me error that provider is not available.Can any one explain this?

sruthima at 2007-7-15 14:25:45 > top of Java-index,Security,Cryptography...
# 4

Hey! I got the answer.I need to register with a provider and I downloaded one from "bouncycastle" and registered in my program and it works fine now.But I need one clarification here: I have done it on JDK1.4 without any provider and it works fine and when I do it on jdk1.5 its giving me error that provider is not available.Can any one explain this?

sruthima at 2007-7-15 14:25:45 > top of Java-index,Security,Cryptography...