C# smartcards programming

A while back I had to write what I thought was going to be some simple and easy smartcard code and let me tell you, it was no walk in the park. First of all, it turned out that good information on smartcards is hard to come by on the Net. And when I did find several websites, there was a lot of discussion of the underlying Win32 API calls and a whole bunch of other low-level stuff, which isn't quite easy reading.

The thing is that they're called smart cards for a reason. That reason is that you're basically interacting with an embedded microprocessor by sending it commands that it understands and responds to. This can get a bit complicated because many of the manufacturers thought it was a good idea to implement their own commands, so many things are contingent on the type of card that you're using. On top of that there are cards that look like smartcards but aren't really smartcards and don't even use any of the "standard" smartcard interfaces, so you end up using the Windows SmartCard API just to fire up your reader, and the smartcard reader's special API provided as a .lib in some obscure corner of their website for the rest...

Well, I did get my app working eventually but like I said, it wasn't easy. At any rate I made a big mistake by assuming it would not be very difficult to do.

If you really want to get into smartcards I suppose your best bet would be to get a good book on the subject but since I too am little more than a novice, I really can't help you there.

As for websites, the most useful thing in C# terms that I was able to dig up was A Smartcard Framework for .Net. Note that this is not the same article as the one posted by michaelvdnest, although the author is the same. The article I linked to describes a C# wrapper for the native smartcard API, while michaelvdnest's adds XML to the mix, although I don't know exactly in what way because I haven't read it yet.

So start dissecting the code supplied with the article and good luck. If memory serves me, the reader enumeration stuff is pretty easy to find.


Check out Smart Card Framework for .NET on Code Project. It describes an XML framework for .NET to program Smart Card applications.


I know it's way late.....

but

You can get a very easy to use, and free for personal use professional library here:

https://www.smartcard-api.com/

There's also a community (Completely free to use) Smart Card library here

https://www.nuget.org/packages/PCSC

In recent years I've favored using the Nu-get one rather than the smart card API one, if you need commercial support however the smart card API lib is the best choice.

Once you have that, you then need to start understanding APDU's and how to shuttle them back and forth from card to terminal.

It really is a huge subject to cover, I have code that will read all the common elements on a standard Chip & Pin EMV card unfortunately it was written for a financial services client so I can't give you it, I can explain however some of what you need to undertake to read the card.

For other cards then it's down to the card itself, for example, I have a card reader and blank cards here, and I have some custom data on some of them which only I know how to access, yet still I have some old satellite and travel cards that are different again.

You can get some ideas here:

How do I read the PAN from an EMV SmartCard from Java

and there's a lot of info on smart cards in general to be found here:

https://web.archive.org/web/20160329205518/http://wrankl.de/SCTables/SCTables.html

especially the generic stuff (of which there is some)

Tags:

C#

Smartcard