Read Large data from Smart card With Error tag was lost

断了今生、忘了曾经 提交于 2019-12-10 12:23:43

问题


I want to Read/write large data in smart card via nfc in Android App. according link Send more than 261 bytes to Java Card with Android NFC I can write large apdu block by block.

but when I want to read large data I have problem: I wrote this code for read large data in android app

  public  byte[] ReadCertificate(int keyindex)
{
    byte ReadCertAPDU[]=new byte[5];
    byte Read_Cert_CLA = AppletCLA;
    byte Read_Cert_INS = (byte)0x26;
    byte Read_Cert_P1 = (byte)0x00;
    byte Read_Cert_P2 = (byte)keyindex;
    byte Read_Cert_LE = (byte)0xFF;

    byte[] cert=new byte[2000];
    ReadCertAPDU[0] = Read_Cert_CLA;
    ReadCertAPDU[1] = Read_Cert_INS;
    ReadCertAPDU[2] = Read_Cert_P1;
    ReadCertAPDU[3] = Read_Cert_P2;
    ReadCertAPDU[4] = Read_Cert_LE;

    try {

        byte[] response = transceives(ReadCertAPDU);
        if(response==null)
            return null;

        else {
            if (response[0] == (byte) 0x61)
                response = GetResult2(LogStep, response);
            return response;
        }

    }
    catch (Exception e)
    {
        Log.e( LogStep,e.getMessage()+" IOException on Sent Command: ");
    }
    return  null;

}

I get Error Tag was lost in exception in transceives() method.when I test it in pyapdu tool I don't have problem. and I get data + 61FF that I continue sending 00c00000ff and I get result. but in android app I have problem in my applet I have method :

private void ReadCertificate(APDU apdu)
 {
     byte[] buffer=apdu.getBuffer();
     byte P2=buffer[ISO7816.OFFSET_P2];

     Util.arrayCopyNonAtomic(((byte[])CertificateArray[P2]),(short)0,Certificate, (short) 0,(short)Certificate.length);


    short toSend = (short) Certificate.length; 

    try {
        apdu.setOutgoing();
        apdu.setOutgoingLength(toSend);
        byte counter = 0;
        short senddate;
        while (toSend > 0) {
            if(toSend>255)
                senddate=(short)255;
            else
                senddate=toSend;
            apdu.sendBytesLong(Certificate, (short) (255 * counter), senddate);

            toSend = (short) (toSend - senddate);
            counter = (byte) (counter + 1);
        }
    } 
  catch (Exception e) {

    }
 }

来源:https://stackoverflow.com/questions/40395275/read-large-data-from-smart-card-with-error-tag-was-lost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!