Пример #1
0
        public byte[] RsReset()
        {
            try
            {
                RespApdu respApdu = mReader.Exchange("A4 0A 00 00 08"); // Get Random Number

                //Console.WriteLine("Random  = 0x" + HexFormatting.ToHexString(respApdu.Data, true));
                if (respApdu != null)
                {
                    baRandom  = respApdu.Data;
                    strRandom = HexFormatting.ToHexString(baRandom, true);
                }
                else
                {
                    baRandom = new byte[8];
                    Array.Clear(baRandom, 0, 8);
                }

                return(baRandom);
            }
            catch (WinSCardException ex)
            {
                //Console.WriteLine(ex.WinSCardFunctionName + " Error 0x" +
                //                   ex.Status.ToString("X08") + ": " + ex.Message);
                throw ex;
            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex.Message);
                throw ex;
            }
        }
Пример #2
0
        public static string cardProtocol()
        {
            WinSCard scard = new WinSCard();

            try
            {
                scard.EstablishContext();
                scard.ListReaders();
                string readerName = scard.ReaderNames[1];
                scard.WaitForCardPresent(readerName);


                scard.Connect(readerName);
                byte[] cmdApdu    = { 0xFF, 0xCA, 0x00, 0x00, 00 }; // Get Card UID ...
                byte[] respApdu   = new byte[10];
                int    respLength = respApdu.Length;

                scard.Transmit(cmdApdu, cmdApdu.Length, respApdu, ref respLength);

                //find a better place for this
                App.UID = HexFormatting.ToHexString(respApdu, true);

                //he wanted some kinda beeping sound when someone swipes their card
                System.Media.SystemSounds.Beep.Play();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            return(App.UID);
        }
Пример #3
0
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                RespApdu respApdu = mPcscReader.Exchange(txtAPDU.Text);

                ml.NewLine();
                ml.AddLine("Send: " + txtAPDU.Text);
                if (respApdu.Data != null && respApdu.Data.Length > 0)
                {
                    //ml.AddLine("Resp: " + HexFormatting.ToHexString(respApdu.Data, true));
                    ml.AddLine(String.Format("Resp({0}B): {1}", respApdu.Data.Length,
                                             HexFormatting.ToHexString(respApdu.Data, true)));
                }
                else
                {
                    ml.AddLine("Resp: (None)");
                }

                ml.AddLine("Status: " + respApdu.SW1SW2.Value.ToString("X4"));

                txtMessage.Text = ml.Text;
            }
            catch (WinSCardException ex)
            {
                txtMessage.Text = ml.AddLine(ex.WinSCardFunctionName + " Error 0x" +
                                             ex.Status.ToString("X08") + ": " + ex.Message);
            }
            catch (Exception ex)
            {
                txtMessage.Text = ml.AddLine(ex.Message);
            }
        }
Пример #4
0
        static void write_to_tag(String bytes_in)
        {
            string command  = "FF D6 00 ";
            int    byte_num = 0;
            int    value;
            //string address = String.Format("{0:X}", value);
            String Hex_address;

            Byte[] Input_bytes = hex_string_to_byte_array(string_to_hex_string(bytes_in));
            byte[] send_chunk  = new byte[4];

            RespApdu write_four_bytes;

            for (int i = 0; i < Input_bytes.Length; i = i + 4)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (j + i < Input_bytes.Length)
                    {
                        send_chunk[j] = Input_bytes[j + i];
                    }
                }

                value            = Convert.ToInt32(byte_num);
                Hex_address      = String.Format("{0:X}", value);
                command          = command + Hex_address + " 04 " + HexFormatting.ToHexString(send_chunk, true);
                write_four_bytes = reader.Exchange(command);
                byte_num         = byte_num + 1;
            }
        }
Пример #5
0
        public static async Task Read()
        {
            PCSCReader reader = new PCSCReader();

            while (true)
            {
                try
                {
                    reader.Connect();
                    reader.ActivateCard();

                    RespApdu respApdu = reader.Exchange("FF CA 00 00 00"); // Get Card UID ...

                    if (respApdu.SW1SW2 == 0x9000)
                    {
                        InvokeScanned("0x" + HexFormatting.ToHexString(respApdu.Data, false));
                        await Task.Delay(1000);
                    }
                }

                catch (WinSCardException ex)
                {
                    InvokeError(ex.WinSCardFunctionName + " Error 0x" + ex.Status.ToString("X08") + ": " + ex.Message);
                    await Task.Delay(1000);
                }

                catch (Exception ex)
                {
                    InvokeError(ex.Message);
                    await Task.Delay(1000);
                }

                finally
                {
                    reader.Disconnect();
                }
            }
        }
Пример #6
0
        public byte[] GetRandom()
        {
            try
            {
                RespApdu respApdu = mReader.Exchange("A4 0A 00 00 08"); // Get Random Number

                //Console.WriteLine("Random  = 0x" + HexFormatting.ToHexString(respApdu.Data, true));
                //if (respApdu != null && respApdu.Data != null)
                if (respApdu.SW1SW2 == 0x9000)
                {
                    baRandom  = respApdu.Data;
                    strRandom = HexFormatting.ToHexString(baRandom, true);
                }
                else
                {
                    //baRandom = new byte[8];
                    //Array.Clear(baRandom, 0, 8);
                    //return null;
                    ushort sw = respApdu.SW1SW2 ?? 0;
                    throw new Exception("GetRandom has not supported. Card's SW = " + sw.ToString("X4"));
                }
                //strRandom = HexFormatting.ToHexString(baRandom, true);

                return(baRandom);
            }
            catch (WinSCardException ex)
            {
                //Console.WriteLine(ex.WinSCardFunctionName + " Error 0x" +
                //                   ex.Status.ToString("X08") + ": " + ex.Message);
                throw ex;
            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex.Message);
                throw ex;
            }
        }
Пример #7
0
        //Currently this writes plaintext to tag in NDEF format
        //TODO: Other tag types
        //      Limit to how much data can be fit to tag
        static void write_to_tag(String bytes_in)
        {
            string command_prefix = "FF D6 00 ";
            string command;
            String Hex_address;

            NdefMessage write_this = new NdefMessage {
                new NdefUriRecord {
                    Uri = bytes_in
                }
            };

            byte[]      ndef_prefix = { 0x03, (byte)write_this.ToByteArray().Length };
            List <byte> concat      = new List <byte>();

            concat.AddRange(ndef_prefix);
            concat.AddRange(write_this.ToByteArray());
            byte[] ndef_bytes = concat.ToArray();

            foreach (var item in ndef_bytes)
            {
                Console.WriteLine(HexFormatting.ToHexString(item));
            }

            //Byte[] Input_bytes = hex_string_to_byte_array(string_to_hex_string(bytes_in));
            byte[]   send_chunk = new byte[4];
            RespApdu write_four_bytes;
            int      count = 0;
            int      mod4  = ndef_bytes.Length % 4;


            for (int i = 0; i < ndef_bytes.Length; ++i)
            {
                send_chunk[count] = ndef_bytes[i];
                ++count;
                if (count == 4)
                {
                    count       = 0;
                    Hex_address = String.Format("{0:X}", Convert.ToInt32((i / 4) + 4));
                    if ((i / 4) + 4 < 16)
                    {
                        Hex_address = "0" + Hex_address;
                    }
                    Console.WriteLine(Hex_address);

                    command = command_prefix + Hex_address + " 04 " + HexFormatting.ToHexString(send_chunk, true);
                    Console.WriteLine(command);
                    write_four_bytes = reader.Exchange(command);
                }
            }

            if (count != 0)
            {
                for (int i = count; i < send_chunk.Length; i++)
                {
                    send_chunk[i] = 0x00;
                }
                send_chunk[2] = 254;
                Hex_address   = String.Format("{0:X}", Convert.ToInt32(((ndef_bytes.Length - count) / 4) + 4));
                if ((((ndef_bytes.Length - count) / 4) + 4) < 16)
                {
                    Hex_address = "0" + Hex_address;
                }
                command          = command_prefix + Hex_address + " 04 " + HexFormatting.ToHexString(send_chunk, true);
                write_four_bytes = reader.Exchange(command);
            }



            //command = command + "04" + " 04 " + hexString;
            //write_four_bytes = reader.Exchange(command);

            /*if (Input_bytes.Length % 4 != 0)
             * {
             *  int mod_of_input = Input_bytes.Length % 4;
             *  for (int i = 0; i < mod_of_input; i++)
             *  {
             *
             *
             *  }
             * }*/

/*
 *          for (int i = 0; i < Input_bytes.Length; i = i + 4)
 *          {
 *              for (int j = 0; j < 4; j++)
 *              {
 *                  if (j + i < Input_bytes.Length)
 *                  {
 *                      send_chunk[j] = Input_bytes[j + i];
 *                  }
 *              }
 *
 *              value = Convert.ToInt32(byte_num);
 *              Hex_address = String.Format("{0:X}", value);
 *              command = command + Hex_address + " 04 " + HexFormatting.ToHexString(send_chunk, true);
 *              write_four_bytes = reader.Exchange(command);
 *              byte_num = byte_num + 1;
 *          }*/
        }
