如何用pyscard写智能卡

时间:2011-07-04 14:45:53

标签: python smartcard

我正在使用读写器acr38f而我的智能卡是SLE4418。如何读取和写入智能卡文本?

例如:Hello World!

  

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

     

响应,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 )

卡片响应:

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 

1 个答案:

答案 0 :(得分:12)

我没有硬件来测试这个,但这应该让你开始:

与智能卡通信涉及发送APDU (智能卡应用协议数据单元)命令并接收APDU响应。

命令APDU通过读/写(您的ACR38F)发送,包含一个4字节的标头,后跟数据(以及有关数据大小和响应大小的信息)

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

回复包括:

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

SLE4418, in order to write data,的情况下,值应如下:

  • CLA = 00
  • INS = D6
  • P1 =存储器地址偏移的MSB以存储字节
  • P2 =存储器地址偏移的LSB以存储字节
  • Lc =要存储的字节长度
  • DATA =要存储的数据
  • Le =(空)

因此在代码中:

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 )

其他相关信息: