Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write smart card with pyscard

I'm using reader/writer acr38f and my smart card is SLE4418. How do I read and write text to my smart card?

For example: Hello World!

apdu = [0XFF, 0X20,0x00,0x00,0x02, 0x00, 0x00]

response, sw1, sw2 = cardservice.connection.transmit( apdu )

apdu = [0XFF,0xA4,0x00,0x00,0x01,0x05]
response, sw1, sw2 = cardservice.connection.transmit( apdu )




apdu = [0XFF,0XB2,0X00,0xA7,0X09]
response, sw1, sw2 = cardservice.connection.transmit( apdu )
print response


apdu = [0XFF, 0XD0,0x00,0xA7,0x09,0xA7,0x02,0xA7,0x02,0xA7,0x02,0xA7,0x02,0xA7] 
response, sw1, sw2 = cardservice.connection.transmit( apdu )

card response :

connecting to ACS CCID USB Reader 0
ATR 3B 04 92 23 10 91
>  FF 20 00 00 02 00 00
<  00 00 00 90 0 
>  FF A4 00 00 01 05
<  []  90 0 
>  FF B2 00 A7 09
<  FF FF FF FF FF FF FF FF FF 90 0 
[255, 255, 255, 255, 255, 255, 255, 255, 255]
>  FF D0 00 A7 09 A7 02 A7 02 A7 02 A7 02 A7
<  []  90 0 
like image 234
john misoskian Avatar asked Jul 04 '11 14:07

john misoskian


People also ask

What is smart card example?

These cards are inserted into the card reader, which reads the information stored on the contact plate and carries out the required transaction. The most common examples of contact smart cards are credit cards, ATM cards, and SIM cards.

What is Pyscard?

pyscard - Python smart card library - is a Python module adding smart cards support to Python.

Can a smart card reader also write?

USB dongles are also frequently used with GSM phones containing a SIM smart card. Phone numbers can be edited on a PC using a USB smart card dongle. Smart card readers can also write to smart cards. So, when someone talks about a smart card reader they really mean a smart card reader/writer.

How do you use a smart card?

Contact smart cards are inserted into a smart card reader, making physical contact with the reader. However, contactless smart cards have an antenna embedded inside the card that enables communication with the reader without physical contact. You tap and pay. Contactless is easy and convenient.


1 Answers

I do not have the hardware to test this, but this should get you going:

Communicating with smart cards involves sending APDU (Smart Card application protocol data unit) commands and receiving APDU responses.

Command APDUs are sent through the reader/write (your ACR38F) and consists of a 4-byte header followed by data (and info about the data size and response size)

Field    Len Description -------------------------------------------- CLA     (1) Instruction Class INS     (1) Instruction Code P1-P2   (2) Instruction Parameters Lc  (0,1,3) Number of data bytes to follow DATA    (*) Data to be transmitted Le    (0-3) Maximum response bytes 

The response consists of:

Field    Len Description -------------------------------------------- DATA    (*) Data to be transmitted SW1-SW2 (2) Command status 

In the case of the SLE4418, in order to write data, the values should be as follows:

  • CLA = 00
  • INS = D6
  • P1 = MSB of memory address offset to store bytes
  • P2 = LSB of memory address offset to store bytes
  • Lc = length of bytes to store
  • DATA = data to store
  • Le = (empty)

So therefore in code:

WRITE = [0x00, 0xD6] STARTMSB = [0x00] #change to where on the card you would like to write STARTLSB = [0x00] #same here MEM_L = [0x01] DATA = [0x01]  cardservice.connection.connect() apdu = READ + STARTMSB + STARTLSB + MEM_L + DATA response1, sw1, sw2 = self.cardservice.connection.transmit( apdu ) 

Other relevant info:

  • http://www.belelectro-m.by/docs/doc3723.pdf
  • http://www.youcard.de/datenblaetter/chipkarten/DB%20SLE441828.pdf
like image 84
Rajiv Makhijani Avatar answered Oct 13 '22 09:10

Rajiv Makhijani