Beispiel #1
0
        /*
         * SCardControl
         * ------------
         */
        void BtnControlClick(object sender, EventArgs e)
        {
            RCtrlClear();

            DynamicByteProvider p;

            byte[] b;

            p = (DynamicByteProvider)hexBoxCCtrl.ByteProvider;

            b = new byte[p.Length];

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

            CardBuffer cctrl = new CardBuffer(b);

            Settings.HistoryControl.Add(cctrl.AsString());
            hist_ctrl_idx = -1;

            CardBuffer rctrl = channel.Control(cctrl);

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

                b = rctrl.GetBytes();

                p = new DynamicByteProvider(b);

                hexBoxRCtrl.ByteProvider = p;

                if (b.Length > 0)
                {
                    eResultByte.Text = String.Format("{0}", 0 - (int)b[0]);

                    if (b[0] == 0)
                    {
                        eResultByteExplain.Text = "Success";
                    }
                    else
                    {
                        ushort sw = (ushort)(0x6F00 | b[0]);
                        eResultByteExplain.Text = SCARD.CardStatusWordsToString(sw);
                    }
                }

                hexBoxRCtrl.BackColor        = hexBoxCApdu.BackColor;
                eResultByte.BackColor        = eCardAtr.BackColor;
                eResultByteExplain.BackColor = eCardAtr.BackColor;
            }
        }
        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);
        }
Beispiel #3
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";
        }
Beispiel #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";
            }
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
Beispiel #10
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";
            }
        }