Guest User

Java Card Example - Loopy

a guest
Jul 6th, 2014
3,245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package testjcard;
  8.  
  9. import javacard.framework.APDU;
  10. import javacard.framework.Applet;
  11. import javacard.framework.ISO7816;
  12. import javacard.framework.ISOException;
  13. import javacard.framework.Util;
  14.  
  15. /**
  16.  *
  17.  * @author colin
  18.  */
  19. public class JCardTest extends Applet {
  20.     private byte[] echoBytes;
  21.     private static final short LENGTH_ECHO_BYTES = 32;
  22.  
  23.     /**
  24.      * Installs this applet.
  25.      *
  26.      * @param bArray
  27.      *            the array containing installation parameters
  28.      * @param bOffset
  29.      *            the starting offset in bArray
  30.      * @param bLength
  31.      *            the length in bytes of the parameter data in bArray
  32.      */
  33.     public static void install(byte[] bArray, short bOffset, byte bLength) {
  34.         new JCardTest();
  35.     }
  36.  
  37.     /**
  38.      * Only this class's install method should create the applet object.
  39.      */
  40.     protected JCardTest() {
  41.         echoBytes = new byte[LENGTH_ECHO_BYTES];
  42.         register();
  43.     }
  44.  
  45.    
  46.    
  47.    
  48.     /**
  49.      * Processes an incoming APDU.
  50.      *
  51.      * @see APDU
  52.      * @param apdu
  53.      *            the incoming APDU
  54.      */
  55.     public void process(APDU apdu) {
  56.          byte buffer[] = apdu.getBuffer();
  57.  
  58.         // check SELECT APDU command
  59.         if ((buffer[ISO7816.OFFSET_CLA] == 0) &&
  60.                 (buffer[ISO7816.OFFSET_INS] == (byte) (0xA4))) {
  61.             return;
  62.         }
  63.  
  64.         short bytesRead = apdu.setIncomingAndReceive();
  65.         //short echoOffset = (short) 0;
  66.        
  67.         short outerLoopCounter = 0;
  68.         short innerLoopCounter = 0;
  69.         short iterations = (short) 0;
  70.  
  71.        
  72.         while (bytesRead > 0) {
  73.             //Util.arrayCopyNonAtomic(buffer, ISO7816.OFFSET_CDATA, echoBytes, echoOffset, bytesRead);
  74.             //echoOffset += bytesRead;
  75.             bytesRead = apdu.receiveBytes(ISO7816.OFFSET_CDATA);
  76.         }
  77.        
  78.         for(outerLoopCounter=0;outerLoopCounter<2;outerLoopCounter++){ 
  79.             for(innerLoopCounter=0;innerLoopCounter<1000;innerLoopCounter++){
  80.                 iterations = (short) (iterations + (short)1);
  81.             }
  82.         }      
  83.        
  84.         //Map to bytes
  85.         echoBytes[0] = (byte)(outerLoopCounter >> 8);
  86.         echoBytes[1] = (byte)outerLoopCounter;
  87.         echoBytes[2] = (byte)(innerLoopCounter >> 8);
  88.         echoBytes[3] = (byte)innerLoopCounter;
  89.         echoBytes[4] = (byte)(iterations >> 8);
  90.         echoBytes[5] = (byte)iterations;
  91.  
  92.         apdu.setOutgoing();
  93.         apdu.setOutgoingLength((short) (6 + 5));
  94.  
  95.         // echo header
  96.         apdu.sendBytes((short) 0, (short) 5);
  97.        
  98.         // echo data
  99.         apdu.sendBytesLong(echoBytes, (short) 0, (short) 6);
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment