예제 #1
0
        protected byte[] ReadUID()
        {
            CAPDU capdu = new CAPDU(0xFF, 0xCA, 0x00, 0x00, 0x00);

            Logger.Trace("< " + capdu.AsString(" "));

            RAPDU rapdu = null;

            rapdu = Channel.Transmit(capdu);

            if (rapdu == null)
            {
                Logger.Trace("Error '" + Channel.LastErrorAsString + "' while reading the UID");
                return(null);
            }

            Logger.Trace("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Logger.Trace("Bad status word " + rapdu.SWString + " while reading the UID");
                return(null);
            }

            if (!rapdu.hasData)
            {
                Logger.Trace("Empty response");
                return(null);
            }

            return(rapdu.data.GetBytes());
        }
예제 #2
0
        protected override LLCP_PDU Exchange(LLCP_PDU send_pdu)
        {
            Trace.WriteLine("< " + send_pdu.AsString());

            CAPDU capdu = new CAPDU(0xFF, 0xFE, 0x00, 0x00, send_pdu.GetBytes());

            Trace.WriteLine("< " + capdu.AsString());

            RAPDU rapdu = _channel.Transmit(capdu);

            if (rapdu == null)
            {
                return(null);
            }

            Trace.WriteLine("> " + rapdu.AsString());

            if (rapdu.SW != 0x9000)
            {
                return(null);
            }

            LLCP_PDU recv_pdu = new LLCP_PDU(rapdu.data);

            Trace.WriteLine("> " + recv_pdu.AsString());

            return(recv_pdu);
        }
예제 #3
0
        protected static byte[] ReadBinary(SCardChannel channel, ushort offset, ushort length)
        {
            CAPDU capdu = new CAPDU(0x00, 0xB0, (byte)(offset / 0x0100), (byte)(offset % 0x0100), (byte)length);

            Trace.WriteLine("< " + capdu.AsString(" "));

            RAPDU rapdu = channel.Transmit(capdu);

            if (rapdu == null)
            {
                Trace.WriteLine("ReadBinary " + String.Format("{0:X4}", offset) + "," + String.Format("{0:X2}", (byte)length) + " error " + channel.LastError + " (" + channel.LastErrorAsString + ")");
                return(null);
            }

            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Trace.WriteLine("ReadBinary " + String.Format("{0:X4}", offset) + "," + String.Format("{0:X2}", (byte)length) + " failed " + rapdu.SWString + " (" + SCARD.CardStatusWordsToString(rapdu.SW) + ")");
                return(null);
            }

            if (rapdu.hasData)
            {
                return(rapdu.data.GetBytes());
            }

            return(null);
        }
예제 #4
0
        private void btnWrite_Click(object sender, EventArgs e)
        {
            string dataToWrite = txtData.Text.Trim().PadRight(47) + (char)0x00;
            bool   success     = true;

            txtDataSent.Text = "";

            txtDataSent.Text = txtData.Text + System.Environment.NewLine;
            byte[] content   = Encoding.ASCII.GetBytes(txtData.Text.PadRight(16, ' '));
            byte[] header    = new byte[] { 0xFF, 0xF4, 0x00, (byte)numBlockNumber.Value, 0x10 };          // Write
            byte[] writeApdu = Combine(header, content);
            txtApduSent.Text += BitConverter.ToString(writeApdu).Replace('-', ' ') + System.Environment.NewLine;

            CAPDU capdu = new CAPDU(writeApdu);                                                 // Command sent to the reader
            RAPDU rapdu = channel.Transmit(capdu);                                              // Response sent from card

            if (rapdu == null)
            {
                txtFinalStatus.Text = "Problem while writing";
                success             = false;
            }
            if (rapdu.SW != 0x9000)                                  // Something failed
            {
                txtFinalStatus.Text = "Error:" + String.Format("{0:X}", rapdu.SW) + ": " + SCARD.CardStatusWordsToString(rapdu.SW);
                success             = false;
            }

            if (success)
            {
                txtFinalStatus.Text = "Write with success";
            }
        }
