0

I am having issue while understanding the concept between triple Des algorithm of java and python. In java the key for the encryption is 48 chars long whereas the same key cannot be applied in case of python. I tried with both the suggestions mentioned here but it doesnot seem to work with either way. I cannot decrypt the string encrypted by java in python. The code in java look like:

    import java.security.spec.KeySpec;
    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESedeKeySpec;


    public class SecretKeyEncryptionExample
    {
      private static final String FORMAT = "ISO-8859-1";
      public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
      private KeySpec ks;
      private SecretKeyFactory skf;
      private Cipher cipher;
      SecretKey key;

      public SecretKeyEncryptionExample()
        throws Exception
      {
        String myEncryptionKey = "<48 chars long string>";

        this.ks = new DESedeKeySpec(myEncryptionKey.getBytes("ISO-8859-1"));
        this.skf = SecretKeyFactory.getInstance("DESede");
        this.cipher = Cipher.getInstance("DESede");
        this.key = this.skf.generateSecret(this.ks);
      }

Whereas in python if I use:

from pyDes import *
import hashlib
import base64
key1 = "<48 chars long key>".decode('hex')
data = "<Some encrypted strings>"
k = triple_des(key1, ECB, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
e = k.decrypt(data)
print e

I did it according to the first suggestion given here but it prints nothing. again I tried the second suggestion from the same page which asked to use first 24 chars as key and ignore rest as:

from pyDes import *
import hashlib
import base64
key1 = "<taking only first 24 chars long key>"
data = "<Some encrypted data>"
k = triple_des(key1, ECB, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
e = k.decrypt(data)
print e

But this also gives me no output.. I am confused with all this research. Please suggest which way is the correct one and why I am not getting any output from either of these. Thanks..

Community
  • 1
  • 1
Tirtha
  • 529
  • 2
  • 10
  • 23

2 Answers2

2

In your Python script you are hex-decoding, as far as I can tell. However, in your Java code you are converting the string itself into bytes.

Try something like:

String myEncryptionKey = "<48 chars long string>";
this.ks = new DESedeKeySpec(DatatypeConverter.parseHexBinary(myEncryptionKey));

Also, ensure you fully specify your cipher algorithm to avoid portability problems. So replace:

Cipher.getInstance("DESede");

with something like:

Cipher.getInstance("DESede/ECB/PKCS5Padding");
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • Your answer was very helpful.It solved the issue. tell me one more thing if I use getInstance("DESede") only then which mode will java take by default..? – Tirtha Feb 25 '14 at 11:02
  • @Tirthajust4u Oracle providers tend to default to ECB-mode and PKCS #5 padding. Not sure about other providers. Hence why being explicit is always recommended. – Duncan Jones Feb 25 '14 at 11:10
1

Oracle JDK has default padding of PKCS5. This is reportedly different for Android or even other JDK's.

(Additionally, Java trims the key for anything longer than 24 chars. It does not throw an exception similar to pydes)

As far as I can tell, there is no default IV for the Java Implementation in ECB Mode.

But if you are making a direct port from Java to Python, this might just do the trick:

triple_des(key1, ECB, pad=None, pad_mode=PAD_PKCS5)