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

[7961 byte] By [tyson.oswalda] at [2007-11-26 19:26:29]
# 1

This problem came up in this forum about 2 weeks ago! There is a bug in the PBE key getkey.getAlgorithm() method in 1.4 so that instead of

key.getAlgorithm()

returning PBEWithMD5AndTripleDES it is returning PBEWithMD5AndDES !

Unfortunately for you, under 1.4 your codeSecretKey key = SecretKeyFactory.getInstance(Alg).generateSecret(keySpec);

ecipher = Cipher.getInstance(key.getAlgorithm());

dcipher = Cipher.getInstance(key.getAlgorithm());

uses DES instead of Tripple DES

This bug has been fixed in 1.6 so of course anything you encrypted in 1.4 will not decrypt in 1.6. It also means that you have only been protecting your data with a 56 bit key!

I can't remember what happens under 1.5! Yes I can! It also has the BUG.

Message was edited by:

sabre150

sabre150a at 2007-7-9 21:51:51 > top of Java-index,Security,Cryptography...
# 2
Oh that's wonderful! Good thing it hasn't moved out of development yet. What would I use in 1.4 to get TripleDES?
tyson.oswalda at 2007-7-9 21:51:51 > top of Java-index,Security,Cryptography...
# 3

> Oh that's wonderful! Good thing it hasn't moved out

> of development yet.

You lucky B!

> What would I use in 1.4 to get

> TripleDES?

If you explicitly use "PBEWithMD5AndTripleDES" then it works OK. It is just the method call that returns the wrong string!

P.S. I found the other thread - http://forum.java.sun.com/thread.jspa?forumID=9&threadID=5129170

Message was edited by:

sabre150

sabre150a at 2007-7-9 21:51:51 > top of Java-index,Security,Cryptography...
# 4

> > Oh that's wonderful! Good thing it hasn't moved

> out

> > of development yet.

>

> You lucky B!

>

> > What would I use in 1.4 to get

> > TripleDES?

>

> If you explicitly use "PBEWithMD5AndTripleDES" then

> it works OK. It is just the method call that returns

> the wrong string!

I'm not sure what you are stating here, I am explicitly using PBEWithMD5AndTripleDES.

>

> P.S. I found the other thread -

> http://forum.java.sun.com/thread.jspa?forumID=9&thread

> ID=5129170

>

> Message was edited by:

> sabre150

tyson.oswalda at 2007-7-9 21:51:51 > top of Java-index,Security,Cryptography...
# 5

> > If you explicitly use "PBEWithMD5AndTripleDES"

> then

> > it works OK. It is just the method call that

> returns

> > the wrong string!

>

> I'm not sure what you are stating here, I am

> explicitly using PBEWithMD5AndTripleDES.

No! You are usingecipher = Cipher.getInstance(key.getAlgorithm());

and you should useecipher = Cipher.getInstance("PBEWithMD5AndTripleDES");

or use the 'Alg' constant you have setup.

The problem is that key.getAlgorithm() should return "PBEWithMD5AndTripleDES" but will return "PBEWithMD5AndDES".

Message was edited by:

sabre150

sabre150a at 2007-7-9 21:51:51 > top of Java-index,Security,Cryptography...
# 6

> > > If you explicitly use "PBEWithMD5AndTripleDES"

> > then

> > > it works OK. It is just the method call that

> > returns

> > > the wrong string!

> >

> > I'm not sure what you are stating here, I am

> > explicitly using PBEWithMD5AndTripleDES.

>

> No! You are using> ecipher =

> Cipher.getInstance(key.getAlgorithm());

> and you should use> ecipher =

> Cipher.getInstance("PBEWithMD5AndTripleDES");

> or use the 'Alg' constant you have setup.

Doh!, I see. It works now, thanks!

>

> The problem is that key.getAlgorithm() should return

> "PBEWithMD5AndTripleDES" but will return

> "PBEWithMD5AndDES".

>

> Message was edited by:

> sabre150

tyson.oswalda at 2007-7-9 21:51:51 > top of Java-index,Security,Cryptography...
# 7

Does TripleDES only work if you have the unlimited strength jurisdiction policy files installed?

The reason I ask is because i get illegal key size if they are not installed. And, I am curious is setting the key length in PBEKeySpec(char[], byte[], int,int) change the key size?

thanks,

Tyson

tyson.oswalda at 2007-7-9 21:51:51 > top of Java-index,Security,Cryptography...
# 8
> Does TripleDES only work if you have the unlimited> strength jurisdiction policy files installed? Yes.
sabre150a at 2007-7-9 21:51:51 > top of Java-index,Security,Cryptography...