Skip to Main Content

Java Card

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Error while sending an APDU 6d 00

869396Jun 16 2011 — edited Jun 16 2011
Hello,
I'm testing Javacard and i just want to send vie smartcard io an apdu command 0x80 0x07 0x00 0x00.
As an response i should get: a simple string back.
However, it doesen't work. After I selected more or less every ins byt eI'm sending results in an 6d00.
But if I'm sending it via the APDUTOOL I'm getting the correct answer??
Must have something to do with the transformation from in to byte. But I don't see where...

Here is the java code:

public class SCcommunication {


private TerminalFactory factory;
private List<CardTerminal> terminals;
private CardTerminal terminal;
private Card card;
private CardChannel channel;
private ResponseAPDU response;
private boolean protocollType;
private int sendLength;
private int selectLength; //00 A4 04 00 + LE + aid
private byte send[], select[];
private CommandAPDU command;

//generate a factory, get terminal list, and take the first terminal available (get(0))!
public SCcommunication(){
factory= TerminalFactory.getDefault();
try{
terminals = factory.terminals().list();
}
catch (CardException ec){
ec.printStackTrace();
}
terminal = terminals.get(0);
//if protocolltype=flase then protocoll is T0 else T1
protocollType=false;
selectLength=13;
sendLength=133;

select= new byte[selectLength];
select[0]=(byte)(0x00);select[1]=(byte)(0xA4);select[2]=(byte)(0x04);select[3]=(byte)(0x0); select[4]=(byte)(8);
select[5]=(byte)(0xAB); select[6]=(byte)(0xCD); select[7]=(byte)(0xEF); select[8]=(byte)(0xFE); select[9]=(byte)(0xDC); select[10]=(byte)(0x12);
select[11]=(byte)(0x34); select[12]=(byte)(0x56); //select[13]=(byte)(0x01); select[14]=(byte)(0x01);

send= new byte[sendLength];

}

public void switchProtocoll(){
if (!protocollType==true)
protocollType=false;
else
protocollType=true;
}

public void selectApplet(byte selectAPDU[]){

try{

if (protocollType)
card = terminal.connect("T=0");
else
card = terminal.connect("T=1");

channel= card.getBasicChannel();

response=channel.transmit(new CommandAPDU(select));
byte respByte[]=response.getBytes();

System.out.print("The response: ");
System.out.println();

for (int i=0;i<respByte.length;i++){
System.out.print(Integer.toHexString(respByte&0xff)+"||");
}
System.out.println();


// the card is not reseted after disconnect, due to false
card.disconnect(false);
}
catch (CardException ec){
ec.printStackTrace();
}
}


public void sendToCard(){
try{

if (protocollType)
card=terminal.connect("T=0");
else
card=terminal.connect("T=1");

channel= card.getBasicChannel();

//response=channel.transmit(new CommandAPDU(send));


send[0]=(byte)0x80; send[1]=(byte)0x07; send[2]=(byte)(0x00);send[3]=(byte)(0x00); send[4]=(byte)(128);

System.out.println("The response: ");
response=channel.transmit(new CommandAPDU(send));
byte respByte[]=response.getBytes();
for (int i=0;i<respByte.length;i++){
System.out.print(Integer.toHexString(respByte[i]&0xff)+"||");
}

System.out.println();
System.out.println("Done");
// the card is not reseted after disconnect, due to false
card.disconnect(false);

}
catch (CardException ec){
ec.printStackTrace();
}
}


public static void main (String args[]){
SCcommunication sc = new SCcommunication();
sc.switchProtocoll();
sc.selectApplet(sc.select);
sc.sendToCard();
}
}

Here is the corresponending Java Card code:
public void process(APDU apdu) throws ISOException {
// this is the text string which will send back from the ICC to the IFD
final byte[] text = {(byte) 'H', (byte) 'l', (byte) 'l', (byte) ' ',
(byte) 'o', (byte) 'M', (byte) 'n', (byte) 'a', (byte) 'm', (byte) ' ',
(byte) 'e', (byte) 'i', (byte) 's', (byte) 'C',
(byte) 'h', (byte) 'a', (byte) 'r', (byte) 'l', (byte) 'y'};

// this is the install method which will be called once for installation
// and registration of the applet

// this is the method, which will be called from JCRE
// the cmd_apdu variable is used because of performance reasons
byte[] cmd_apdu = apdu.getBuffer();
if (cmd_apdu[0] ==(byte)0x80) { // it is the rigth class

short len_text = (short)text.length; // the len_text variable is used because of performance reasons
apdu.setOutgoing(); // set transmission to outgoing data
apdu.setOutgoingLength((short)len_text); // set the number of bytes to send to the IFD
apdu.sendBytesLong(text, (short)0, (short)len_text);

} // if
else{
ISOException.throwIt(ISO7816.SW_NO_ERROR);
}
} // process



}

Does anybode have any idea?
Best regards
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 14 2011
Added on Jun 16 2011
2 comments
1,349 views