Hello everybody!

I am just writing my first demo program. But I have a problem with
sending the pin to the card.

The demo program should just give my name after you have entered you
pin.
What is wrong at my program?

It gets an error after it has sent the apdu for the pin :-(

I hope somebody can help me!

Thank you in advance.

Ciao
Kaweh


Program at the JavaCard
-----------------------------
package wallet;

import javacard.framework.*;


public class TestApp extends Applet{


 final static byte Test_CLA   = (byte)0x80;
 final static byte PerformCHV = (byte)0x01;
        final static byte getName    = (byte)0x02;


        final static byte PIN_TRY_LIMIT = (byte) 10;
        final static byte MAX_PIN_SIZE  = (byte) 4;

        private OwnerPIN pin_
                    = new OwnerPIN(PIN_TRY_LIMIT,MAX_PIN_SIZE);


        byte[] name =
{(byte)'K',(byte)'a',(byte)'w',(byte)'e',(byte)'h'};




 public static void install
  (byte[] bArray, short bOffset, byte bLength) {
  new TestApp(bArray, bOffset, bLength);
 }


 private TestApp(byte[] bArray, short bOffset, byte bLength) {

  register();
          byte[] pin = {(byte)'1',(byte)'2',(byte)'3',(byte)'4'};
          pin_.update(pin, (short) 0, (byte)pin.length);

        }

 public void process(APDU apdu){
  byte[] buffer = apdu.getBuffer();

  switch (buffer[ISO7816.OFFSET_INS]) {
                case PerformCHV: performCHV(apdu); return;
  case getName: name(apdu); return;

  default: return;
  }
 }


        private void performCHV(APDU apdu) {

        byte[] buffer = apdu.getBuffer();
        byte lc = buffer[4];
        if (pin_.check(buffer, ISO7816.OFFSET_CDATA, lc) == false)
          ISOException.throwIt(
            ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);

        apdu.setOutgoingAndSend((short)0,(short)0);
      }


 private void name(APDU apdu) {


         byte[] buffer = apdu.getBuffer();
  short le = apdu.setOutgoing();

                if(pin_.isValidated() != true)
                  ISOException.throwIt(
                    ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);

               apdu.setOutgoingLength((short)name.length);
  apdu.sendBytesLong(name,(short)0,(short)name.length);
                return;
 }

}


-----------------------------------

Card access with OCF

package sample;

import java.io.ByteArrayOutputStream;
import opencard.core.service.SmartCard;
import opencard.core.service.CardRequest;
import opencard.opt.util.PassThruCardService;
import opencard.opt.util.PassThruCardServiceFactory;
import opencard.core.terminal.APDU;

import opencard.core.terminal.CommandAPDU;
import opencard.core.terminal.ResponseAPDU;
import opencard.opt.database.DataObject;

import java.io.*;

public class PassThru extends DataObject {


     PassThruCardService ptcs;

     PassThru()
     {
      System.out.println("wait for card! ");
      try
      {
      SmartCard.start();

      CardRequest cr = new CardRequest ();
      cr.setWaitBehavior(CardRequest.ANYCARD);
      SmartCard sc = SmartCard.waitForCard(cr);



    if (sc != null) {

      ptcs = (PassThruCardService)
      sc.getCardService(PassThruCardService.class, true);






    }
  } catch (Exception e) {
    e.printStackTrace();

  }
 }




     public void select() {

     try {
      byte[] data = new String( "TestApp").getBytes();
      APDU(this.ptcs,0x00,0xa4,0x04,0x00,data.length,data);
      }

    catch (Exception e) {
    e.printStackTrace();

    }
    }

    public void pin(char[] spin) {

    String spinstring = new String();
    spinstring = spinstring.copyValueOf(spin);

    try{
      byte[] data = new String(spinstring).getBytes();

      APDU(this.ptcs,0x00,0x01,0x00,0x00,0x04,data);
      }
      catch (Exception e) {
      e.printStackTrace();
      }
    }

    public String getName() {

  String output= new String();
  try{

    byte[] data = null;
    output = APDU(this.ptcs,0x80,0x02,0x00,0x00,0x05,data);
    }
    catch (Exception e) {
    e.printStackTrace();
    }
  return output;
  }

  private static String APDU(PassThruCardService ptcs,int cla, int ins,
int p1, int p2,
                    int length, byte[] data)
  {
    String output= new String();
    try {
        CommandAPDU command;

        if (data!=null)
        {
            byte[] apdu = {(byte)cla,(byte)ins,(byte) p1, (byte)p2,
(byte)length};
            ByteArrayOutputStream apdudata = new ByteArrayOutputStream(
data.length + 6);
            apdudata.write(apdu);
            apdudata.write(data);
            byte[] nothing = {0x00};
            apdudata.write(nothing);
            byte[] apdubyte = apdudata.toByteArray();
            System.out.println();
            for (int i=0; i<(apdubyte.length);i++)
              {
              int byteAsInt = unsignedByteToInt(apdubyte[i]);
              System.out.print(Integer.toHexString(byteAsInt));
              }
            System.out.println();
            command = new CommandAPDU(apdubyte);
        }
        else
        {
            byte[] apdubyte = {(byte)cla,(byte)ins,(byte) p1, (byte)p2,
(byte)length};
            command = new CommandAPDU(apdubyte);
        }

      ResponseAPDU response= ptcs.sendCommandAPDU(command);


      System.out.println(response);



      System.out.println("---------------------");

      if (response.data()!=null) {
        byte [] databuffer = response.data();
        char [] chardata = new char [databuffer.length];
        for (int i=0; i<(databuffer.length);i++)
          chardata[i]=(char)databuffer[i];

       output =output.copyValueOf(chardata);
       System.out.println(output);
      }

    }
    catch (Exception e) {
    e.printStackTrace();
    }
    return output;
  }
  public void shutdown() {
    try{
      SmartCard.shutdown();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
public static int unsignedByteToInt(byte b) {
     return (int) b & 0xFF;}
}


--------------------------------





GUI
-------------
package sample;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;




public class Frame1 extends JFrame {
  JPanel contentPane;
  JTextField jTextField1 = new JTextField();
  JLabel jLabel1 = new JLabel();
  JButton readcard = new JButton();
  JPasswordField jPasswordField1 = new JPasswordField();
  JLabel Pin = new JLabel();

  /**Construct the frame*/
  public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  /**Component initialization*/
  private void jbInit() throws Exception  {
    jTextField1.setEditable(false);
    jTextField1.setBounds(new Rectangle(90, 43, 218, 28));
    jTextField1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jTextField1_actionPerformed(e);
      }
    });

//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your
Icon]")));
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(null);
    this.setSize(new Dimension(400, 300));
    this.setTitle("Frame Title");
    jLabel1.setText("Name:");
    jLabel1.setBounds(new Rectangle(14, 44, 64, 22));
    readcard.setText("read card");
    readcard.setBounds(new Rectangle(81, 223, 233, 39));
    readcard.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        readcard_actionPerformed(e);
      }
    });
    jPasswordField1.setBounds(new Rectangle(161, 174, 83, 26));
    jPasswordField1.addActionListener(new
java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jPasswordField1_actionPerformed(e);
      }
    });
    Pin.setText("Pin");
    Pin.setBounds(new Rectangle(98, 184, 48, 15));
    contentPane.add(jTextField1, null);
    contentPane.add(jLabel1, null);
    contentPane.add(Pin, null);
    contentPane.add(readcard, null);
    contentPane.add(jPasswordField1, null);
  }
  /**Overridden so we can exit when window is closed*/
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

  void readcard_actionPerformed(ActionEvent e) {
  System.out.println("Start!");

  PassThru card = new PassThru();
  card.select();
  card.pin(jPasswordField1.getPassword());
  jTextField1.setText(card.getName());
  card.shutdown();


  }

  void jTextField1_actionPerformed(ActionEvent e) {

  }

  void jPasswordField1_actionPerformed(ActionEvent e) {

  }
}




---
> Visit the OpenCard web site at http://www.opencard.org/ for more
> information on OpenCard---binaries, source code, documents.
> This list is being archived at http://www.opencard.org/archive/opencard/

! To unsubscribe from the [EMAIL PROTECTED] mailing list send an email
! to
!                           [EMAIL PROTECTED]
! containing the word
!                           unsubscribe 
! in the body.

Reply via email to