예제 #5
0
        /// <summary>
        /// Callback used when the reader's status change
        /// As this method is called from a thread, you can't directly modify the user interface
        /// </summary>
        /// <param name="readerState"></param>
        /// <param name="cardAtr"></param>
        private void readerStatusChanged(uint readerState, CardBuffer cardAtr)
        {
            // When you are in a thread you can't directly modify the user interface
            if (InvokeRequired)
            {
                this.BeginInvoke(new readerStatusChangedInvoker(readerStatusChanged), readerState, cardAtr);
                return;
            }
            btnRead.Enabled      = false;
            txtAsciiContent.Text = "";
            txtFinalStatus.Text  = "";
            txtHexData.Text      = "";
            lblCardAtr.Text      = "";
            lblStatus.Text       = SCARD.ReaderStatusToString(readerState);

            if (cardAtr != null)
            {
                lblCardAtr.Text = cardAtr.AsString(" ");
                channel         = new SCardChannel(reader);
                if (!channel.Connect())
                {
                    lblStatus.Text = "Error, can't connect to the card";
                    return;
                }
                CAPDU capdu = new CAPDU(0xFF, 0xCA, 0x00, 0x00);    // Command sent to the reader
                RAPDU rapdu = channel.Transmit(capdu);              // Response sent from card
                if (rapdu.SW != 0x9000)                             // Something failed
                {
                    lblStatus.Text = "Get UID APDU failed!";
                    return;
                }
                btnRead.Enabled = true;
            }
        }
예제 #6
0
        public void _0003_EF()
        {
            //Connect to 'OMNIKEY CardMan 3x21 0', share=2, protocol=1
            //Transmit << 00A4040C06FF544143484F
            //Transmit >> 9000
            //Transmit << 00A4020C020520
            //Transmit >> 9000
            //Disconnect, disposition=1
            //AID: ¡FF 54 41 43 48 4F¡ for the Tachograph application
            //MF->DF
            //    ->EF
            //    ->EF
            byte[] command = new byte[] { 0x00, 0xA4, 0x04, 0x0C, 0x06, 0xFF, 0x54, 0x41, 0x43, 0x48, 0x4F };
            CAPDU  capdu   = new CAPDU(command);
            RAPDU  rapdu   = SmartCard.Transmit(capdu);

            byte[] answer = rapdu.GetBytes();
            Assert.AreEqual(new byte[] { 0x90, 0x00 }, answer, "Válasz nem az amit vártam...");

            //Select EF Identification
            command = new byte[] { 0x00, 0xA4, 0x02, 0x0C, 0x02, 0x05, 0x20 };
            capdu   = new CAPDU(command);
            rapdu   = SmartCard.Transmit(capdu);
            answer  = rapdu.GetBytes();
            Assert.AreEqual(new byte[] { 0x90, 0x00 }, answer, "Válasz nem az amit vártam...");
        }
예제 #7
0
        protected static byte[] GetUid(SCardChannel channel)
        {
            CAPDU capdu = new CAPDU(0xFF, 0xCA, 0x00, 0x00, 0x00);

            Trace.WriteLine("< " + capdu.AsString(" "));

            RAPDU rapdu = null;

            rapdu = channel.Transmit(capdu);
            if (rapdu == null)
            {
                Trace.WriteLine("Error '" + channel.LastErrorAsString + "' while getting the card's UID");
                return(null);
            }

            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Trace.WriteLine("Bad status word " + rapdu.SWString + " while getting the card's UID");
                return(null);
            }

            if (!rapdu.hasData)
            {
                Trace.WriteLine("Empty response");
                return(null);
            }

            return(rapdu.data.GetBytes());
        }
        /// <summary>
        /// Write all sectors to the card
        /// </summary>
        /// <param name="address"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        private bool MifareClassicWrite(ushort address, byte[] data, byte[] key)
        {
            if (data == null)
            {
                return(false);
            }

            CAPDU capdu = null;

            if (key == null)
            {
                capdu = new CAPDU(0xFF, 0xF4, (byte)(address / 0x0100), (byte)(address % 0x0100), data);
            }
            else
            {
                // Concat data and key
                byte[] sentData = new byte[data.Length + key.Length];
                System.Buffer.BlockCopy(data, 0, sentData, 0, data.Length);
                System.Buffer.BlockCopy(key, 0, sentData, data.Length, key.Length);
                capdu = new CAPDU(0xFF, 0xF4, (byte)(address / 0x0100), (byte)(address % 0x0100), sentData);
            }

            Logger.Trace("< " + capdu.AsString(" ") + " (writing without specified key)");

            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = Channel.Transmit(capdu);
                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }

                Thread.Sleep(15);
            }

            if (rapdu == null)
            {
                Logger.Trace("Error '" + Channel.LastErrorAsString + "' while writing the card");
                return(false);
            }

            Logger.Trace("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Logger.Trace("Bad status word " + rapdu.SWString + " while writing the card");
                return(false);
            }

            return(true);
        }
        public static bool IsDesfireEV1(SCardChannel channel)
        {
            bool is_desfire_ev1 = false;

            CAPDU capdu = new CAPDU(0x90, 0x60, 0x00, 0x00, 0x00);

            Trace.WriteLine("< " + capdu.AsString(" "));

            RAPDU rapdu = channel.Transmit(capdu);

            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x91AF)
            {
                Trace.WriteLine("Desfire GetVersion function failed");
                return(false);
            }

            if (rapdu.GetByte(3) > 0)
            {
                Trace.WriteLine("This is a Desfire EV1");
                is_desfire_ev1 = true;
            }
            else
            {
                Trace.WriteLine("This is a Desfire EV0");
            }

            capdu = new CAPDU(0x90, 0xAF, 0x00, 0x00, 0x00);

            Trace.WriteLine("< " + capdu.AsString(" "));

            rapdu = channel.Transmit(capdu);
            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x91AF)
            {
                Trace.WriteLine("Desfire GetVersion(2) function failed");
                return(false);
            }

            capdu = new CAPDU(0x90, 0xAF, 0x00, 0x00, 0x00);

            Trace.WriteLine("< " + capdu.AsString(" "));

            rapdu = channel.Transmit(capdu);
            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9100)
            {
                Trace.WriteLine("Desfire GetVersion(3) function failed");
                return(false);
            }

            return(is_desfire_ev1);
        }
