Python 通过 pyscard 模块操作 M1 卡,下载和说明地址:http://pyscard.sourceforge.net/index.html
对 M1 卡的操作 使用 ISO智能卡通用APDU命令集,简称APDU,网上能找到一堆资料。
M1 卡的使用简单分为三部分:
to load auth key to reader
I send: 0xFF 0x82 0x00 0x00 0x06 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
Recive: 90 00 - it’s ok
EXTERNAL AUTHENTICATE命令报文:
0xFF 0x82 : CLA INS
0x00 0x00:P1,P2
0x06:Lc
0xFF 0xFF 0xFF 0xFF 0xFF 0xFF:数据字段 key
以下各命令格式类似:
to authenticate in block 01
I send: 0xFF 0x86 0x00 0x00 0x05 0x01 0x00 0x01 0x60 0x00
Recive: 90 00 - it’s ok
to read data from block 01
I send: 0xFF 0xB0 0x00 0x01 0x0F
Recive: 90 00
一段可直接提交APDU代码:
from smartcard.scard import *
import smartcard.util
hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
assert hresult==SCARD_S_SUCCESS
hresult, readers = SCardListReaders(hcontext, [])
assert len(readers)>0
reader = readers[0]
hresult, hcard, dwActiveProtocol = SCardConnect(
hcontext,
reader,
SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1)
hresult, response = SCardTransmit(hcard,dwActiveProtocol,[0xFF,0x82,0x00,0x00,0x06,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]) # 写入想要向卡发送的APDU指令,即可提交执行
print(response) #卡片返回的数据
print smartcard.util.toHexString(response)
def load_key():
response = SCardTransmit(hcard,dwActiveProtocol,[0xFF,0x82,0x00,0x00,0x06,0xF5,0x03,0x9A,0xBB,0x22,0x36])
print smartcard.util.toHexString(response[1])
def authenticate_block():
response = SCardTransmit(hcard,dwActiveProtocol,[0xFF,0x86,0x00,0x00,0x05,0x01,0x00,0x3E,0x60,0x00])
print smartcard.util.toHexString(response[1])
def read_data():
response = SCardTransmit(hcard,dwActiveProtocol,[0xFF,0xB0,0x00,0x3E,0x0F])
print smartcard.util.toHexString(response[