Modifies the data and calls ca.exchangeAPDU to perform the actual send.
The data returned from the smart card is returned by this method within
the exchangeApdu
object
final boolean select = isSelect(sendData);
byte[] dataWithChecksum;
if(select) {
dataWithChecksum = new byte[ sendData.length ];
System.arraycopy(sendData, 0, dataWithChecksum, 0, sendData.length);
}
else {
// set secure bits in CLA
sendData[0] |= APDU_SM_MASK_TYPE4;
dataWithChecksum = new byte[ sendData.length + 2];
System.arraycopy(sendData, 0, dataWithChecksum, 0, sendData.length-1);
dataWithChecksum[dataWithChecksum.length-1] = sendData[sendData.length-1];
int Lc = dataWithChecksum[4];
short csum = 0;
for(short n = 5; n<Lc+5; ++n) {
csum += sendData[n];
}
dataWithChecksum[Lc+5] = (byte) (csum>>8);
dataWithChecksum[Lc+6] = (byte) (csum);
dataWithChecksum[4] += 2; // increase Lc
}
if(debug) {
for(int i=0; i<dataWithChecksum.length;++i) {
System.out.print(Integer.toHexString(dataWithChecksum[i] & 0x00FF) + " ");
}
System.out.println();
}
byte[] receiveDataWithChecksum = ca.exchangeAPDU( dataWithChecksum );
byte[] receiveData;
if(debug) {
for(int i=0; i<receiveDataWithChecksum.length;++i) {
System.out.print(Integer.toHexString(receiveDataWithChecksum[i] & 0x00FF) + " ");
}
System.out.println();
System.out.println();
}
if(!select) // verify the checksum
{
int Le = receiveDataWithChecksum.length - 2; // 2 bytes reserved for SW
if(debug) {
System.out.println("Le=" + Le);
}
short csum1 = 0;
for(short n = 2; n<Le; ++n) {
csum1 += receiveDataWithChecksum[n];
}
short csum2 = (short)
(
(receiveDataWithChecksum[receiveDataWithChecksum.length - 2]<<8)
|
(receiveDataWithChecksum[receiveDataWithChecksum.length - 1] & 0x00FF)
);
if(debug) {
System.out.println("csum1=" + csum1);
System.out.println("csum2=" + csum2);
}
if(csum1 != csum2) throw new java.io.IOException(msg.getString("msg01"));
receiveData = new byte[receiveDataWithChecksum.length-2];
System.arraycopy(receiveDataWithChecksum, 0, receiveData, 0, receiveData.length);
}
else {
receiveData = new byte[receiveDataWithChecksum.length];
System.arraycopy(receiveDataWithChecksum, 0, receiveData, 0, receiveData.length);
}
if(debug) {
for(int i=0; i<receiveData.length;++i) {
System.out.print(Integer.toHexString(receiveData[i] & 0x00FF) + " ");
}
System.out.println();
System.out.println();
}
return receiveData;