Page 1 of 1

Triple DES in java card

Posted: Mon Jan 25, 2016 2:55 am
by user143839285163098
I have finished an applet which encodes some information using 3DES with no padding.

Here is part of the applet code in my card.

Code: Select all

        cipherDES = Cipher.getInstance(Cipher.ALG_DES_CBC_NOPAD, false);
        key = (DESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES3_3KEY, false);
        key.setKey(KEY_BYTES, (short) 0);     

        //Encrypt message
        cipherDES.init(key, Cipher.MODE_ENCRYPT);
        cipherDES.doFinal(tempMessage, startOffset, inLength, tempMessage, startOffset);


The key is 24 bytes length.After I execute the encryption in my card and I obtain the encrypted text.
Then I try to decrypt the encrypted text in the normal Java application but the decrypted text has nothing to do with the original one. Could some one give me any pointers?

Java applet code to decrypt the text is as below:

Code: Select all

byte[] KEY_BYTES = { (byte) 0x38, (byte) 0x12, (byte) 0xA4,
                   (byte) 0x19, (byte) 0xC6, (byte) 0x3B, (byte) 0xE7, (byte) 0x71,
                   (byte) 0x00, (byte) 0x12, (byte) 0x00,(byte) 0x19, (byte) 0x80,
                   (byte) 0x3B, (byte) 0xE7, (byte) 0x71,
                   (byte) 0x39, (byte) 0x12, (byte) 0xA4,
                   (byte) 0x19, (byte) 0xC6, (byte) 0x3C, (byte) 0xE7, (byte) 0x71};
                 
         java.security.spec.AlgorithmParameterSpec alSpec =
            new javax.crypto.spec.IvParameterSpec(new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00});

          try {
               Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
               cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY_BYTES,"DESede"),alSpec);
               
               byte[] decrypted = cipher.doFinal(data);
               
               return new String(decrypted);

Re: Triple DES in java card

Posted: Tue Jan 26, 2016 3:35 am
by mabel
Did you try to use different buffers as input/output for the Cipher.doFinal() method ?

The Cipher.doFinal() method is known for being particularly unpredictable when the input and output buffers overlap.