private static void StartTransaction()
        {
            Array.Clear(SendBuff, 0, 262);
            Array.Clear(RecvBuff, 0, 262);

            //Prepare Buffer
            SendBuff[0] = 0xFF; //start
            SendBuff[2] = 0x20; //memory adres
            SendBuff[3] = 0x0;
            SendBuff[4] = 0x01;
            SendBuff[5] = 0x1;//I2c
            sendLen     = 6;
            RecvLen     = RecvBuff.Length;

            ioRequest.dwProtocol  = Protocol;
            ioRequest.cbPciLength = 8;

            retCode = ModWinsCard.SCardBeginTransaction(hCard);
            if (retCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                throw new Exception(retCode.ToString());
            }

            retCode = ModWinsCard.SCardTransmit(hCard, ref ioRequest, ref SendBuff[0], sendLen, ref ioRequest, ref RecvBuff[0], ref RecvLen);
            if (retCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                throw new Exception(retCode.ToString());
            }
        }
Esempio n. 2
0
        public int SendAPDU()
        {
            int    indx;
            string tmpStr;

            pioSendRequest.dwProtocol  = Aprotocol;
            pioSendRequest.cbPciLength = 8;

            // Display Apdu In
            tmpStr = "";
            for (indx = 0; indx <= SendLen - 1; indx++)
            {
                tmpStr = tmpStr + " " + string.Format("{0:X2}", SendBuff[indx]);
            }
            displayOut(2, 0, tmpStr);
            retCode = ModWinsCard.SCardTransmit(hCard, ref pioSendRequest, ref SendBuff[0], SendLen, ref pioSendRequest, ref RecvBuff[0], ref RecvLen);

            if (retCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                displayOut(1, retCode, "");
                return(retCode);
            }

            tmpStr = "";
            for (indx = 0; indx <= RecvLen - 1; indx++)
            {
                tmpStr = tmpStr + " " + string.Format("{0:X2}", RecvBuff[indx]);
            }

            displayOut(3, 0, tmpStr);
            return(retCode);
        }
Esempio n. 3
0
        public string GetBlockValue(int block, int hCard)
        {
            var value = @"";

            _returnCode = AuthenticateBlock(block, hCard);

            if (_returnCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                throw new ApplicationException(@"Fail to authenticate block", new ApplicationException(ModWinsCard.GetScardErrMsg(_returnCode)));
            }

            _recvBuffLen = _sentBuff[4] + 2;

            _sentBuff = new byte[255];
            _recvBuff = new byte[255];

            _sentBuff = APDUCommands.GetBlockValue(block);

            _returnCode = ModWinsCard.SCardTransmit(hCard, ref _ioSendRequest, ref _sentBuff[0], _sentBuff.Length, ref _ioSendRequest, ref _recvBuff[0], ref _recvBuffLen);

            if (_returnCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                throw new ApplicationException(@"Fail to fetch block value", new ApplicationException(ModWinsCard.GetScardErrMsg(_returnCode)));
            }

            for (int j = 0; j <= _recvBuffLen - 3; j++)
            {
                value += Convert.ToChar(_recvBuff[j]);
            }

            return(value);
        }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="handleFlag">isWriteOrNot</param>
        /// <returns></returns>
        public int SendAPDU(int handleFlag)
        {
            pioSendRequest.dwProtocol  = Aprotocol;
            pioSendRequest.cbPciLength = 8;

            // Display Apdu In
            string tmpStr = string.Empty;

            if (handleFlag == 2)//Update
            {
                for (int idx = 0; idx <= 4; idx++)
                {
                    tmpStr = tmpStr + " " + string.Format("0x{0:X2}({0:D3})", SendBuff[idx]);
                }
            }
            else if (handleFlag == 1)//Read
            {
                for (int idx = 0; idx <= SendLen - 1; idx++)
                {
                    tmpStr = tmpStr + " " + string.Format("0x{0:X2}({0:D3})", SendBuff[idx]);
                }
            }
            else if (handleFlag == 0)//Format
            {
                for (int idx = 0; idx <= 4; idx++)
                {
                    tmpStr = tmpStr + " " + string.Format("0x{0:X2}({0:D3})", SendBuffAll[idx]);
                }
            }

            //WriteLog(2, 0, tmpStr);
            retCode = ModWinsCard.SCardTransmit(hCard, ref pioSendRequest, ref SendBuff[0], SendLen, ref pioSendRequest, ref RecvBuff[0], ref RecvLen);

            if (retCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                WriteLog(1, retCode, "");
                return(retCode);
            }

            tmpStr = "";
            for (int idx = 0; idx < RecvLen; idx++)
            {
                tmpStr = tmpStr + " " + string.Format("0x{0:X2}({0:D3})", RecvBuff[idx]);
            }

            WriteLog(3, 0, tmpStr);

            return(retCode);
        }
Esempio n. 5
0
        private int AuthenticateBlock(int nBlock, int hCard)
        {
            _ioSendRequest = new ModWinsCard.SCARD_IO_REQUEST {
                dwProtocol = ModWinsCard.SCARD_PROTOCOL_UNDEFINED, cbPciLength = 8
            };

            _recvBuffLen = 0x02;

            _sentBuff = new byte[255];
            _recvBuff = new byte[255];

            _sentBuff = APDUCommands.AuthenticateBlock(nBlock);

            return(ModWinsCard.SCardTransmit(hCard, ref _ioSendRequest, ref _sentBuff[0], _sentBuff.Length, ref _ioSendRequest, ref _recvBuff[0], ref _recvBuffLen));
        }
Esempio n. 6
0
        private int Transmit()
        {
            ioRequest.dwProtocol  = Protocol;
            ioRequest.cbPciLength = 8;


            RecvLen = 262;

            // Issue SCardTransmit
            retCode = ModWinsCard.SCardTransmit(hCard, ref ioRequest, ref SendBuff[0], SendLen, ref ioRequest, ref RecvBuff[0], ref RecvLen);

            if (retCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                displayOut(1, retCode, "");
            }
            return(retCode);
        }
Esempio n. 7
0
        private int Transmit()
        {
            string tmpStr;
            int    indx;

            //Display Apdu In
            tmpStr = "";

            for (indx = 0; indx <= SendLen - 1; indx++)
            {
                tmpStr = tmpStr + " " + string.Format("{0:X2}", SendBuff[indx]);
            }

            displayOut(2, 0, tmpStr);

            ioRequest.dwProtocol  = Protocol;
            ioRequest.cbPciLength = 8;

            RecvLen = 262;

            //Issue SCardTransmit
            retCode = ModWinsCard.SCardTransmit(hCard, ref ioRequest, ref SendBuff[0], SendLen, ref ioRequest, ref RecvBuff[0], ref RecvLen);

            if (retCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                displayOut(1, retCode, "");
            }

            else
            {
                tmpStr = "";

                for (indx = 0; indx <= (RecvLen - 1); indx++)
                {
                    tmpStr = tmpStr + string.Format("{0:X2}", RecvBuff[indx]) + " ";
                }

                displayOut(3, 0, tmpStr);
            }
            return(retCode);
        }
        public static void CreateNewCard(Roles r)
        {
            Array.Clear(SendBuff, 0, 262);
            Array.Clear(RecvBuff, 0, 262);

            InitialiseSmartCardReader();
            StartTransaction();

            SendBuff[0] = 0xFF; //start
            SendBuff[1] = 0xD0; //instruction write
            SendBuff[2] = 0x0;  //memory adres
            SendBuff[3] = 0x0;
            SendBuff[4] = 0x01;

            switch (r)
            {
            case Roles.Administrator: SendBuff[5] = 0x66;
                break;

            case Roles.User: SendBuff[5] = 0x69;
                break;

            case Roles.Staff: SendBuff[5] = 0x99;
                break;

            case Roles.Reset: SendBuff[5] = 0x00;
                break;
            }

            sendLen = 6;

            retCode = ModWinsCard.SCardTransmit(hCard, ref ioRequest, ref SendBuff[0], sendLen, ref ioRequest, ref RecvBuff[0], ref RecvLen);
            if (retCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                throw new Exception(retCode.ToString());
            }

            EndTransaction();
        }
