Announce two new product: SmartCard Reader Rockey200 and USB2.0 Extension Cable

ROCKEY 200 is a handy and portable USB Smart Card reader that can perform read/write operations on any ISO 7816-1/2/3/4 smart cards which are compatible with protocol T=0 and T=1.

USB2.0 Extension Cable With high speed USB 2.0 devices such as USB token ,USB Card Reader and so on.

Come to try it now!

The different results with DES algorithm

Algorithm School

Moderator: UNKNwYSHSA

Heather
Posts: 24
Joined: Mon Nov 30, 2015 5:41 am
Points: 117
Contact:

The different results with DES algorithm

Postby Heather » Thu Jan 19, 2017 11:07 pm

I finished a test case. Using DES Cipher, I got different encrypted results with the same APDU.
The length of the input data is random.But When I use javax.crypto to verify, the result is correct.

Here is part of applet code:

Code: Select all

case (byte)0x03:
            short updateLen = apdu.setIncomingAndReceive();
            if (updateLen != buf[ISO7816.OFFSET_LC]) {
                ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
                return;
            }
            try {
                short updatedLen = _cipher.update(buf, ISO7816.OFFSET_CDATA, updateLen, buf, (short) 0);
                apdu.setOutgoingAndSend((short) 0, updatedLen);
            } catch (CryptoException e) {
                Util.setShort(buf, (short) 0, e.getReason());
                apdu.setOutgoingAndSend((short) 0, (short) 2);
                ISOException.throwIt(ISO7816.SW_UNKNOWN);
            }
            break;


The code that use javax.crypto to verify

Code: Select all

            IvParameterSpec ivParameterSpec2 = new IvParameterSpec(iv);
            DESKeySpec desKeySpec2 = new DESKeySpec(key);
            SecretKeyFactory secretKeyFactory2 = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey2 = secretKeyFactory2.generateSecret(desKeySpec2);
            Cipher cipher2 = Cipher.getInstance("DES/CBC/NoPadding");
            cipher2.init(Cipher.ENCRYPT_MODE, secretKey2, ivParameterSpec2);
            byte[] resultData_soft_once = cipher2.update(data);


When using javax.crypto to calculate, the results are always same.

Danieken
Posts: 19
Joined: Thu Sep 17, 2015 10:28 pm
Points: 101
Contact:

Re: The different results with DES algorithm

Postby Danieken » Fri Jan 20, 2017 4:38 am

Could you provide the data?

Heather
Posts: 24
Joined: Mon Nov 30, 2015 5:41 am
Points: 117
Contact:

Re: The different results with DES algorithm

Postby Heather » Fri Jan 20, 2017 4:47 am

Danieken wrote:Could you provide the data?

Among the result data, some blocks' data are same, but the subsequent data is inconsistent.
I used jcop/A22CR and JC30M48CR to do the test.

User avatar
mabel
Posts: 219
Joined: Mon May 18, 2015 3:09 am
Points: 1517
Contact:

Re: The different results with DES algorithm

Postby mabel » Fri Jan 20, 2017 5:00 am

You don't need to check the data. The problem is that input and output used the same buffer.

You can create a temp buffer. input uses apdu buffer and output uses the temp buffer. Then have a try.

Heather
Posts: 24
Joined: Mon Nov 30, 2015 5:41 am
Points: 117
Contact:

Re: The different results with DES algorithm

Postby Heather » Fri Jan 20, 2017 5:34 am

mabel wrote:You don't need to check the data. The problem is that input and output used the same buffer.

You can create a temp buffer. input uses apdu buffer and output uses the temp buffer. Then have a try.


Oops, that's it. I did as you said, it works.

Danieken
Posts: 19
Joined: Thu Sep 17, 2015 10:28 pm
Points: 101
Contact:

Re: The different results with DES algorithm

Postby Danieken » Fri Jan 20, 2017 5:59 am

I just found this description from the specification, which may explain the problem.

When non-block aligned data is presented as input data, no amount of input and output buffer data overlap is allowed; if inBuff==outBuff and outOffset < inOffset+inLength, incorrect output may result.

Heather
Posts: 24
Joined: Mon Nov 30, 2015 5:41 am
Points: 117
Contact:

Re: The different results with DES algorithm

Postby Heather » Fri Jan 20, 2017 6:10 am

Thanks for all the help. It's due to overlap problem, indeed.


Return to “Algorithm School”

Who is online

Users browsing this forum: No registered users and 1 guest

JavaCard OS : Disclaimer