Greetings all, we are proud to announce our store new smartcard products to you all. For more details, please visit our online store.

Mifare Classic 1K Card / Mifare Classic 1K Keyfob

125kHz ID Card / 125kHz ID Card Keyfob

MIFARE DESFire EV1 2K(D21) / MIFARE DESFire EV1 4K(D41) / MIFARE DESFire EV1 8K(D81)

Send a random number to host

JavaCard Applet Development Related Questions and Answers.
Posts: 3
Joined: Wed Dec 21, 2016 11:50 pm
Points: 44

Send a random number to host

Postby Marcat » Wed Jan 11, 2017 10:56 pm

I want to send a random number to a host. Now my problem is how to create APDU answer. anyone can help?

Code: Select all

    private void sendChallenge(APDU apdu){

    byte[] apduBuffer = apdu.getBuffer();

    try {
      SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
      String randomNum = new Integer( prng.nextInt() ).toString();
      byte[] randomBytes = randomNum.getBytes();
   /* I want to send the number here */

    catch ( NoSuchAlgorithmException ex ) {


User avatar
Posts: 176
Joined: Mon May 18, 2015 3:09 am
Points: 1075

Re: Send a random number to host

Postby mabel » Thu Jan 12, 2017 3:53 am

Sample code for you

Code: Select all

   short le = apdu.setOutgoing();
   if (le < (short)2) ISOException.throwIt( ISO7816.SW_WRONG_LENGTH );
   apdu.setOutgoingLength( (short)3 );

   buffer[0] = (byte)1; buffer[1] = (byte)2; buffer[3] = (byte)3;
   apdu.sendBytes ( (short)0 , (short)3 );

Posts: 85
Joined: Wed Aug 19, 2015 1:56 am
Points: 348

Re: Send a random number to host

Postby Tarantino » Thu Jan 12, 2017 5:39 am

It seems that the code you posted is J2SE-code, not JavaCard code.
For Java Card, you can only use the classes defined in JavaCard 2.1 specification.


Code: Select all

    byte[] apduBuffer = apdu.getBuffer();
    RandomData random = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM); 
    random.generateData(randomArray,(short)0, (short)32);
    apdu.setOutgoingAndSend((short)0, (short)32);

The greatest glory in living lies not in never falling, but in rising every time we fall.--Nelson Mandela

Return to “Questions & Answers”

Who is online

Users browsing this forum: No registered users and 2 guests

JavaCard OS : Disclaimer