Esempio n. 9
0
        public void UpdateBlockValue(int block, string value, int hCard)
        {
            _returnCode = AuthenticateBlock(block, hCard);

            if (_returnCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                throw new ApplicationException(@"Fail to authenticate block", new ApplicationException(ModWinsCard.GetScardErrMsg(_returnCode)));
            }

            _recvBuffLen = 0x02;

            _sentBuff = new byte[255];
            _recvBuff = new byte[255];

            _sentBuff = APDUCommands.SetBlockValue(block, value);

            _returnCode = ModWinsCard.SCardTransmit(hCard, ref _ioSendRequest, ref _sentBuff[0], _sentBuff[4] + 5, ref _ioSendRequest, ref _recvBuff[0], ref _recvBuffLen);

            if (_returnCode != ModWinsCard.SCARD_S_SUCCESS || _recvBuff[0].ToString(@"X2") != @"90")
            {
                throw new ApplicationException(@"Fail to update block value", new ApplicationException(ModWinsCard.GetScardErrMsg(_returnCode)));
            }
        }
Esempio n. 10
0
        private string GetUID()
        {
            string theCardUID = "";

            byte[] receivedUID = new byte[256];
            ModWinsCard.SCARD_IO_REQUEST request = new ModWinsCard.SCARD_IO_REQUEST();
            request.cbPciLength = System.Runtime.InteropServices.Marshal.SizeOf(typeof(ModWinsCard.SCARD_IO_REQUEST));
            request.dwProtocol  = ModWinsCard.SCARD_PROTOCOL_T1;
            byte[] sendBytes = new byte[] { 0xFF, 0xCA, 0x00, 0x00, 0x00 };
            int    outBytes  = receivedUID.Length;

            retCode = ModWinsCard.SCardTransmit(hCard, ref request, ref sendBytes[0], sendBytes.Length, ref request, ref receivedUID[0], ref outBytes);
            if (retCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                displayOut(1, retCode, "");
                System.Environment.Exit(0);
            }
            else
            {
                theCardUID = BitConverter.ToString(receivedUID.Take(4).ToArray()).Replace("-", string.Empty).ToLower();
            }
            return(theCardUID);
        }
        public static Roles ReadCard()
        {
            Array.Clear(SendBuff, 0, 262);
            Array.Clear(RecvBuff, 0, 262);

            InitialiseSmartCardReader();
            StartTransaction();

            SendBuff[0] = 0xFF; //start
            SendBuff[1] = 0xB0; //instruction Read
            SendBuff[2] = 0x0;  //memory adres
            SendBuff[4] = 0x01;
            sendLen     = 6;

            retCode = ModWinsCard.SCardTransmit(hCard, ref ioRequest, ref SendBuff[0], sendLen, ref ioRequest, ref RecvBuff[0], ref RecvLen);
            if (retCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                throw new Exception(retCode.ToString());
            }

            EndTransaction();

            return(GetCardRole());
        }
