1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| from smartcard.System import readers from smartcard.CardConnection import CardConnection
class M1CardTool:
KEY_DEFAULT = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
def __init__(self): self.connection = None
def connect(self): """ 建立连接
:return: 成功 """ r = readers() if len(r) == 0: print("未发现读卡器") return False print(f"发现读卡器: {r[0]}") self.connection = r[0].createConnection() self.connection.connect(CardConnection.T1_protocol) print(f"与读卡器建立连接成功: {r[0]}") return True
def disconnect(self): """ 断开连接 """ if self.connection: self.connection.disconnect() print(f"与读卡器断开连接成功")
def authenticate_sector(self, block_addr, key=None, is_key_a=True): """ 认证扇区
:param conn: 连接 :param block_addr: 区块编号,从0开始 :param key: 密码 :param is_key_a: 是否使用密钥A验证密码 :return: 成功 """
if key is None: key = self.KEY_DEFAULT
key_type = 0x60 if is_key_a else 0x61
apdu = [0xFF, 0x86, 0x00, 0x00, 0x05, 0x01, 0x00, block_addr, key_type, 0x00] + key _, sw1, sw2 = self.connection.transmit(apdu) return sw1 == 0x90 and sw2 == 0x00
def read_block(self, block_addr): """ 读取块
:param block_addr: 区块编号,从0开始 :return: 块数据 """
apdu = [0xFF, 0xB0, 0x00, block_addr, 0x10] data, sw1, sw2 = self.connection.transmit(apdu) if sw1 == 0x90 and sw2 == 0x00: return data else: return False
def write_block(self, block_addr, data_16bytes): """ 写入块
:param block_addr: 区块编号,从0开始 :param data_16bytes: 16字节的数据 :return: 成功 """
if len(data_16bytes) != 16: print("必须写入16字节数据") return False
apdu = [0xFF, 0xD6, 0x00, block_addr, 0x10] + data_16bytes _, sw1, sw2 = self.connection.transmit(apdu) return sw1 == 0x90 and sw2 == 0x00
|