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.

Random Data Genaration

JavaCard Applet Development Related Questions and Answers.
shibulijack
Posts: 8
Joined: Tue Dec 15, 2015 2:09 am
Points :53
Contact:

Random Data Genaration

Post by shibulijack » Thu May 11, 2017 11:00 pm

I am trying to generate a 32 bytes random number. However, I have never succeed yet.I always got the same data back. :(

It seems that my code doesn't work at all, I mean,it doesn't generate random as I expected.
Where did I do wrongly? Any help.

Code: Select all

RandomData rd = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);

...

byte[] buffer = apdu.getBuffer();
short bytesRead = apdu.setIncomingAndReceive(); //bytesRead = 32byte
         
rd.setSeed(buffer, (short)ISO7816.OFFSET_CDATA, bytesRead);
rd.generateData(buffer, (short)ISO7816.OFFSET_CDATA, bytesRead);
         
apdu.setOutgoing();
apdu.setOutgoingLength(bytesRead);
apdu.sendBytesLong(buffer, (short)ISO7816.OFFSET_CDATA, bytesRead);


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

Re: Random Data Genaration

Post by mabel » Fri May 12, 2017 5:34 am

I didn't find out any question in your code.

You can try to use this code to check if this can work.

Code: Select all

private byte[] rnd;
private RandomData randomData;
static final short RND_LENGTH = 32;

public LicenseApplet() {
                randomData = RandomData.getInstance(RandomData.ALG_PSEUDO_RANDOM);
                rnd = JCSystem.makeTransientByteArray(RND_LENGTH, JCSystem.CLEAR_ON_RESET);
}
private short processGetChallenge(APDU apdu, boolean protectedApdu, short le) {
                //There're more things but I cut them out
             byte[] buffer = apdu.getBuffer();

             randomData.generateData(rnd, (short) 0, le);
             rndLength = le;
             Util.arrayCopyNonAtomic(rnd, (short) 0, buffer, bufferOffset, le);
     
             return le;
    }

shibulijack
Posts: 8
Joined: Tue Dec 15, 2015 2:09 am
Points :53
Contact:

Re: Random Data Genaration

Post by shibulijack » Fri May 12, 2017 6:27 am

Does this code work on your card? Unfortunately it did not solve the problem.

tay00000
Posts: 161
Joined: Tue Sep 27, 2016 10:58 am
Points :2324
Contact:

Re: Random Data Genaration

Post by tay00000 » Sun May 14, 2017 5:39 am

I have no problem running your code fragment.

Here's my process() method.

Code: Select all

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

        byte[] buffer = apdu.getBuffer();
        short bytesRead = apdu.setIncomingAndReceive();

        if (buffer[ISO7816.OFFSET_CLA] == (byte) 0x00 && buffer[ISO7816.OFFSET_INS] == (byte) 0x01) {                       

            rd.setSeed(buffer, (short) ISO7816.OFFSET_CDATA, bytesRead);
            rd.generateData(buffer, (short) ISO7816.OFFSET_CDATA, bytesRead);

            apdu.setOutgoing();
            apdu.setOutgoingLength(bytesRead);
            apdu.sendBytesLong(buffer, (short) ISO7816.OFFSET_CDATA, bytesRead);
        }
    }


Here's my APDU command transactions on my test card

Code: Select all

>>> 00 01 00 00 0C 00 00 00 00 00 00 00 00 00 00 00 00
<<< 6C 89 24 6D 45 20 0C 79 1C 8B 03 46 90 00

>>> 00 01 00 00 20 B2 0A 60 28 E4 18 93 D7 71 19 18 B2 63 83 63 3B EC 17 2F 37 C3 15 6C C0 36 5C D2 B6 44 2D E3 D3
<<< 45 5B 6B AD A8 77 3E BA 47 90 D5 EF 14 7B 0E 23 A0 FA E1 B6 1E E3 9D 7C 28 62 0E 5E C0 C4 7E DD 90 00


You try to use the process() method as per what I used above and see if it works. Also I assume your card has ALG_SECURE_RANDOM.

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 46 guests

JavaCard OS : Disclaimer