Our Online Store have the new products: RFID antenna board. Currently it can work with JC10M24R and JCOP4 card chips.
Compared with normal cards, the antenna board module has a smaller size and fixed holes, which is easy to integrate in the IOT(Internet Of Things) project.

My applet returns 16 bytes of 0 instead of AES encrypted value,why?

Algorithm School

Moderator: UNKNwYSHSA

User avatar
NikerCR
Posts: 10
Joined: Fri Jun 19, 2015 6:49 am
Points :15
Contact:

My applet returns 16 bytes of 0 instead of AES encrypted value,why?

Post by NikerCR » Mon Nov 30, 2015 2:54 am

I am writing a java card applet to encrypt 16 bytes of incoming APDU command data section and return the encrypted value.
My code:

Code: Select all

public class DoAES extends Applet {

    static Cipher myCipher;
    static AESKey myAESKey;
    byte[] cipheredData = JCSystem.makeTransientByteArray((short) 0x10, JCSystem.CLEAR_ON_RESET);

    final static byte SET_KEY = (byte) 0x12;
    final static byte WRITE_TEXT = (byte) 0x04;
    final static byte READ_TEXT = (byte) 0xC0;

    private DoAES() {

        try {
            myCipher = Cipher.getInstance(Cipher.ALG_AES_BLOCK_128_ECB_NOPAD, false);
            myAESKey = (AESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_AES,
                    KeyBuilder.LENGTH_AES_128, false);
        } catch (CryptoException e) {
            ISOException.throwIt(((CryptoException) e).getReason());
        }
    }

    public static void install(byte bArray[], short bOffset, byte bLength)
            throws ISOException {
        (new DoAES()).register();
    }

    public void process(APDU apdu) throws ISOException {
        if (selectingApplet()) {
            return;
        }

        byte[] buffer = apdu.getBuffer();

        if ((buffer[ISO7816.OFFSET_CLA] & 0x00FF) != 0x80) {
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
        }

        try {
            switch (buffer[ISO7816.OFFSET_INS]) {

                case SET_KEY:
                    myAESKey.setKey(buffer, (short) ISO7816.OFFSET_CDATA);
                    myCipher.init(myAESKey, Cipher.MODE_ENCRYPT);
                    break;

                case WRITE_TEXT:
                    myCipher.doFinal(buffer, (short) ISO7816.OFFSET_CDATA, (short) 0x10, cipheredData, (short) 0);
                    break;

                case READ_TEXT:
                    Util.arrayCopyNonAtomic(cipheredData, (short) 0, buffer, (short) 0, (short) 0x10);
                    apdu.setOutgoingAndSend((short) 0, (short) 0x10);
                    break;

                default:
                    ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
            }
        } catch (CryptoException e) {
            ISOException.throwIt(((CryptoException) e).getReason());
        }
    }
}


The sending / received APDUs are as follows
>> 00 A4 04 00 06 81 82 83 84 85 01
<< 90 00

>> 80 12 00 00 10 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
<< 90 00

>> 80 C0 00 00
<< 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...

Why my applet returns 16 bytes of 0 instead of AES encrypted value?
One way.

User avatar
btwtiger
Posts: 28
Joined: Wed Jun 10, 2015 7:22 am
Points :134
Contact:

Re: My applet returns 16 bytes of 0 instead of AES encrypted value,why?

Post by btwtiger » Tue Dec 01, 2015 1:22 am

Hi bro.
please send encryption APDU first.
80 04 00 00 10 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF

Then try to read array by 80 C0 00 00
Onward...

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 32 guests

JavaCard OS : Disclaimer