简体   繁体   中英

Decode/Decrypt APDU responses Java Card

I use smartcardio library to communicate with smart card by Java. I get responses from card then I want to decode the response to text and it's kinda complicated.

Example :

  1. I select the file on the card.
  2. Get Binary response from the file.
  3. Read response to console.

The response looks :

ResponseAPDU: 145 bytes, SW=9000

Hex : 13E013A801C800001AB4138E03931BC6142B1C3214371C5114531C5B04FA24FB0026001252AE42800554003E200005621D670012002252AF9400055502C518001083188700B2195501C91BEB04601C6904FC1D660022003652B0E580055601AF180010561875108F189200941B9213CA1BD613D81BDB143E1C4314481C4A144F04511CA315261D2D656B0036001A529000

Binary:  10011111000000001001110101000000000011100100000000000000000000001101010110100000100111000111000000011100100110001101111000110000101000010101100011100001100100001010000110111000111000101000100010100010100110001110001011011000001001111101000100100111110110000000000100110000000000001001001010010101011100100001010000000000001010101010000000000001111100010000000000000000001010110001000011101011001110000000000010010000000000010001001010010101011111001010000000000000001010101010100000010110001010001100000000000000100001000001100011000100001110000000010110010000110010101010100000001110010010001101111101011000001000110000000011100011010010000010011111100000111010110011000000000001000100000000000110110010100101011000011100101100000000000010101010110000000011010111100011000000000000001000001010110000110000111010100010000100011110001100010010010000000001001010000011011100100100001001111001010000110111101011000010011110110000001101111011011000101000011111000011100010000110001010001001000000111000100101000010100010011110000010001010001000111001010001100010101001001100001110100101101011001010110101100000000001101100000000000011010010100101001000000000000

After execute : 
System.out.println("Response : " + new String(r.getData()));

new String(r.getBytes(), 0, r.getBytes().length, "UTF8")); --> give me almost the same result.

I get : 
���  ����+27QS[�$� & R�B�T >  bg  "R�� U� �� �U��`i�f " 6R���V� Vu�� ������>CHJOQ�&-ek 6 R� 

Is there different way to decode responses from the card to text? It's great that I get responses from card, but it's useless if I even don't know what I get. Most of solutions I found about decoding apdu responses are simple like my above, but this solution isn't enough.

Also tried decode the response by checking all charsets:

        SortedMap<String,Charset> charsets = Charset.availableCharsets();
        Iterator i = charsets.entrySet().iterator();

        while (i.hasNext())
        {
            Map.Entry m = (Map.Entry)i.next();
            String key = (String)m.getKey();

            System.out.println("Response charset " + key + " : " + new String(r.getBytes(), 0, r.getBytes().length, key));
        }

Read Binary may easily (ie more often than not) return non-text data. This depends on the specification of the application. Without additional information, what specification the card conforms to, and which file it is, our guess is not better than yours. Note, that often a structured data-format called BER-TLV is used by smartcards. As a start, try to get hold of ISO 7816, part 4 for an introduction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM