Read RFID tag using LibUsbDotNet - c#

I have a generic USB RFID card reader.
I am using code from How to read from a usb rfid reader? to read the data. It seems to read okay, however, the output is a byte array. What I want to get is the RFID number, the one that's printed on the card. How can I get this?
EDIT
I successfully retrieved the tag number by implementing a key logger. It seems that the reader does not directly send the tag number down the wire, but rather, sends a command to type the tag number out. This solution works but I'm still open to other, more direct approaches.

After successfully reading the RFID tag, all you need to do is to convert the byte array into a string. Please refer to the following line of code and this link for more details.
// readBuffer: The byte array containing the sequence of bytes to decode.
// 0: The index of the first byte to decode.
// bytesRead: The number of bytes to decode.
var strRfidTag = System.Text.Encoding.Default.GetString(readBuffer, 0, bytesRead);

Related

How to send Bytes completely via WebSocketSharp

I am making a audio chat program, So I tried to sending Audio bytes via web-socket
first I did, I get audio bytes and send it on But it was failed(maybe there can't pass completely)
second I tried is convert bytes to string with use BitConverter and convert again to byte array with Encoding.UTF8.GetBytes method
this is my code
var pcmAudio = stream.ToByteArray();
var audio = Encoding.UTF8.GetBytes(BitConverter.ToString(pcmAudio));
if I send that 'audio' it works. I can convert to byte array and I can play audio.
but, if I send pcmAudio there are an error
Stream ms = new MemoryStream(Encoding.UTF8.GetBytes(data));
above is my receiving audio code. data is string. there's no way to receive with Byte type.
So i had to convert data to bytes.
Unfortunately it doesn't work.
the error message is 'Wave header is corrupted'
i want to send byte array compeletly.
ur question 1. Why do you want to send bytes ? You know the way to send audio with bitconverter
my answer 1. Byte length would be larger than not converted
thank you

Getting originality signature from Mifare Ultralight card using an AC/SC reader

Currently in my C# console application, I've implemented an NFC tag reader functionality by sending APDU commands using ModWinsCard. So far I can get a list of connected readers, connect to it, get the NFC tag's UID, and reading/writing NDEF message to the NFC tag.
Right now, I'm trying to check whether the NFC tag in question is a genuine tag made by NXP by validating the originality signature, as per this document: https://www.nxp.com/docs/en/data-sheet/NTAG213_215_216.pdf (section 8.9).
However, while I have implemented the signature validation, I have yet to be able to get the signature from the NFC tag, since section 10.8 (READ_SIG) does not have any equivalent for APDU command.
Is there any APDU command I can use to send the READ_SIG command to the NFC tag and get the originality signature? The reader I'm currently using is ACS ACR1281U-C1, if it's any help.
If you look Section 10.8 of the same doc the READ_SIG is just a standard low level command like READ or WRITE or FAST_READ, you just need to transceive the right byte array to the card and handle the byte array returned
I don't do c# and ModWinsCard but at a glance you need to SCardTransmit with byte SendBuff byte array of [3C,00] and receive back a 32 byte array.
I have done this in Java on Android easily.
Extra info as NTAG 21X cards are only Type 2 Cards they don't use APDU's
From the datasheet
NTAG213, NTAG215 and NTAG216 (from now on, generally called NTAG21x) are designed to fully comply to NFC Forum Type 2 Tag
The generic NFC docs for Type 2 Cards are available http://apps4android.org/nfc-specifications/
Only Type 4 cards use APDU commands
Update:
May be this helps?
https://stackoverflow.com/a/26069377/2373819
Seems You can "Pass Trough" The APDU level to send Native commands (In this case 0x1B is a Type 2 command, correct for Password Auth
Along with command i.e. 3C 00 you have to pass CRC of 2 bytes.

isolating arrays in byte arrays received in different times

I am receiving a Bytes array every second from a source in my winform application via Uart. lets say it is something like below. 99,98 marks the start of a new packet. Each packet is of variable length but it always starts with this 99,98 id. I want to copy the individual packet into a receivedBuffer and then process them individually
second 1:
{56,42,43,76,125,56,34,234,12,3,5,76,8,0,99,98,234,56,211,122,22,4,7,89,76,64,12,3,5,99,98,0,6,125}
second2:
{6,125,56,34,234,12}
So inabove example in second 1 I receive first some garbage value, then 1 full packet and other partial packet. and in second 2 I receive the remaining of 2nd packet
(ps: packet 1 is 99,98,234,56,211,122,22,4,7,89,76,64,12,3,5)
a packet continues until u receive a 99,98 id bytes
Sounds like a protocol difficult to handle: you can only rely on a "begin of packet" marker, you don't have a "packet length" nor "end of packet" marker.
So, reasonably, what you can do is:
start with an empty array / memoryStream
every time you get some bytes, add them the the array
parse the new array content looking for "beging of packet" marker. if you find two (or more) of them, extract bytes included between them, and remove all bytes preceding last "begin of packet" found
wait for next bytes
Note that you aren't able to understand when last packet is completed just parsing received bytes.

Determine what Byte array is?

I have recently started learning C# Networking and I was wondering how would you tell if the received Byte array is a file or a string?
A byte array is just a byte array. It's just got data in.
How you interpret that data is up to you. What's the difference between a text file and a string, for example?
Fundamentally, if your application needs to know how to interpret the data, you've got to put that into the protocol.
A byte array is just a byte array. However, you could make the original byte array include a byte that describes what type it is (assuming you are the originator of it). Then you find this descriptor byte and use it to make decisions.
Strings are encoded byte arrays; files can contain strings and/or binary data.
ASCII strings use byte values between 0-127 to represent characters and control codes. For UTF8 people have written validation routines (https://stackoverflow.com/a/892443/884862).
You'd have to check the array for all of the string encoding characteristics before you could assume it's a binary file.
edit Here's an SO question about classifying a file type Using .NET, how can you find the mime type of a file based on the file signature not the extension using a signature (first X bytes) of the file to determine it's mimetype.
No you can't. Data is data, you must layer on top of your network communication form of protocol, it will need to say something like: "If the first byte I see is a 1 the next four bytes represent a int, if I see a 2 read the next byte and that is the length of the text string that follows that..."
A much easier solution than inventing your own protocol is use a prebuilt one that gives you a higher level abstraction like WCF so you don't need to deal with byte arrays.
Not quite a "file", an array contains data. You should loop through that array and write the data,
Try this:
foreach(string data in array)
{
Console.WriteLine(data);
}
Now, if it doesn't contain strings, but data, you can simply use a
foreach(var data in array)
{
Console.WriteLine(data.ToString());
}

c# reading/writing bytes to/from console

I have a byte[] array and want to write it to stdout: Console.Out.Write(arr2str(arr)). How to convert byte[] to string, so that app.exe > arr.txt does the expected thing? I just want to save the array to a file using a pipe, but encodings mess things up.
I'd later want to read that byte array from stdin: app.exe < arr.txt and get the same thing.
How can I do these two things: write and read byte arrays to/from stdin/stdout?
EDIT:
I'm reading with string s = Console.In.ReadToEnd(), and then System.Text.Encoding.Default.GetBytes(s). I'm converting from array to string with System.Text.Encoding.Default.GetString(bytes), but this doesn't work when used with <,>. By "doesn't work" I mean that writing and reading over a pipe does not return the same thing.
To work with binary files you want Console.OpenStandardInput() to retrieve a Stream that you can read from. This has been covered in other threads here at SO, this one for example: Read binary data from Console.In
If you are writing to Console.WriteLine you need to encode the text in to a printable format. If you want to output to a file as a binary you can't use Console.WriteLine
If you still need to output to the console you either need to open the raw stream with Console.OpenStandardOutput() or call Convert.ToBase64String to turn the byte array to a string. There is also Convert.FromBase64String to come back from base64 to a byte array.

Categories