예제 #10
0
        public void _0002_Connect_To_DriverCard()
        {
            //00 A4 02 0C 02 00 02
            //SELECT FILE
            byte[] command = new byte[] { 0x00, 0xA4, 0x02, 0x0C, 0x02, 0x00, 02 };
            CAPDU  capdu   = new CAPDU(command);
            RAPDU  rapdu   = SmartCard.Transmit(capdu);

            byte[] answer = rapdu.GetBytes();
            Assert.AreEqual(new byte[] { 0x90, 0x00 }, answer, "Válasz nem az amit vártam...");
        }
예제 #11
0
        private bool WriteBinaryWithKey(ushort address, byte[] data, byte[] key)
        {
            if (data == null)
            {
                return(false);
            }

            if (key == null)
            {
                return(false);
            }

            byte[] dataAndKey = new byte[data.Length + key.Length];
            Array.ConstrainedCopy(data, 0, dataAndKey, 0, data.Length);
            Array.ConstrainedCopy(key, 0, dataAndKey, data.Length, key.Length);

            CAPDU capdu = new CAPDU(0xFF, 0xF4, (byte)(address / 0x0100), (byte)(address % 0x0100), dataAndKey);

            log("< " + capdu.AsString(" ") + " (writing with specified key)");

            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = _channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }

                Thread.Sleep(15);
            }

            if (rapdu == null)
            {
                log("Error '" + _channel.LastErrorAsString + "' while writing the card");
                return(false);
            }

            log("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                log("Bad status word " + rapdu.SWString + " while writing the card");
                return(false);
            }

            return(true);
        }
예제 #12
0
        private bool ReadBinaryWithKey(byte[] key)
        {
            int  address;
            byte length;

            GetSectorData(out address, out length);

            CAPDU capdu = new CAPDU(0xFF, 0xF3, (byte)(address / 0x0100), (byte)(address % 0x0100), key, length);

            Logger.Trace("< " + capdu.AsString(" ") + " (reading with specified key)");

            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = Card.Channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }

                Thread.Sleep(10);
            }

            if (rapdu == null)
            {
                Logger.Trace("Error '" + Card.Channel.LastErrorAsString + "' while reading the card");
                return(false);
            }

            Logger.Trace("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Logger.Trace("Bad status word " + rapdu.SWString + " while reading the card");
                return(false);
            }

            if (!rapdu.hasData)
            {
                Logger.Trace("Empty response");
                return(false);
            }

            Data = rapdu.data.GetBytes();
            PopulateBlocks();
            return(true);
        }
예제 #13
0
        protected static bool WriteBinary(SCardChannel channel, ushort address, byte[] data)
        {
            if (data == null)
            {
                return(false);
            }

            if (data.Length != 4)
            {
                Trace.WriteLine("Type 2 Tag: Write Binary accepts only 4B");
                return(false);
            }

            CAPDU capdu = new CAPDU(0xFF, 0xD6, (byte)(address / 0x0100), (byte)(address % 0x0100), data);

            Trace.WriteLine("< " + capdu.AsString(" "));


            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }

                Thread.Sleep(15);
            }

            if (rapdu == null)
            {
                Trace.WriteLine("Error '" + channel.LastErrorAsString + "' while writing the card");
                return(false);
            }

            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Trace.WriteLine("Bad status word " + rapdu.SWString + " while writing the card");
                return(false);
            }


            return(true);
        }
        protected byte[] MifareClassicRead(ushort blockNumber, byte dataLength = 0, byte[] keyValue = null)
        {
            CAPDU capdu = null;

            if (keyValue == null)               // Reading without key
            {
                capdu = new CAPDU(0xFF, 0xF3, (byte)(blockNumber / 0x0100), (byte)(blockNumber % 0x0100), dataLength);
            }
            else                                                // Reading with key
            {
                capdu = new CAPDU(0xFF, 0xF3, (byte)(blockNumber / 0x0100), (byte)(blockNumber % 0x0100), keyValue, dataLength);
            }

            Logger.Trace("< " + capdu.AsString(" "));
            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = Channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }
            }

            if (rapdu == null)
            {
                Logger.Trace("Error '" + Channel.LastErrorAsString + "' while reading the card");
                return(null);
            }

            Logger.Trace("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Logger.Trace("Bad status word " + rapdu.SWString + " while reading the card");
                return(null);
            }

            if (!rapdu.hasData)
            {
                Logger.Trace("Empty response");
                return(null);
            }
            return(rapdu.data.GetBytes());
        }
예제 #15
0
        /*
         * SCardTransmit
         * ------------
         */
        void BtnTransmitClick(object sender, EventArgs e)
        {
            RApduClear();

            DynamicByteProvider p;

            byte[] b;

            p = (DynamicByteProvider)hexBoxCApdu.ByteProvider;

            b = new byte[p.Length];

            for (int i = 0; i < p.Length; i++)
            {
                b[i] = p.ReadByte(i);
            }

            CAPDU capdu = new CAPDU(b);

            Settings.HistoryTransmit.Add(capdu.AsString());
            hist_apdu_idx = -1;

            RAPDU rapdu = channel.Transmit(capdu);

            if (rapdu == null)
            {
                ShowError();
            }
            else
            {
                ShowSuccess();

                b = rapdu.GetBytes();

                p = new DynamicByteProvider(b);

                hexBoxRApdu.ByteProvider = p;

                eStatusWord.Text        = String.Format("{0:X2} {1:X2}", rapdu.SW1, rapdu.SW2);
                eStatusWordExplain.Text = rapdu.SWString;

                hexBoxRApdu.BackColor        = hexBoxCApdu.BackColor;
                eStatusWord.BackColor        = eCardAtr.BackColor;
                eStatusWordExplain.BackColor = eCardAtr.BackColor;
            }
        }
예제 #16
0
        protected static byte[] ReadBinary(SCardChannel channel, ushort address, byte length)
        {
            CAPDU capdu = new CAPDU(0xFF, 0xB0, (byte)(address / 0x0100), (byte)(address % 0x0100), length);

            Trace.WriteLine("< " + capdu.AsString(" "));

            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }

                Thread.Sleep(10);
            }

            if (rapdu == null)
            {
                Trace.WriteLine("Error '" + channel.LastErrorAsString + "' while reading the card");
                return(null);
            }

            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Trace.WriteLine("Bad status word " + rapdu.SWString + " while reading the card");
                return(null);
            }

            if (!rapdu.hasData)
            {
                Trace.WriteLine("Empty response");
                return(null);
            }

            return(rapdu.data.GetBytes());
        }
예제 #17
0
        private bool WriteBinary(ushort address, byte[] data)
        {
            if (data == null)
            {
                return(false);
            }

            CAPDU capdu = new CAPDU(0xFF, 0xD6, (byte)(address / 0x0100), (byte)(address % 0x0100), data);

            log("< " + capdu.AsString(" "));

            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = _channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }

                Thread.Sleep(15);
            }

            if (rapdu == null)
            {
                log("Error '" + _channel.LastErrorAsString + "' while writing the card");
                return(false);
            }

            log("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                log("Bad status word " + rapdu.SWString + " while writing the card");

                return(false);
            }

            return(true);
        }
예제 #18
0
        private byte[] ReadBinaryWithKey(ushort address, byte length, byte[] key)
        {
            CAPDU capdu = new CAPDU(0xFF, 0xF3, (byte)(address / 0x0100), (byte)(address % 0x0100), key, length);

            log("< " + capdu.AsString(" ") + " (reading with specified key)");

            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = _channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }

                Thread.Sleep(10);
            }

            if (rapdu == null)
            {
                log("Error '" + _channel.LastErrorAsString + "' while reading the card");
                return(null);
            }

            log("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                log("Bad status word " + rapdu.SWString + " while reading the card");
                return(null);
            }

            if (!rapdu.hasData)
            {
                log("Empty response");
                return(null);
            }

            return(rapdu.data.GetBytes());
        }
예제 #19
0
        protected byte[] ReadBinary(ushort P1P2, byte Le = 0)
        {
            CAPDU capdu = new CAPDU(0xFF, 0xB0, (byte)(P1P2 / 0x0100), (byte)(P1P2 % 0x0100), Le);

            Logger.Trace("< " + capdu.AsString(" "));

            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = Channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }
            }

            if (rapdu == null)
            {
                Logger.Trace("Error '" + Channel.LastErrorAsString + "' while reading the card");
                return(null);
            }

            Logger.Trace("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Logger.Trace("Bad status word " + rapdu.SWString + " while reading the card");
                return(null);
            }

            if (!rapdu.hasData)
            {
                Logger.Trace("Empty response");
                return(null);
            }

            return(rapdu.data.GetBytes());
        }
예제 #20
0
        protected bool WriteBinary(ushort P1P1, byte[] Data)
        {
            if (Data == null)
            {
                return(false);
            }

            CAPDU capdu = new CAPDU(0xFF, 0xD6, (byte)(P1P1 / 0x0100), (byte)(P1P1 % 0x0100), Data);

            Logger.Trace("< " + capdu.AsString(" "));

            RAPDU rapdu = null;

            for (int retry = 0; retry < 4; retry++)
            {
                rapdu = Channel.Transmit(capdu);

                if (rapdu == null)
                {
                    break;
                }
                if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B))
                {
                    break;
                }
            }

            if (rapdu == null)
            {
                Logger.Trace("Error '" + Channel.LastErrorAsString + "' while writing the card");
                return(false);
            }

            Logger.Trace("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Logger.Trace("Bad status word " + rapdu.SWString + " while writing the card");
                return(false);
            }

            return(true);
        }
예제 #21
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public byte[] GetUID()
        {
            SCardChannel channel = new SCardChannel(reader);

            byte[] uid = new byte[0];

            if (!channel.Connect())
            {
                return(uid);
            }

            CAPDU command  = NFCCommand.ReadCardUid();
            RAPDU response = channel.Transmit(command);

            if (response.SW != 0x9000)
            {
                return(uid);
            }

            return(response.data.GetBytes());;
        }
예제 #22
0
        private static bool Transmit(byte[] command, out byte[] response)
        {
            CAPDU capdu = new CAPDU(command);
            RAPDU rapdu = m_hCard.Transmit(capdu);

            response = null;
            if (rapdu == null)
            {
                LogManager.DoLogOperation(string.Format("[ERROR] fails to transmit"));
                return(false);
            }
            response = new byte[rapdu.Length];
            Array.Copy(rapdu.Bytes, 0, response, 0, rapdu.Length);
            if (rapdu.SW != 0x9000)
            {
                LogManager.DoLogOperation(string.Format("[ERROR] failed " + SCARD.CardStatusWordsToString(rapdu.SW) + "(" + SCARD.CardStatusWordsToString(rapdu.SW) + ")"));
                return(false);
            }


            return(true);
        }
예제 #23
0
        private void btnWrite_Click(object sender, EventArgs e)
        {
            string dataToWrite = txtData.Text.Trim().PadRight(47) + (char)0x00;
            int    page        = 4;
            bool   success     = true;

            txtDataSent.Text = "";

            foreach (var chunck in ChunksUpto(dataToWrite, 4))
            {
                txtDataSent.Text += chunck + System.Environment.NewLine;
                byte[] content   = Encoding.ASCII.GetBytes(chunck.PadRight(4, ' '));
                byte[] header    = new byte[] { 0xFF, 0xD6, 0x00, (byte)page, 0x04 }; // Update Binary
                byte[] writeApdu = Combine(header, content);
                txtApduSent.Text += BitConverter.ToString(writeApdu).Replace('-', ' ') + System.Environment.NewLine;

                CAPDU capdu = new CAPDU(writeApdu);                         // Command sent to the reader
                RAPDU rapdu = channel.Transmit(capdu);                      // Response sent from card
                if (rapdu == null)
                {
                    txtFinalStatus.Text = "Problem while writing";
                    success             = false;
                    break;
                }
                if (rapdu.SW != 0x9000)                                  // Something failed
                {
                    txtFinalStatus.Text = "Error:" + String.Format("{0:X}", rapdu.SW) + ": " + SCARD.CardStatusWordsToString(rapdu.SW);
                    success             = false;
                    break;
                }
                page++;
            }

            if (success)
            {
                txtFinalStatus.Text = "Write with success";
            }
        }
예제 #24
0
        private void btnRead_Click(object sender, EventArgs e)
        {
            txtAsciiContent.Text = "";
            txtHexData.Text      = "";

            byte[] readApdu = new byte[] { 0xFF, 0xB0, 0x00, 0x04, 0x30 }; // Read binary
            CAPDU  capdu    = new CAPDU(readApdu);                         // Command sent to the reader
            RAPDU  rapdu    = channel.Transmit(capdu);                     // Response sent from card

            if (rapdu == null)
            {
                txtFinalStatus.Text = "Problem while reading";
                return;
            }
            if (rapdu.SW != 0x9000)                                  // Something failed
            {
                txtFinalStatus.Text = "Error:" + String.Format("{0:X}", rapdu.SW) + ": " + SCARD.CardStatusWordsToString(rapdu.SW);
                return;
            }
            txtHexData.Text      = BitConverter.ToString(rapdu.GetBytes()).Replace('-', ' ');
            txtAsciiContent.Text = ByteArrayToString(rapdu.GetBytes());
            txtFinalStatus.Text  = "Read with success";
        }
예제 #25
0
        protected static bool WriteBinary(SCardChannel channel, ushort offset, byte[] buffer)
        {
            CAPDU capdu = new CAPDU(0x00, 0xD6, (byte)(offset / 0x0100), (byte)(offset % 0x0100), buffer);

            Trace.WriteLine("< " + capdu.AsString(" "));

            RAPDU rapdu = channel.Transmit(capdu);

            if (rapdu == null)
            {
                Trace.WriteLine("WriteBinary " + String.Format("{0:X4}", offset) + "," + String.Format("{0:X2}", (byte)buffer.Length) + " error " + channel.LastError + " (" + channel.LastErrorAsString + ")");
                return(false);
            }

            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Trace.WriteLine("WriteBinary " + String.Format("{0:X4}", offset) + "," + String.Format("{0:X2}", (byte)buffer.Length) + " failed " + rapdu.SWString + " (" + SCARD.CardStatusWordsToString(rapdu.SW) + ")");
                return(false);
            }
            return(true);
        }
예제 #26
0
        private static bool SelectNfcApplication(SCardChannel channel)
        {
            CAPDU capdu = new CAPDU(0x00, 0xA4, 0x04, 0x00, (new CardBuffer(NDEF_APPLICATION_ID)).GetBytes(), 0x00);

            Trace.WriteLine("< " + capdu.AsString(" "));

            RAPDU rapdu = channel.Transmit(capdu);

            if (rapdu == null)
            {
                Trace.WriteLine("SelectNfcApplication error " + channel.LastError + " (" + channel.LastErrorAsString + ")");
                return(false);
            }

            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Trace.WriteLine("SelectNfcApplication failed " + rapdu.SWString + " (" + SCARD.CardStatusWordsToString(rapdu.SW) + ")");
                return(false);
            }

            return(true);
        }
예제 #27
0
        private static bool SelectRootApplication(SCardChannel channel)
        {
            CAPDU capdu = new CAPDU(0x00, 0xA4, 0x00, 0x00, "3F00");

            Trace.WriteLine("< " + capdu.AsString(" "));

            RAPDU rapdu = channel.Transmit(capdu);

            if (rapdu == null)
            {
                Trace.WriteLine("SelectRootApplication error " + channel.LastError + " (" + channel.LastErrorAsString + ")");
                return(false);
            }

            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Trace.WriteLine("SelectRootApplication failed " + rapdu.SWString + " (" + SCARD.CardStatusWordsToString(rapdu.SW) + ")");
                return(false);
            }

            return(true);
        }
예제 #28
0
        private static bool SelectFile(SCardChannel channel, ushort file_id)
        {
            CAPDU capdu = new CAPDU(0x00, 0xA4, 0x00, 0x0C, (new CardBuffer(file_id)).GetBytes());

            Trace.WriteLine("< " + capdu.AsString(" "));

            RAPDU rapdu = channel.Transmit(capdu);

            if (rapdu == null)
            {
                Trace.WriteLine("SelectFile " + String.Format("{0:X4}", file_id) + " error " + channel.LastError + " (" + channel.LastErrorAsString + ")");
                return(false);
            }

            Trace.WriteLine("> " + rapdu.AsString(" "));

            if (rapdu.SW != 0x9000)
            {
                Trace.WriteLine("SelectFile " + String.Format("{0:X4}", file_id) + " failed " + rapdu.SWString + " (" + SCARD.CardStatusWordsToString(rapdu.SW) + ")");
                return(false);
            }

            return(true);
        }
예제 #29
0
        /// <summary>
        /// Callback used when the reader's status change
        /// As this method is called from a thread, you can't directly modify the user interface
        /// </summary>
        /// <param name="readerState"></param>
        /// <param name="cardAtr"></param>
        private void readerStatusChanged(uint readerState, CardBuffer cardAtr)
        {
            // When you are in a thread you can't directly modify the user interface
            if (InvokeRequired)
            {
                this.BeginInvoke(new readerStatusChangedInvoker(readerStatusChanged), readerState, cardAtr);
                return;
            }

            lblCardUid.Text  = "";
            lblCardAtr.Text  = "";
            lblCardType.Text = "";
            lblProtocol.Text = "";
            lblStatus.Text   = SCARD.ReaderStatusToString(readerState);

            if (cardAtr != null)
            {
                lblCardAtr.Text = cardAtr.AsString(" ");
                channel         = new SCardChannel(reader);
                if (!channel.Connect())
                {
                    lblCardUid.Text = "Error, can't connect to the card";
                    return;
                }
                CAPDU capdu = new CAPDU(0xFF, 0xCA, 0x00, 0x00);    // Command sent to the reader
                RAPDU rapdu = channel.Transmit(capdu);              // Response sent from card
                if (rapdu.SW != 0x9000)                             // Something failed
                {
                    lblCardUid.Text = "Get UID APDU failed!";
                    return;
                }
                // Display card's UID formated as an hexadecimal string
                byte[] rapduB            = rapdu.data.GetBytes();
                string hexadecimalResult = BitConverter.ToString(rapduB);
                lblCardUid.Text = hexadecimalResult.Replace("-", " ");

                // Card's type
                if (cardAtr.Length >= 19)
                {
                    // Card's protocol
                    int cardProdocol = cardAtr[12];
                    if (protocols.ContainsKey(cardProdocol))
                    {
                        lblProtocol.Text = protocols[cardProdocol];
                    }

                    if (cardAtr[4] == 0x80 && cardAtr[5] == 0x4F && cardAtr[6] == 0x0C && cardAtr[7] == 0xA0 && cardAtr[8] == 0x00 && cardAtr[9] == 0x00 && cardAtr[10] == 0x03 && cardAtr[11] == 0x06)
                    {
                        string pixSs = cardAtr[13].ToString("X2") + cardAtr[14].ToString("X2");
                        lblCardType.Text = "Wired-logic: ";
                        if (cardsNames.ContainsKey(pixSs))
                        {
                            lblCardType.Text += cardsNames[pixSs];
                        }
                    }
                    else
                    {
                        lblCardType.Text = "Unknow card type";
                    }
                }
                else
                {
                    lblCardType.Text = "Smartcard";
                }
            }
        }
