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)