Пример #8
0
        /*<summary>
         * BYTE[] find_ndef()
         * This function returns Byte array containgin all bytes from
         * starting byte of NDEF "D1" record to the termination byte "FE"
         *</summary>
         *
         * <remarks>
         * This function is naive and only tries to find the content
         * delimited by start and terminating bytes of NDEF:s
         * </remarks>
         *
         *
         */
        static Byte[] find_ndef()
        {
            RespApdu read_four_bytes;
            int      byte_num = 0;
            String   command;
            String   Hex_address;
            int      value;

            var allbytes = new List <string>();

            //Used for easy appending of all found bytes
            var hexbytes = new List <byte>();


            while (true)
            {
                value       = Convert.ToInt32(byte_num);
                Hex_address = String.Format("{0:X}", value);

                //RespApdu only accepts commands that are of type
                //"XX XX XX XX XX" so zero has to be prepended
                //for values smaller than 0x10
                if (byte_num < 16)
                {
                    Hex_address = "0" + Hex_address;
                }

                //We start from block 0 byte 0
                //Reading command: "FF B0 00 XX YY"
                //XX is reading address
                //YY is amount of bytes to read
                //Distance between each XX is 04
                command = "FF B0 00 " + Hex_address + " 04";

                read_four_bytes = reader.Exchange(command);
                if (read_four_bytes.SW1SW2 != 0x9000)
                {
                    Console.WriteLine("Reading bytes from the NFC tag failed. reader returned: ", HexFormatting.ToHexString(read_four_bytes.Data, true));
                    break;
                }

                allbytes.Add(HexFormatting.ToHexString(read_four_bytes.Data, true));
                hexbytes.AddRange(read_four_bytes.Data);


                if (HexFormatting.ToHexString(read_four_bytes.Data, true).Contains("FE"))
                {
                    Console.WriteLine("End of NDEF found");
                    break;
                }
                byte_num = byte_num + 1;
            }

            foreach (Object obj in hexbytes)
            {
                Console.Write("   {0}", String.Format("{0:X}", obj));
            }
            Console.WriteLine();

            for (int i = 0; i < hexbytes.Count; i++)
            {
                //This IS D1 in hex. It starts NDEF tags
                if (hexbytes[i] == 209)
                {
                    hexbytes.RemoveRange(0, i);
                }
                else if (hexbytes[i] == 254)
                {
                    break;
                }
            }

            return(hexbytes.ToArray());
        }
Пример #9
0
        static void Main(string[] args)
        {
            ConsoleTraceListener consoleTraceListener = new ConsoleTraceListener();

            Trace.Listeners.Add(consoleTraceListener);

            reader = new PCSCReader();
            //NdefLibrary.Ndef.NdefMessage message = new NdefLibrary.Ndef.NdefMessage();



            /*WinSCard testi = new WinSCard();
             * testi.Connect(null);*/



            string input_text = "";

            while (input_text != "joo")
            {
                try
                {
                    //reader.SCard.Connect("",SCARD_SHARE_MODE.Direct, SCARD_PROTOCOL.Tx);
                    reader.Connect();

                    //set_buzzer_on();
                    //set_target_mode();

                    reader.ActivateCard();

                    //WinSCard reader = new WinSCard();
                    //testi.Connect(null);

                    //For some reason Direct commands only work after card has been activated (the command above)
                    //Also the reader resets to normal state after it's been unplugged.
                    //TODO: check if mode changes can be made permanent
                    if (!modes_changed)
                    {
                        //Console.WriteLine("YOLOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
                        //change_modes_for_reader();
                        modes_changed = true;
                    }

                    RespApdu respApdu = reader.Exchange(APDU_commands["get_card_uid"]); // Get Card UID ...

                    if (respApdu.SW1SW2 == 0x9000)
                    {
                        Console.WriteLine("UID  = 0x" + HexFormatting.ToHexString(respApdu.Data, true));
                    }

                    use_stuff_properly();
                    //RespApdu tespApdu = reader.Exchange(String.Format(APDU_commands["direct_command_prefix"],"18", "D4 86 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")); // Get Card UID ...
                    //RespApdu bespApdu = reader.Exchange(String.Format(APDU_commands["direct_command_prefix"], "18", "D4 40 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")); // Get Card UID ...

                    //message = NdefLibrary.Ndef.NdefMessage.FromByteArray();
                    //message = NdefLibrary.Ndef.NdefMessage.FromByteArray(find_ndef());
                    //parse_record(message);

                    //write_to_tag("meloonis");
                }
                catch (WinSCardException ex)
                {
                    Console.WriteLine(ex.WinSCardFunctionName + " Error 0x" +
                                      ex.Status.ToString("X08") + ": " + ex.Message);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    reader.Disconnect();
                    Console.WriteLine("Please press any key...");
                    input_text = Console.ReadLine();
                }
            }
        }