2012年12月16日 星期日

Linux Python 讀取健保卡 程式碼

$ vi healthcard.py
from smartcard.System import readers                                                                                        

# define the APDUs used in this script
SelectAPDU = [ 0x00, 0xA4, 0x04, 0x00, 0x10, 0xD1, 0x58, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00 ]

ReadProfileAPDU = [ 0x00, 0xca, 0x11, 0x00, 0x02, 0x00, 0x00 ]

# get all the available readers
r = readers()
print "Available readers:", r

reader = r[0]
print "Using:", reader

connection = reader.createConnection()
connection.connect()

data, sw1, sw2 = connection.transmit(SelectAPDU)
print "Select Applet: %02X %02X" % (sw1, sw2)

data, sw1, sw2 = connection.transmit(ReadProfileAPDU)
#print data
print "Command: %02X %02X" % (sw1, sw2)
print  'Card Number : %s' % ''.join(chr(i) for i in data[0:12])
print  'Name : %s' % ''.join(chr(i) for i in data[12:18])
print  'ID Number : %s' % ''.join(chr(i) for i in data[32:42])
print  'Birthday : %s' % ''.join(chr(i) for i in data[43:49])
print  'Sex : %s' % ''.join(chr(i) for i in data[49:50])
print  'Card Date : %s' % ''.join(chr(i) for i in data[51:57])

$ python healthcard.py



Reference :
Ludovic Rousseau blog: PCSC sample in Python

2 則留言:

  1. 無效啊啦!
    如合庫卡插著
    $ /usr/share/doc/python-pyscard/examples/Examples/simple/getATR.py
    CASTLES EZ100PU 00 00 3B F7 11 00 00 81 71 FE...
    健保卡則無。
    再以 pcsc_scan(1)
    合庫卡很多東西,健保則
    Card state: Card inserted, Unresponsive card,
    至於貴 program,
    smartcard.Exceptions.CardConnectionException: Unable to connect with protocol: T0 or T1. Card is unpowered.

    回覆刪除
  2. 請問哪裡有!!smartcard.System???

    回覆刪除