// Java imports
import java.io.*;
import java.util.*;

// OCF imports
import opencard.core.service.*;
import opencard.core.terminal.*;
import opencard.opt.util.*;
import opencard.core.util.*;
import opencard.opt.terminal.UserInteraction;
import com.gemplus.opencard.terminal.*;

// Cryptix imports
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.DESKeySpec;


public class TestAPDU {
  
  static PassThruCardService myCardService = null;
  
  public static void main(String[] args) {
    ExtendedCommandAPDU commandToSend = null;
    ResponseAPDU        responseFromCard = null;

    // true if you need the debug info, false otherwise.
    boolean deb = true;
    byte cardResponse[];
    byte idDF[] = {(byte)0x02, (byte)0x00};
    byte idEF[] = {(byte)0x02, (byte)0x01};
    byte writeData[] = new byte[4];
    
    for(int i=0; i<4; i++)
    	writeData[i] = (byte)(i%8);
    
    try {

      // Init OCF
      SmartCard.start();
      System.out.println("\nInsert the card (Timeout 15s)...\n ");

      SmartCard myCard = SmartCard.waitForCard(new CardRequest(15));
      myCardService = (PassThruCardService)myCard.getCardService(PassThruCardService.class, true);
         
      System.out.println("***** Select DF (0x0200) *****");
      commandToSend = new ExtendedCommandAPDU((byte)0x00, (byte)0xA4, (byte)0x01, (byte)0x00, idDF);
      System.out.println("Command: " + commandToSend.toString());
      responseFromCard = myCardService.sendCommandAPDU(commandToSend);
      System.out.println("Response: " + responseFromCard.toString());
            
      System.out.println("***** Select EF (0x0201) *****");
      commandToSend = new ExtendedCommandAPDU((byte)0x00, (byte)0xA4, (byte)0x02, (byte)0x00, idEF);
      System.out.println("Command: " + commandToSend.toString());
      responseFromCard = myCardService.sendCommandAPDU(commandToSend);
      System.out.println("Response: " + responseFromCard.toString());
      
      System.out.println("***** Update Binary *****");
      commandToSend = new ExtendedCommandAPDU((byte)0x00, (byte)0xD6, (byte)0x01, (byte)0xFE, writeData);
      System.out.println("Command: " + commandToSend.toString());
      responseFromCard = myCardService.sendCommandAPDU(commandToSend);
      System.out.println("Response: " + responseFromCard.toString());
      
      System.out.println("***** Read Binary *****");
      commandToSend = new ExtendedCommandAPDU((byte)0x00, (byte)0xB0, (byte)0x01, (byte)0xFE, (byte)0x04);
      System.out.println("Command: " + commandToSend.toString());
      responseFromCard = myCardService.sendCommandAPDU(commandToSend);
      System.out.println("Response: " + responseFromCard.toString());
      
      // Retrieve the card response
      cardResponse = responseFromCard.data();
      
      Util.printBytes("", cardResponse);
          
      System.out.println("*******   End of Test *********");

      SmartCard.shutdown();
    } catch(Exception e) {
      e.printStackTrace(System.out);
    }
    
  } // main()
    
} // class TestAPDU

