//this method will open a reader instance and read the nfc device with //cmd as its input and the command to run. it will return a string of the result. //@params cmd - the command to run private string readNFC(CmdApdu cmd) { PCSCReader reader = new PCSCReader(); string payload = ""; try { reader.Connect(); reader.ActivateCard(GS.SCard.Const.SCARD_SHARE_MODE.Exclusive, GS.SCard.Const.SCARD_PROTOCOL.Tx); RespApdu respApdu = reader.Exchange(cmd, 0x9000); if (respApdu.SW1SW2 == 0x9000) { payload = Encoding.UTF8.GetString(respApdu.Data); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { reader.Disconnect(); } return(payload); }
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); } }
public void SelectAID() { try { RespApdu respApdu = mReader.Exchange("00 A4 04 00 0C A0 00 00 02 83 00 00 08 12 01 07 00"); // SELECT_AID } 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; } }
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(); } } }
/*<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()); }
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(); } } }