Beispiel #1
0
 internal static extern int SCardTransmit(UInt32 hCard,
                                          [In] ref SCard_IO_Request pioSendPci,
                                          byte[] pbSendBuffer,
                                          UInt32 cbSendLength,
                                          IntPtr pioRecvPci,
                                          [Out] byte[] pbRecvBuffer,
                                          out UInt32 pcbRecvLength
                                          );
Beispiel #2
0
        public byte[] GetBytes()
        {
            uint RecvLength = (uint)(Le + APDUResponse.SW_LENGTH);

            byte[]           ApduBuffer   = null;
            byte[]           ApduResponse = new byte[Le + APDUResponse.SW_LENGTH];
            SCard_IO_Request ioRequest    = new SCard_IO_Request();
            int leIndex = -1;

            // Build the command APDU
            if (Data == null)
            {
                if (P3 > 0)
                {
                    ApduBuffer    = new byte[APDUCommand.APDU_MIN_LENGTH + 2];
                    ApduBuffer[4] = P3;
                    ApduBuffer[5] = (byte)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)Le;
                    leIndex       = 4;
                    //                    }
                }
            }
            else
            {
                ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + 1 + Data.Length];

                for (int nI = 0; nI < Data.Length; nI++)
                {
                    ApduBuffer[APDUCommand.APDU_MIN_LENGTH + 1 + nI] = Data[nI];
                }

                ApduBuffer[APDUCommand.APDU_MIN_LENGTH] = (byte)Data.Length;
            }
            //if (ApduCmd.Data == null)
            //{
            //    ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + 1];
            //    ApduBuffer[4] = 0x18;
            //leIndex = -1;
            //}
            ApduBuffer[0] = Class;
            ApduBuffer[1] = Ins;
            ApduBuffer[2] = P1;
            ApduBuffer[3] = P2;
            return(ApduBuffer);
        }
Beispiel #3
0
        public APDUResponse Test()
        {
            uint RecvLength = 3;

            byte[]           ApduBuffer   = new byte[5];
            byte[]           ApduResponse = new byte[3];
            SCard_IO_Request ioRequest    = new SCard_IO_Request();

            ioRequest.m_dwProtocol  = m_nProtocol;
            ioRequest.m_cbPciLength = 8;
            //if (ApduCmd.Data == null)
            //{
            //    ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + 1];
            //    ApduBuffer[4] = 0x18;
            //leIndex = -1;
            //}
            ApduBuffer[0] = 0xFE;
            ApduBuffer[1] = 0x11;
            ApduBuffer[2] = 0xFE;
            ApduBuffer[3] = 0xFE;
            ApduBuffer[4] = 0x00;

            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;
                throw new Exception(msg);
            }

            byte[] ApduData = new byte[RecvLength];

            for (int nI = 0; nI < RecvLength; nI++)
            {
                ApduData[nI] = ApduResponse[nI];
            }

            return(new APDUResponse(ApduData));
        }
Beispiel #4
0
        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()));
        }