Friday, January 16, 2015

Get ATR using pcsc-sharp

This time I'm gonna show you how to get an ATR from a smartcard using pcsc-sharp wrapper step by step. First you have to download the pcsc-shrap project, you can download that from this site. After downloading that project, extract it into your pc hardisk. Then let's start by making a new project in visual studio 2010 :

Then add one button, textbloct and viewbox from toolbox menu to mainwindow.xml then arrange those items according to picture below :


If you are confuse with that, you can directly edit the xml file like this :

Well, you can see that the main window is so simple, so you can make your own window as you like. Next let's add the pcsc-sharp project into this project. Just right click on the solution project, choose "add", then "Existing Project..."  :
After clicking "Existing Project.." then browse the "pcsc-sharp.csproj" :


If everything is right, then you should see a new project in your visual studio.
In order to use all of the available function in pcsc-sharp, we need to reference to it first. So right click in getATRv0 project, choose "add reference.." :

After clicking "Add Reference...", choose project tab, then choose pcsc-sharp, last hit "ok" button :

Until this state, now we can use the available method, class, or namespace from pcsc-sharp project. To prove that, let's go back to our mainwindow.xaml.cs and put these line codes :

As you can see at line 14, we are using PCSC namespace that we reference from pcsc-sharp project and at line 23 to 26, we are using the classes from PCSC namespace, so this is a proof that now we can use the pcsc-sharp project in our current visual studio project. Well, umm... I think it's good if we run our getATR project, just to make sure that everything is ok hehhee.... :D

Ok,.Let's add another variables into our project :

the szReaders will gonna be saving the reader name for our smartcard and atr will be being used to save the byte of smartcard ATR. Ok let's add some methods, the first is :
getAvailableReader will be used to make a context from our reader that already connected to our PC. That method is also used to list all of the available reader then put the readers name to szReader variabel.

The next method is :
As you can see from the method's name, connecReader will be used to connect our application to smartcard reader.

The last method is getATR() as we can see in below :

getATR is the main method that will accomplish our goal in this posting. This method will be used to get the ATR bytes. As a note, this method has to be preceded by getAvailableReader and connectReader methos. Btw, if getATR method failed to be executed or an error occured during getATR method execution, then atrbuff will be filled up with 0x6E and 0x00.

Next, let's add a handle code to our button. Just double click the button in your xml file, then add these line codes into it :
As we can see, when the button is pressed by user, the getAvailableReader's method is called first then connectReader. After that we call our getAtr which put the ATR bytes into atr variable, then we display those atr bytes into our textbox with helped by BitConverter that change atr bytes into a hex string.

The result from this project is shown below :

Thanks,.. I hope this posting is usefull to someone who has been facing the same problem with me when trying to find a way to get the ATR from smartcard using pcsc-sharp :-)

No comments:

Post a Comment