Esempio n. 12
0
        private int SendAPDUandDisplay(int reqType)
        {
            int    indx;
            string tmpStr;

            pioSendRequest.dwProtocol  = Aprotocol;
            pioSendRequest.cbPciLength = 8;

            // Display Apdu In
            tmpStr = "";
            for (indx = 0; indx <= SendLen - 1; indx++)
            {
                tmpStr = tmpStr + " " + string.Format("{0:X2}", SendBuff[indx]);
            }

            displayOut(2, 0, tmpStr);
            retCode = ModWinsCard.SCardTransmit(hCard, ref pioSendRequest, ref SendBuff[0], SendLen, ref pioSendRequest, ref RecvBuff[0], ref RecvLen);

            if (retCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                displayOut(1, retCode, "");
                return(retCode);
            }

            else
            {
                tmpStr = "";
                switch (reqType)
                {
                case 0:
                    for (indx = (RecvLen - 2); indx <= (RecvLen - 1); indx++)
                    {
                        tmpStr = tmpStr + " " + string.Format("{0:X2}", RecvBuff[indx]);
                    }


                    if ((tmpStr).Trim() != "90 00")
                    {
                        displayOut(4, 0, "Return bytes are not acceptable.");
                    }

                    break;

                case 1:

                    for (indx = (RecvLen - 2); indx <= (RecvLen - 1); indx++)
                    {
                        tmpStr = tmpStr + string.Format("{0:X2}", RecvBuff[indx]);
                    }


                    if (tmpStr.Trim() != "90 00")
                    {
                        tmpStr = tmpStr + " " + string.Format("{0:X2}", RecvBuff[indx]);
                    }

                    else
                    {
                        tmpStr = "ATR : ";
                        for (indx = 0; indx <= (RecvLen - 3); indx++)
                        {
                            tmpStr = tmpStr + " " + string.Format("{0:X2}", RecvBuff[indx]);
                        }
                    }

                    break;

                case 2:

                    for (indx = 0; indx <= (RecvLen - 1); indx++)
                    {
                        tmpStr = tmpStr + " " + string.Format("{0:X2}", RecvBuff[indx]);
                    }

                    break;

                case 3:

                    for (indx = (RecvLen - 2); indx <= (RecvLen - 1); indx++)
                    {
                        tmpStr = tmpStr + " " + string.Format("{0:X2}", RecvBuff[indx]);
                    }


                    if (tmpStr.Trim() == "6A 81")
                    {
                        displayOut(4, 0, "The function is not supported.");
                        return(retCode);
                    }


                    if (tmpStr.Trim() == "63 00")
                    {
                        displayOut(4, 0, "The operation failed.");
                        return(retCode);
                    }


                    validATS = true;
                    break;
                }

                displayOut(3, 0, tmpStr.Trim());
            }
            return(retCode);
        }
Esempio n. 13
0
        private void PerformTransmitAPDU(ref ModWinsCard.APDURec apdu)
        {
            ModWinsCard.SCARD_IO_REQUEST SendRequest;
            ModWinsCard.SCARD_IO_REQUEST RecvRequest;

            SendBuff[0] = apdu.bCLA;
            SendBuff[1] = apdu.bINS;
            SendBuff[2] = apdu.bP1;
            SendBuff[3] = apdu.bP2;
            SendBuff[4] = apdu.bP3;

            if (apdu.IsSend)
            {
                for (indx = 0; indx < apdu.bP3; indx++)
                {
                    SendBuff[5 + indx] = apdu.Data[indx];
                }

                SendBuffLen = 5 + apdu.bP3;
                RecvBuffLen = 2;
            }
            else
            {
                SendBuffLen = 5;
                RecvBuffLen = 2 + apdu.bP3;
            }

            SendRequest.dwProtocol  = Aprotocol;
            SendRequest.cbPciLength = 8;

            RecvRequest.dwProtocol  = Aprotocol;
            RecvRequest.cbPciLength = 8;

            retcode = ModWinsCard.SCardTransmit(hCard, ref SendRequest, ref SendBuff[0], SendBuffLen, ref SendRequest, ref RecvBuff[0], ref RecvBuffLen);
            if (retcode != ModWinsCard.SCARD_S_SUCCESS)
            {
                displayOut(1, retcode, "");
                return;
            }

            sTemp = "";
            // do loop for sendbuffLen
            for (indx = 0; indx < SendBuffLen; indx++)
            {
                sTemp = sTemp + " " + string.Format("{0:X2}", SendBuff[indx]);
            }

            // Display Send Buffer Value
            displayOut(2, 0, sTemp);

            sTemp = "";
            // do loop for RecvbuffLen
            for (indx = 0; indx < RecvBuffLen; indx++)
            {
                sTemp = sTemp + " " + string.Format("{0:X2}", RecvBuff[indx]);
            }

            // Display Receive Buffer Value
            displayOut(3, 0, sTemp);

            if (apdu.IsSend == false)
            {
                for (indx = 0; indx < apdu.bP3 + 2; indx++)
                {
                    apdu.Data[indx] = RecvBuff[indx];
                }
            }
        }
