/// <summary> /// transmit apdu с поддержкой ответов 6c/61 /// </summary> /// <param name="ApduCmd"></param> /// <returns></returns> public APDUResponse TransmitA(APDUCommand ApduCmd) { APDUResponse res = Transmit(ApduCmd); if (res.SW1 == 0x6C) { ApduCmd.SetLe(res.SW2); res = Transmit(ApduCmd); } if (res.SW1 == 0x61) { ApduCmd = new APDUCommand(0x00, 0xC0, 0x00, 0x00, null, res.SW2); res = Transmit(ApduCmd); } return(res); }
public APDUResponse Transmit(APDUCommand ApduCmd) { uint RecvLength = (uint)(ApduCmd.Le + APDUResponse.SW_LENGTH); byte[] ApduBuffer = null; byte[] ApduResponse = new byte[ApduCmd.Le + APDUResponse.SW_LENGTH]; SCard_IO_Request ioRequest = new SCard_IO_Request(); ioRequest.m_dwProtocol = m_nProtocol; ioRequest.m_cbPciLength = 8; int leIndex = -1; // Build the command APDU if (ApduCmd.Data == null) { if (ApduCmd.P3 > 0) { ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + 2]; ApduBuffer[4] = ApduCmd.P3; ApduBuffer[5] = (byte)ApduCmd.Le; leIndex = 5; } else { //ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + ((ApduCmd.Le != 0) ? 1 : 0)]; ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + 1]; // if (ApduCmd.Le != 0) // { ApduBuffer[4] = (byte)ApduCmd.Le; leIndex = 4; // } } } else { ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + 1 + ApduCmd.Data.Length]; for (int nI = 0; nI < ApduCmd.Data.Length; nI++) { ApduBuffer[APDUCommand.APDU_MIN_LENGTH + 1 + nI] = ApduCmd.Data[nI]; } ApduBuffer[APDUCommand.APDU_MIN_LENGTH] = (byte)ApduCmd.Data.Length; //ApduBuffer[APDUCommand.APDU_MIN_LENGTH + 2 + ApduCmd.Data.Length - 1] = (byte)ApduCmd.Le; } //if (ApduCmd.Data == null) //{ // ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + 1]; // ApduBuffer[4] = 0x18; //leIndex = -1; //} ApduBuffer[0] = ApduCmd.Class; ApduBuffer[1] = ApduCmd.Ins; ApduBuffer[2] = ApduCmd.P1; ApduBuffer[3] = ApduCmd.P2; m_nLastError = SCardTransmit(m_hCard, ref ioRequest, ApduBuffer, (uint)ApduBuffer.Length, IntPtr.Zero, ApduResponse, out RecvLength); if (m_nLastError != 0) { string msg = "SCardTransmit error: " + m_nLastError + String.Format(" ({0:X})", m_nLastError); throw new Exception(msg); } byte[] ApduData = new byte[RecvLength]; for (int nI = 0; nI < RecvLength; nI++) { ApduData[nI] = ApduResponse[nI]; } return(new APDUResponse(ApduData, ApduCmd.GetApduResponseType())); }