SunJCE TripleDES problem with JDK 1.6
Hello All,
If this has been answered in another post I am sorry I but couldn't find it. Ok, I am using the attched class to do TripleDES encryption and decryption. It works great in JDK1.4 and 1.5, but no go in 1.6. What has changed? I am on a project with right now uses 1.4 and I want to to be able to run on JRE 1.6 later without having to recompile. Why doesn't this work?
I downloaded the SunJCE Unlimited Stringth policy files which fixed the key size issue I was getting, but now it's getting "Given final block not properly padded" when I try and decrypt my data that was originally encryoted using the 1.4 JRE.
import java.io.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
publicclass Crypto
{
Cipher ecipher;
Cipher dcipher;
// 8-byte Salt
byte[] salt ={
(byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
(byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03
};
// Iteration count
int iterationCount = 19;
String Alg ="PBEWithMD5AndTripleDES";
public Crypto(String passPhrase)
{
try{
// Create the key
KeySpec keySpec =new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
SecretKey key = SecretKeyFactory.getInstance(Alg).generateSecret(keySpec);
ecipher = Cipher.getInstance(key.getAlgorithm());
dcipher = Cipher.getInstance(key.getAlgorithm());
// Prepare the parameter to the ciphers
AlgorithmParameterSpec paramSpec =new PBEParameterSpec(salt, iterationCount);
// Create the ciphers
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
}
catch (java.security.InvalidAlgorithmParameterException e)
{
System.err.println(e.getMessage()+"\n"+Functions.getStackTrace(e));
}catch (java.security.spec.InvalidKeySpecException e)
{
System.err.println(e.getMessage()+"\n"+Functions.getStackTrace(e));
}catch (javax.crypto.NoSuchPaddingException e)
{
System.err.println(e.getMessage()+"\n"+Functions.getStackTrace(e));
}catch (java.security.NoSuchAlgorithmException e)
{
System.err.println(e.getMessage()+"\n"+Functions.getStackTrace(e));
}catch (java.security.InvalidKeyException e)
{
System.err.println(e.getMessage()+"\n"+Functions.getStackTrace(e));
}
}
public String encrypt(String str)
{
try
{
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
String result =new sun.misc.BASE64Encoder().encode(enc);
return (result);
}catch (javax.crypto.BadPaddingException e)
{
System.err.println(e.getMessage()+"\n"+Functions.getStackTrace(e));
}catch (IllegalBlockSizeException e)
{
System.err.println(e.getMessage()+"\n"+Functions.getStackTrace(e));
}catch (UnsupportedEncodingException e)
{
System.err.println(e.getMessage()+"\n"+Functions.getStackTrace(e));
}catch (java.io.IOException e)
{
System.err.println(e.getMessage()+"\n"+Functions.getStackTrace(e));
}
returnnull;
}
public String decrypt(String str)
{
try
{
// Decode base64 to get bytes
byte[] dec =new sun.misc.BASE64Decoder().decodeBuffer((str));
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Convert to a string
String result =new String(utf8,"UTF8");
// Decode using utf-8
return result;
}
catch (javax.crypto.BadPaddingException e)
{
System.err.println(e.getMessage()+"\n"+Functions.getStackTrace(e));
}
catch (IllegalBlockSizeException e)
{
System.err.println(e.getMessage()+"\n"+Functions.getStackTrace(e));
}
catch (UnsupportedEncodingException e)
{
System.err.println(e.getMessage()+"\n"+Functions.getStackTrace(e));
}
catch (java.io.IOException e)
{
System.err.println(e.getMessage()+"\n"+Functions.getStackTrace(e));
}
returnnull;
}
}
any help woule be appreciated.
thank you
Tyson OSwald