Esempio n. 14
0
        //private static byte[] StringToByteSequence(string sourceString)
        //{
        //    int i = 0, n = 0;
        //    int j = (sourceString.Length) / 2;

        //    byte[] a = new byte[j];
        //    for (i = 0, n = 0; n < j; i += 2, n++)
        //    {
        //        a[n] = Convert.ToByte(sourceString.Substring(i, 2), 16);
        //    }
        //    return a;
        //}

        /// <summary>
        ///
        /// </summary>
        /// <param name="handleFlag">是否是更新操作</param>
        /// <param name="isReadOrWriteAll">是不是全部读取或写入</param>
        /// <returns></returns>
        public int SendAPDU(int handleFlag, bool isReadOrWriteAll, int dataLength)
        {
            int    indx;
            string tmpStr;

            pioSendRequest.dwProtocol  = Aprotocol;
            pioSendRequest.cbPciLength = 8;

            // Display Apdu In
            tmpStr = "";
            if (handleFlag == 2)//更新
            {
                if (isReadOrWriteAll)
                {
                    for (indx = 0; indx <= 4; indx++)
                    {
                        tmpStr = tmpStr + " " + string.Format("{0:X2}", SendBuffAll[indx]);//更新的APDU命令
                    }
                    for (int i = 0; i <= SendLen - 6; i++)
                    {
                        SendBuffAll[i + 5] = bytes[i + dataLength];
                    }
                    SendLen = 21;
                }
                else
                {
                    for (indx = 0; indx <= 4; indx++)
                    {
                        tmpStr = tmpStr + " " + string.Format("{0:X2}", SendBuff[indx]);//更新的APDU命令
                    }
                    for (int i = 0; i <= SendLen - 6; i++)
                    {
                        SendBuff[i + 5] = bytes[i];
                    }
                }
            }
            else if (handleFlag == 1) //读取
            {
                if (isReadOrWriteAll) //全部
                {
                    for (indx = 0; indx <= SendLen - 1; indx++)
                    {
                        tmpStr = tmpStr + " " + string.Format("{0:X2}", SendBuffAll[indx]);
                    }
                }
                else
                {
                    for (indx = 0; indx <= SendLen - 1; indx++)
                    {
                        tmpStr = tmpStr + " " + string.Format("{0:X2}", SendBuff[indx]);
                    }
                }
            }
            else if (handleFlag == 0)//清空
            {
                for (indx = 0; indx <= 4; indx++)
                {
                    tmpStr = tmpStr + " " + string.Format("{0:X2}", SendBuffAll[indx]);//更新的APDU命令
                }
                for (int i = 0; i <= SendLen - 6; i++)
                {
                    SendBuffAll[i + 5] = 0x00;
                }
            }

            displayOut(2, 0, tmpStr);
            if (!isReadOrWriteAll)
            {
                retCode = ModWinsCard.SCardTransmit(hCard, ref pioSendRequest, ref SendBuff[0], SendLen, ref pioSendRequest, ref RecvBuff[0], ref RecvLen);
            }
            else
            {
                retCode = ModWinsCard.SCardTransmit(hCard, ref pioSendRequest, ref SendBuffAll[0], SendLen, ref pioSendRequest, ref RecvBuffAll[0], ref RecvLen);
            }

            if (retCode != ModWinsCard.SCARD_S_SUCCESS)
            {
                displayOut(1, retCode, "");
                return(retCode);
            }

            tmpStr = "";
            if (isReadOrWriteAll)
            {
                for (indx = 0; indx <= RecvLen - 1; indx++)
                {
                    tmpStr = tmpStr + " " + string.Format("{0:X2}", RecvBuffAll[indx]);
                }
            }
            else
            {
                for (indx = 0; indx <= RecvLen - 1; indx++)
                {
                    tmpStr = tmpStr + " " + string.Format("{0:X2}", RecvBuff[indx]);
                }
            }
            displayOut(3, 0, tmpStr);
            return(retCode);
        }