예제 #30
0
        /// <summary>
        /// Callback used when the reader's status change
        /// As this method is called from a thread, you can't directly modify the user interface
        /// </summary>
        /// <param name="readerState"></param>
        /// <param name="cardAtr"></param>
        private void readerStatusChanged(uint readerState, CardBuffer cardAtr)
        {
            // When you are in a thread you can't directly modify the user interface
            if (InvokeRequired)
            {
                this.BeginInvoke(new readerStatusChangedInvoker(readerStatusChanged), readerState, cardAtr);

                return;
            }

            lblCardUid.Text  = "";
            lblCardAtr.Text  = "";
            lblCardType.Text = "";
            lblProtocol.Text = "";


            lblMonto.BackColor = Color.FromArgb(240, 240, 240);

            pctColor.BackColor = Color.FromArgb(255, 0, 0);


            //  pctFoto.Image = Properties.Resources.atleta;


            lblStatus.Text = SCARD.ReaderStatusToString(readerState);

            if (cardAtr != null)
            {
                lblCardAtr.Text = cardAtr.AsString(" ");
                channel         = new SCardChannel(reader);


                pctColor.BackColor = Color.FromArgb(121, 174, 235);

                //   pctFoto.Image = Properties.Resources.atleta;
                //    pctFoto.SizeMode = PictureBoxSizeMode.StretchImage;


                lblCedula.Text      = "Cédula: ";
                lblId.Text          = "Identificación: ";
                lblEstatus.Text     = "Cobrado: ";
                lblTotal.Text       = "Total: ";
                lblMonto.Text       = "Retraso: ";
                lblNombreNino.Text  = "";
                lblDeporteNino.Text = "";

                pctFoto.Image = Properties.Resources.atleta;
                pctCara.Image = Properties.Resources.blanco;

                if (!channel.Connect())
                {
                    lblCardUid.Text = "Error, no puede connectar la tarjeta";

                    return;
                }
                CAPDU capdu = new CAPDU(0xFF, 0xCA, 0x00, 0x00);    // Command sent to the reader
                RAPDU rapdu = channel.Transmit(capdu);              // Response sent from card

                if (connectCard())
                {
                    string cardUID = getcardUID();
                    lblCardUid.Text = cardUID;
                    lblUid2.Text    = cardUID;
                    lblST.Text      = "OK";
                    lblST.ForeColor = Color.FromArgb(34, 177, 76);



                    string aux = "Data Source=" + conexion.Fuente + ";Initial Catalog=" + conexion.Catalogo + ";User ID=" + conexion.User + ";Password="******"Select * from datos where brasalete =@zip";
                    try
                    {
                        SqlCommand cmd = new SqlCommand(consulta, conn);
                        cmd.Parameters.AddWithValue("@zip", cardUID);

                        conn.Open();
                        cmd.CommandType = CommandType.Text;
                        SqlDataReader dr = cmd.ExecuteReader();


                        while (dr.Read())
                        {
                            //   pctFoto.Image = Properties.Resources.lala;
                            pctFoto.Image    = new Bitmap(conexion.RutaImg + dr[1].ToString() + ".jpg");
                            pctFoto.SizeMode = PictureBoxSizeMode.StretchImage;

                            lblId.Text      = "Identificación: " + dr[0].ToString();
                            lblCedula.Text  = "Cédula: " + dr[1].ToString();
                            lblTotal.Text   = " Total: B/. " + dr[3].ToString();
                            lblEstatus.Text = "Cobrado:  B/. " + dr[4].ToString() + "  " + "Pendiente: B/. " + dr[5].ToString();

                            lblNombreNino.Text  = dr[0].ToString();
                            lblDeporteNino.Text = dr[2].ToString();

                            string monto = dr[10].ToString();
                            lblMonto.Text = "Retraso: " + monto + "  Meses";
                            switch (monto)
                            {
                            case "1":
                                // celeste
                                lblMonto.ForeColor = Color.FromArgb(121, 174, 235);
                                pctCara.Image      = Properties.Resources.caraFeliz;
                                break;

                            case "2":
                                // amarillo
                                lblMonto.BackColor = Color.FromArgb(255, 247, 0);
                                pctCara.Image      = Properties.Resources.caraTriste;
                                //  pctFoto.
                                break;

                            case "3":
                                // rojo
                                lblMonto.ForeColor = Color.FromArgb(255, 0, 0);
                                pctCara.Image      = Properties.Resources.llorando;
                                break;

                            // rojo
                            default:
                                lblMonto.ForeColor = Color.FromArgb(255, 0, 0);
                                pctCara.Image      = Properties.Resources.blanco;
                                break;
                            }


                            dr.GetString(1);
                            break;
                        }


                        dr.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open)
                        {
                            conn.Close();
                        }
                    }
                }//fin de connect card()
                 //----------------------------------------------------------------



                /*
                 *
                 * if (rapdu.SW != 0x9000)                              // Something failed
                 * {
                 *  lblCardUid.Text = "Get UID APDU fallido!";
                 *  return;
                 * }
                 */
                // Display card's UID formated as an hexadecimal string

                /*
                 * byte[] rapduB = rapdu.data.GetBytes();
                 * string hexadecimalResult = BitConverter.ToString(rapduB);
                 *
                 * lblCardUid.Text = hexadecimalResult.Replace("-", " ");
                 */


                // Card's type
                if (cardAtr.Length >= 19)
                {
                    // Card's protocol
                    int cardProdocol = cardAtr[12];
                    if (protocols.ContainsKey(cardProdocol))
                    {
                        lblProtocol.Text = protocols[cardProdocol];
                    }

                    if (cardAtr[4] == 0x80 && cardAtr[5] == 0x4F && cardAtr[6] == 0x0C && cardAtr[7] == 0xA0 && cardAtr[8] == 0x00 && cardAtr[9] == 0x00 && cardAtr[10] == 0x03 && cardAtr[11] == 0x06)
                    {
                        string pixSs = cardAtr[13].ToString("X2") + cardAtr[14].ToString("X2");
                        lblCardType.Text = "Wired-logic: ";
                        if (cardsNames.ContainsKey(pixSs))
                        {
                            lblCardType.Text += cardsNames[pixSs];
                        }
                    }
                    else
                    {
                        lblCardType.Text = "Desconocido tipo de tarjeta";
                    }
                }
                else
                {
                    lblCardType.Text = "Smartcard";
                }
            }
        }