-1

I want to compare 2 byte arrays using the Javacard Framework.

The first array is predefined:
static byte[] PasswortA = { 'a', 'b', 'c', 'd' };

The second array will be input via command line:

case (byte) 0x03: ///send D003000004|abcd
    GetPassword = new byte[buffer[ISO7816.OFFSET_LC]];
        Util.arrayCopy(buffer, ISO7816.OFFSET_CDATA, GetPassword, (short) 0, lc);
        Util.arrayCopy(GetPassword, (short) 0, buffer, (short) 0, lc);
        apdu.setOutgoingAndSend((short) 0, (short) lc);

Output:
/send D003000004|abcd
=> D0 03 00 00 04 61 62 63 64 .....abcd
(349416 nsec)
<= 61 62 63 64 90 00 abcd..
Status: No Error

I'm comparing the arrays as per: http://www.win.tue.nl/pinpasjc/docs/apis/jc222/javacard/framework/Util.html

case (byte) 0x05: ///send D005000001
if( (byte) 0 == Util.arrayCompare(PasswortA, (short) 0,  GetPassword, (short) 0, lc ) ){
                buffer[0] = '1';
            } else {
                buffer[0] = '0';            
            }

Output:
/send D005000001
=> D0 05 00 00 01 .....
(291102 nsec)
<= 30 90 00 0..
Status: No Error

Visually both arrays are identical, so I should be getting a 1 written in the buffer (buffer[0] = '1';) as response because when both arrays are identical the function should return a 0 as per the documentation from above.

Anyone know what I'm doing wrong, and why the arrayCompare function reports that the 2 arrays aren't identical?

Thank you!

  • How/where do you declare `GetPassword`? What are the values of `lc` while evaluating `case (byte) 0x03:` and `case (byte) 0x05:`? – Michael Roland Jun 18 '14 at 05:30
  • GetPassword is declared as follows: `private byte[] GetPassword;` `byte[] GetPassword = new byte[buffer[ISO7816.OFFSET_LC]];`. The values of lc are always `short lc = buffer[ISO7816.OFFSET_LC];`. – crappyleft Jun 18 '14 at 08:18
  • @owlstead removing `static` from `PasswortA` didn't work. Thank you though. – crappyleft Jun 18 '14 at 08:34
  • 1
    @user3750185 can you please add your complete code here.Thanks – Anurag Sharma Jun 18 '14 at 09:12
  • 1
    @user3750185 That's not really helpful without knowing where your declare/set those variable in relation to the code that you provided. – Michael Roland Jun 18 '14 at 10:06

1 Answers1

0

The problem was that I tried to compare the arrays in the switch case function and didn't store the input byte array in a separate function. After storing it like this:
private void compareLogin(APDU apdu, byte[] buffer, short lc){ byte a = Util.arrayCompare(PasswortA, (short) 0, GetPassword, (short) 0, lc); buffer[0] = a; apdu.setOutgoingAndSend((short) 0, (short) lc); }
The compare function works as described in the documentation.

Thank for all your help!

  • So, you forgot to send the stored result... don't forget to accept your own answer :) – David Jun 19 '14 at 11:54
  • @David We can't really know as the OP provided an "answer" but did not provide sufficient information (code portions) describing/showing the actual problem. Therefore: useless answer. – Michael Roland Jun 20 '14 at 07:48