Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing Data to CardChannelIso7816 #1

Closed
KingdomHeards3 opened this issue Feb 20, 2021 · 2 comments
Closed

Writing Data to CardChannelIso7816 #1

KingdomHeards3 opened this issue Feb 20, 2021 · 2 comments
Assignees
Labels

Comments

@KingdomHeards3
Copy link

Hey, would it be possible to receive some examples on how to write Data to a CardChannelIso7816?
So which Commands to use and what parameters to pass in?
I would just like to write some basic stirngs and afterwards read them from the NFC Tag.
For a non expert in NFC Communication trying to read the little Documentation on the Classes without some examples is really difficult.

Thank you very much in advance.
KingdomHeards3.

@zetoken zetoken self-assigned this Feb 21, 2021
@wsct
Copy link
Owner

wsct commented Feb 21, 2021

Hi. I'll try to give you some information.

First, you must make sure that your NFC card is 7816-4 compliant. That means that it supports not only native NFC commands but also ISO/IEC 7816 APDU (Command & Response). That will allow you to use the CardChannelIso7816 channel to send CRP (Command Response Pair).
For memory, a 7816-4 APDU is made of these 4 first bytes and 3 fields: CLA INS P1 P2 (Lc UDC) (Le), where (Lc UDC) are the optional parameter bytes of the command and (Le) the optional size of result buffer.

Then you can use the example given in the README of this project and build the Command APDU you need by using the baseCommandAPDU class. For example, something like:

var command = new CommandAPDU(0x90, 0xBD, 0x00, 0x00, new byte[] {}, 0x00)

You may also extend the CommandAPDU to get easier to use commands, like this example for a Myfare EV1 card:

public class ReadDataCommand : CommandAPDU
{
    public ReadDataCommand(byte fileId, int offset, int length) : base(0x90, 0xBD, 0x00, 0x00, 0x00)
    {
        Udc = fileId.ToByteArray()
            .Concat(offset.ToBytes(3).Reverse())
            .Concat(length.ToBytes(3).Reverse())
            .ToArray();
    }
}

then use it like this:

var readDataCommand = new ReadDataCommand(1, 0, 31);
var crp = new CommandResponsePair(command);
if (crp.ErrorCode != ErrorCode.Success)
{
    Console.WriteLine($"Something went wrong: {crp.ErrorCode}");
    return;
}
// do whatever you want with crp.RApdu.Udr

If you're looking for the supported commands list, you should have a look at this NFC Forum specification page 31 that defines the READ and WRITE commands as 7816-4 compliant commands.
If think that I previously wrote these 2 commands for NDEF so may add a link to it later.

If your card is not 7816-4 then you may try to use the raw access to the card. Note that not all contactless readers support the raw mode. You won't be able to use the CardChannelIso7816 class and will have to stick to the base CardChannel.
Initialization code would be like:

var rawChannel = new CardChannel(context, context.Readers.Last());
rawchannel.Connect(ShareMode.Exclusive, Protocol.Raw);

You would then define a RawCommand class (for example the one defined in this gist) and build your commands.

Hope it helps.

@zetoken
Copy link
Collaborator

zetoken commented Mar 18, 2021

No news is good news. Closing this issue :).

@zetoken zetoken closed this as completed Mar 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants