Page 1 of 1

Send a random number to host

Posted: Wed Jan 11, 2017 10:56 pm
by Marcat
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();
    apdu.setOutgoing();

    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 ) {
      System.err.println(ex);
    }

    }
   

Re: Send a random number to host

Posted: Thu Jan 12, 2017 3:53 am
by mabel
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 );


Re: Send a random number to host

Posted: Thu Jan 12, 2017 5:39 am
by Tarantino
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.

Example:

Code: Select all


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