I've already used javax.smartcardio
to read serial number from smart card without much effort. However now I'm assigned to create MF on blank card (without which serial number can't be read).I'm creating APDU command for that as per ISO 7816 guidelines,but unable to create proper APDU command because my hex values are getting converted to wrong bytes.
import javax.smartcardio.Card;
import javax.smartcardio.CardChannel;
import javax.smartcardio.CardException;
import javax.smartcardio.CardTerminal;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import javax.smartcardio.TerminalFactory;
class SmartCardAPIs {
public int Create_MF() throws CardException{
//--Variable declaration
int result=0;
Card card=null;
byte[] responseData=null;
ResponseAPDU answer=null;
String SW1=null;
String SW2=null;
int cla, ins, p1, p2;
byte[] data=null;
//---------------------------------------------
//--1--Establish connection with the smart card
TerminalFactory factory = TerminalFactory.getDefault();
List<CardTerminal> terminals = factory.terminals().list();
// Use the first terminal
CardTerminal terminal = terminals.get(0);
// Connect with the card
card = terminal.connect("*");
CardChannel channel = card.getBasicChannel();
//---------------------------------------------
//--2--Create MF
cla=0x00;
ins=0xE0;
p1=0x00;
p2=0x00;
data = new byte[] {
(byte) 0x21,
(byte) 0x62,
(byte) 0x1F,
(byte) 0x82, // **** Getting converted to -126 ****
--
--
--
};
answer = channel.transmit(new CommandAPDU(cla, ins, p1, p2, data));
responseData= answer.getBytes();
if(responseData!=null)
{
if(responseData.length==2)
{
SW1=String.format("%02X ", (responseData[0])).trim();
SW2=String.format("%02X ", (responseData[1])).trim();
}
}
}
}
I've 2 problems
1: data in command APDU is taking a byte which is wrong(marked as *).
2: SW1 and SW2 are returning as 6A 80 which means parameter in data field are incorrect(I guess because of negative value while casting int in hex format to byte,but can not help much as I'm forced to do it).
The partial APDU command which I've placed here is part of full command which I've been supplied ,and the command is 100% OK and tested as I've been successful in creating MF in blank card using smart card tools with the command.I want to do the same in java now.
I assume that problem lies with the way this APDU is being getting created,may be negative value problem(I'm not very expert with java APDU stuffs although I 've created Applet to read serial number from card).
null
. If you declare them final you are sure that they get assigned a value, which is much more "valuable". Methods and variable names start with lowercase, and Java normally puts the{
behind the last line. This makes it much more readable for other Java developers. – Maarten BodewesgetData()
method that never returns null (neither doesgetBytes()
), so theif
becomes completely irrelevant. You might also have found thegetSW()
,getSW1()
andgetSW2()
methods in theResponseData
type. And now the declarations are separate from their uses, which makes it hard to refactor the code. But it seems you are too confident in your own code, so this is the last I will say about it. – Maarten Bodewes