def readPhoto(self): if self.status == None: if self.connect(self.readerIndex) == None: return None photo = self.read(self.CMD_PHOTO01)[0] photo += self.read(self.CMD_PHOTO02)[0] photo += self.read(self.CMD_PHOTO03)[0] photo += self.read(self.CMD_PHOTO04)[0] photo += self.read(self.CMD_PHOTO05)[0] photo += self.read(self.CMD_PHOTO06)[0] photo += self.read(self.CMD_PHOTO07)[0] photo += self.read(self.CMD_PHOTO08)[0] photo += self.read(self.CMD_PHOTO09)[0] photo += self.read(self.CMD_PHOTO10)[0] photo += self.read(self.CMD_PHOTO11)[0] photo += self.read(self.CMD_PHOTO12)[0] photo += self.read(self.CMD_PHOTO13)[0] photo += self.read(self.CMD_PHOTO14)[0] photo += self.read(self.CMD_PHOTO15)[0] photo += self.read(self.CMD_PHOTO16)[0] photo += self.read(self.CMD_PHOTO17)[0] photo += self.read(self.CMD_PHOTO18)[0] photo += self.read(self.CMD_PHOTO19)[0] photo += self.read(self.CMD_PHOTO20)[0] response = HexListToBinString(photo) return response
def run_aes(l_secretKey, l_input, l_mask): lengthKey = len(l_secretKey) lengthInput = len(l_input) lengthMask = len(l_mask) ################################################## # send Write Command print "Set Key" # build complete Read APDU l_APDUSOSSESetKey_complete = [] l_APDUSOSSESetKey_complete += l_APDUSOSSESetKey # unused P1 l_APDUSOSSESetKey_complete.append(0) # unused P2 l_APDUSOSSESetKey_complete.append(0) # key length l_APDUSOSSESetKey_complete.append(lengthKey) # key value l_APDUSOSSESetKey_complete += l_secretKey # send Write APDU print "send APDU: %s" % toHexString(l_APDUSOSSESetKey_complete) response, sw1, sw2 = o_deviceConnection.transmit( l_APDUSOSSESetKey_complete) print "data: %s" % toHexString(response) print "sw : %s" % toHexString([sw1]) + toHexString([sw2]) ################################################## # send Read Command print "Read Key" # build complete Read APDU l_APDUSOSSEGetKey_complete = [] l_APDUSOSSEGetKey_complete += l_APDUSOSSEGetKey # unused P1 l_APDUSOSSEGetKey_complete.append(0) # unused P2 l_APDUSOSSEGetKey_complete.append(0) # key length l_APDUSOSSEGetKey_complete.append(lengthKey) # send Read APDU response, sw1, sw2 = o_deviceConnection.transmit( l_APDUSOSSEGetKey_complete) print "data: %s" % toHexString(response) print "sw : %s" % toHexString([sw1]) + toHexString([sw2]) ################################################## # send Write Command print "Set Input" # build complete Read APDU l_APDUSOSSESetInput_complete = [] l_APDUSOSSESetInput_complete += l_APDUSOSSESetInput # unused P1 l_APDUSOSSESetInput_complete.append(0) # unused P2 l_APDUSOSSESetInput_complete.append(0) # input length l_APDUSOSSESetInput_complete.append(lengthInput) # input value l_APDUSOSSESetInput_complete += l_input # send Write APDU print "send APDU: %s" % toHexString(l_APDUSOSSESetInput_complete) response, sw1, sw2 = o_deviceConnection.transmit( l_APDUSOSSESetInput_complete) print "data: %s" % toHexString(response) print "sw : %s" % toHexString([sw1]) + toHexString([sw2]) ################################################## # send Read Command print "Read Input" # build complete Read APDU l_APDUSOSSEGetInput_complete = [] l_APDUSOSSEGetInput_complete += l_APDUSOSSEGetInput # unused P1 l_APDUSOSSEGetInput_complete.append(0) # unused P2 l_APDUSOSSEGetInput_complete.append(0) # input length l_APDUSOSSEGetInput_complete.append(lengthInput) # send Read APDU response, sw1, sw2 = o_deviceConnection.transmit( l_APDUSOSSEGetInput_complete) print "data: %s" % toHexString(response) print "sw : %s" % toHexString([sw1]) + toHexString([sw2]) ################################################## # send Write Command print "Set Mask" # build complete Read APDU l_APDUSOSSESetMask_complete = [] l_APDUSOSSESetMask_complete += l_APDUSOSSESetMask # unused P1 l_APDUSOSSESetMask_complete.append(0) # unused P2 l_APDUSOSSESetMask_complete.append(0) # mask length l_APDUSOSSESetMask_complete.append(lengthMask) # mask value l_APDUSOSSESetMask_complete += l_mask # send Write APDU print "send APDU: %s" % toHexString(l_APDUSOSSESetMask_complete) response, sw1, sw2 = o_deviceConnection.transmit( l_APDUSOSSESetMask_complete) print "data: %s" % toHexString(response) print "sw : %s" % toHexString([sw1]) + toHexString([sw2]) ################################################## # send Read Command print "Read Mask" # build complete Read APDU l_APDUSOSSEGetMask_complete = [] l_APDUSOSSEGetMask_complete += l_APDUSOSSEGetMask # unused P1 l_APDUSOSSEGetMask_complete.append(0) # unused P2 l_APDUSOSSEGetMask_complete.append(0) # mask length l_APDUSOSSEGetMask_complete.append(lengthMask) # send Read APDU response, sw1, sw2 = o_deviceConnection.transmit( l_APDUSOSSEGetMask_complete) print "data: %s" % toHexString(response) print "sw : %s" % toHexString([sw1]) + toHexString([sw2]) ################################################## # send Read Command print "Launch AES 128" # build complete Read APDU l_APDUSOSSEAESEnc_complete = [] l_APDUSOSSEAESEnc_complete += l_APDUSOSSEAESEnc # unused P1 l_APDUSOSSEAESEnc_complete.append(0) # unused P2 l_APDUSOSSEAESEnc_complete.append(0) # unused P3 l_APDUSOSSEAESEnc_complete.append(0) # send Read APDU response, sw1, sw2 = o_deviceConnection.transmit( l_APDUSOSSEAESEnc_complete) print "data: %s" % toHexString(response) print "sw : %s" % toHexString([sw1]) + toHexString([sw2]) ################################################## # send Read Command print "Read Output" # build complete Read APDU l_APDUSOSSEGetOutput_complete = [] l_APDUSOSSEGetOutput_complete += l_APDUSOSSEGetOutput # unused P1 l_APDUSOSSEGetOutput_complete.append(0) # unused P2 l_APDUSOSSEGetOutput_complete.append(0) # output length l_APDUSOSSEGetOutput_complete.append(lengthInput) # send Read APDU ciphertext, sw1, sw2 = o_deviceConnection.transmit( l_APDUSOSSEGetOutput_complete) print "data: %s" % toHexString(ciphertext) print "sw : %s" % toHexString([sw1]) + toHexString([sw2]) ################################################## # check encryption # transform the key in binary string keyBinaryString = HexListToBinString(l_secretKey) # define size of key AES.key_size = lengthKey # create an AES engine in ECB mode o_cryptoEngine = AES.new(keyBinaryString, AES.MODE_ECB) # allocate memory for ciphertext2 ciphertext2 = [0] * lengthInput # compute the expected ciphertext ciphertext2 = BinStringToHexList( o_cryptoEngine.encrypt(HexListToBinString(l_input))) # display print "\n" print "--- AES128 encryption ---" print "plaintext : %s" % toHexString(l_input) print "key : %s" % toHexString(l_secretKey) print "mask : %s" % toHexString(l_mask) print "ciphertext: %s" % toHexString(ciphertext) print "checking : %s" % toHexString(ciphertext2) # check that ciphertext computed by the card is equal to ciphertext computed in Python if (ciphertext == ciphertext2): print "encryption ok :-)" print "\n" return 0 else: print "encryption ko :-(" print "\n" return 1
13:'Notes line 1', 14:'Notes line 2', 15:'Notes line 3', 16:'Notes line 4', } # print all enteries in personal data file send([0x00, 0xa4, 0x00, 0x0c]) send([0x00, 0xA4, 0x01, 0x0C] + [0x02, 0xEE, 0xEE]) send([0x00, 0xA4, 0x02, 0x0C, 0x02, 0x50, 0x44]) print("[+] Personal data file:") #output = output + "[+] Personal data file:\n" for i in range(16): record = send([0x00, 0xB2, (i+1), 0x04]) print "\t["+str(i+1)+"]"+table[(i+1)]+":",HexListToBinString(record).decode("cp1252").encode("utf8") # output = output + "\t["+str(i+1)+"]"+table[(i+1)]+": "+HexListToBinString(record).decode("cp1252").encode("utf8")+"\n" # reading pin retry counters in the card (EstEID spec page 26) print("[+] PIN retry counters:") #output = output + "[+] PIN retry counters:\n" send([0x00, 0xa4, 0x00, 0x0c]) send([0x00, 0xA4, 0x02, 0x0C, 0x02, 0x00, 0x16]) record = send([0x00, 0xB2, 0x01, 0x04]) print "\tPIN1:",record[5],"left" #output = output + "\tPIN1: "+str(record[5])+" left\n" record = send([0x00, 0xB2, 0x01, 0x04]) print "\tPIN2:",record[5],"left"
def photoid13(): # Reset SELECT = [0x00, 0xA4, 0x04, 0x00, 0x08, 0xA0, 0x00, 0x00, 0x00, 0x54, 0x48, 0x00, 0x01] # CID COMMAND1 = [0x80, 0xb0, 0x00, 0x04, 0x02, 0x00, 0x0d] COMMAND2 = [0x00, 0xc0, 0x00, 0x00, 0x0d] # Fullname Thai + Eng + BirthDate + Sex COMMAND3 = [0x80, 0xb0, 0x00, 0x11, 0x02, 0x00, 0xd1] COMMAND4 = [0x00, 0xc0, 0x00, 0x00, 0xd1] # Address COMMAND5 = [0x80, 0xb0, 0x15, 0x79, 0x02, 0x00, 0x64] COMMAND6 = [0x00, 0xc0, 0x00, 0x00, 0x64] # issue/expire COMMAND7 = [0x80, 0xb0, 0x01, 0x67, 0x02, 0x00, 0x12] COMMAND8 = [0x00, 0xc0, 0x00, 0x00, 0x12] # Photo_Part1-20 req1 = [0x00, 0xc0, 0x00, 0x00] CMD_PHOTO1 = [0x80, 0xb0, 0x01, 0x7B, 0x02, 0x00, 0xFF] CMD_PHOTO2 = [0x80, 0xb0, 0x02, 0x7A, 0x02, 0x00, 0xFF] CMD_PHOTO3 = [0x80, 0xb0, 0x03, 0x79, 0x02, 0x00, 0xFF] CMD_PHOTO4 = [0x80, 0xb0, 0x04, 0x78, 0x02, 0x00, 0xFF] CMD_PHOTO5 = [0x80, 0xb0, 0x05, 0x77, 0x02, 0x00, 0xFF] CMD_PHOTO6 = [0x80, 0xb0, 0x06, 0x76, 0x02, 0x00, 0xFF] CMD_PHOTO7 = [0x80, 0xb0, 0x07, 0x75, 0x02, 0x00, 0xFF] CMD_PHOTO8 = [0x80, 0xb0, 0x08, 0x74, 0x02, 0x00, 0xFF] CMD_PHOTO9 = [0x80, 0xb0, 0x09, 0x73, 0x02, 0x00, 0xFF] CMD_PHOTO10 = [0x80, 0xb0, 0x0A, 0x72, 0x02, 0x00, 0xFF] CMD_PHOTO11 = [0x80, 0xb0, 0x0B, 0x71, 0x02, 0x00, 0xFF] CMD_PHOTO12 = [0x80, 0xb0, 0x0C, 0x70, 0x02, 0x00, 0xFF] CMD_PHOTO13 = [0x80, 0xb0, 0x0D, 0x6F, 0x02, 0x00, 0xFF] CMD_PHOTO14 = [0x80, 0xb0, 0x0E, 0x6E, 0x02, 0x00, 0xFF] CMD_PHOTO15 = [0x80, 0xb0, 0x0F, 0x6D, 0x02, 0x00, 0xFF] CMD_PHOTO16 = [0x80, 0xb0, 0x10, 0x6C, 0x02, 0x00, 0xFF] CMD_PHOTO17 = [0x80, 0xb0, 0x11, 0x6B, 0x02, 0x00, 0xFF] CMD_PHOTO18 = [0x80, 0xb0, 0x12, 0x6A, 0x02, 0x00, 0xFF] CMD_PHOTO19 = [0x80, 0xb0, 0x13, 0x69, 0x02, 0x00, 0xFF] CMD_PHOTO20 = [0x80, 0xb0, 0x14, 0x68, 0x02, 0x00, 0xFF] # get all the available readers r = readers() print("Available readers:", r) # for readerIndex,readerItem in enumerate(readerList): for readerIndex,readerItem in enumerate(r): print(readerIndex, readerItem) readerSelectIndex = 0 #int(input("Select reader[0]: ") or "0") # reader = readerList[readerSelectIndex] # reader = r[readerSelectIndex] reader = r[0] print("Using:", reader) connection = reader.createConnection() connection.connect() atr = connection.getATR() print ("ATR: " + toHexString(atr)) if (atr[0] == 0x3B & atr[1] == 0x67): req = [0x00, 0xc0, 0x00, 0x01] else : req = [0x00, 0xc0, 0x00, 0x00] # Check card # data, sw1, sw2 = connection.transmit(SELECT + THAI_CARD) # print ("Select Applet: %02X %02X" % (sw1, sw2)) # print(date) # Reset , check card data, sw1, sw2 = connection.transmit(SELECT) print(data) print("Select Applet: %02X %02X" % (sw1, sw2)) data, sw1, sw2 = connection.transmit(COMMAND1) print(data) print("Command1: %02X %02X" % (sw1, sw2)) # CID # data = getData(CMD_CID, req) # data1 = getData(CMD_CID, req) # cid = thai2unicode(data1[0]) data, sw1, sw2 = connection.transmit(COMMAND2) print(data) print("testkri %s" % (data)) kid = [] for d in data: kid.append(chr(d)) print(chr(d), end=' ') print() kid.append(",") print("Command2: %02X %02X" % (sw1, sw2)) # kid2= [] #print kid # Fullname Thai + Eng + BirthDate + Sex data, sw1, sw2 = connection.transmit(COMMAND3) print(data) print("Command3: %02X %02X" % (sw1, sw2)) data, sw1, sw2 = connection.transmit(COMMAND4) print(data) for d in data: if six.PY2: kid.append(chr(d)) print(thai2unicode(chr(d)),end='') else: ktext = chr(d).encode('cp1252').decode('tis-620') kid.append(chr(d).encode('cp1252').decode('tis-620')) print( ktext , end= ' ') print() kid.append(",") print("Command4: %02X %02X" % (sw1, sw2)) # Address data, sw1, sw2 = connection.transmit(COMMAND5) print(data) print("Command5: %02X %02X" % (sw1, sw2)) data, sw1, sw2 = connection.transmit(COMMAND6) print(data) for d in data: if six.PY2: kid.append(chr(d)) print(thai2unicode(chr(d)),end='') else: ktext = chr(d).encode('cp1252').decode('tis-620') kid.append(chr(d).encode('cp1252').decode('tis-620')) print( ktext , end= ' ') print() kid.append(",") print("Command6: %02X %02X" % (sw1, sw2)) # issue/expire data, sw1, sw2 = connection.transmit(COMMAND7) print(data) print("Command7: %02X %02X" % (sw1, sw2)) data, sw1, sw2 = connection.transmit(COMMAND8) print(data) for d in data: if six.PY2: kid.append(chr(d)) print(thai2unicode(chr(d)),end='') else: ktext = chr(d).encode('cp1252').decode('tis-620') kid.append(chr(d).encode('cp1252').decode('tis-620')) print( ktext , end= ' ') print() kid.append(",") f1 = open('./kriid.txt','w+') for item in kid: # f1.write("%s" %item.strip()) f1.write("%s" %item) f1.close print("Command8: %02X %02X" % (sw1, sw2)) photo = [] data, sw1, sw2 = connection.transmit(CMD_PHOTO1) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO1[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO2) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO2[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO3) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO3[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO4) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO4[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO5) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO5[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO6) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO6[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO7) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO7[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO8) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO8[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO9) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO9[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO10) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO10[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO11) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO11[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO12) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO12[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO13) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO13[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO14) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO14[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO15) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO15[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO16) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO16[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO17) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO17[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO18) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO18[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO19) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO19[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO20) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO20[-1]]) photo += data dataa1 = HexListToBinString(photo) f1 = open('./kriid.txt','r') data=f1.readline() list=data.split(",") id13=list[0] expire=list[3] kripic =id13+expire+".jpg" f = open(kripic, "wb") if six.PY2: f.write(dataa1) else: f.write(dataa1.encode('ISO-8859-1')) # iso-8859-1 use to save impage in python3 f.flush() f.close sleep(0.1) ImageFile.LOAD_TRUNCATED_IMAGES = True f_src = open(id13+expire+".jpg",'rb') f_dest = open("images.jpg",'wb') shutil.copyfileobj(f_src,f_dest) sleep(0.1) imgkri(kripic) words = list[1].split("#") pre = words[0] name = words[1] # uniline22 = words[3].split() # surname = uniline22[0] surname = words[3].split()[0] # uniline6 = words[6].split() # uniline66 = uniline6[1] # birth = uniline66[0:8] birth = words[6].split()[1][0:8] # sex = uniline66[-1] sex = words[6].split()[1][-1] if sex=='1': kripre = '1' if sex=='2': if str(pre,"tis-620") == 'น.ส.' or str(pre,"tis-620") == 'ด.ญ.': kripre='2' else: kripre='3' words = list[2].split("#") address2 = words[0]+words[1] if six.PY2: address1 = address2.decode("tis-620").encode('UTF-8') else: address1 = address2 tumbon0 = words[5] if six.PY2: thaitumbon = tumbon0.decode("tis-620").encode('UTF-8') tumbon1 = thaitumbon.replace('ตำบล',"") # tumbon = tumbon1.encode('tis-620') tumbon = tumbon1 else: thaitumbon = tumbon0 tumbon1 = thaitumbon.replace('ตำบล',"") tumbon = tumbon1 # tumbon0 = words[5] # tumbon = tumbon0[4:30] # amphur = amphur0[5:30] # province = province0[7:30] # thaitumbon = str(tumbon0,"tis-620") amphur0 = words[6] if six.PY2: thaiamphur = amphur0.decode("tis-620").encode('UTF-8') amphur1 = thaiamphur.replace('อำเภอ',"") amphur = amphur1 else: thaiamphur = amphur0 amphur1 = thaiamphur.replace('อำเภอ',"") amphur = amphur1 province0 = words[7].strip() if six.PY2: print(province0) thaiprovince = province0.decode("tis-620").encode('UTF-8') print(thaiprovince) province1 = thaiprovince.replace('จังหวัด',"") province = province1 else: print(province0) thaiprovince = province0 province1 = thaiprovince.replace('จังหวัด',"") province = province1 f1.close csvfile = '../Dropbox/krifoxone/kriid.csv' if os.path.isfile(csvfile): f1 = open(csvfile,'w+') f1.write(id13+","+pre+name+" "+surname+","+birth+","+sex+","+address1+","+tumbon+","+amphur+","+province+","+name+","+surname+","+kripre+","+expire) f1.close sv = StringVar(root,value='เริ่ม อ่านบัตร') print("start cardreader") a=Entry(root,textvariable=sv) #creating entry box a.grid(row=5,column=1) sv1 = StringVar(root,value="HN") aa=Entry(root,textvariable=sv1) #creating entry box aa.grid(row=5,column=6) print("HN") cardname = pre+name+" "+surname if six.PY2: cardname2 = cardname.decode("tis-620").encode('UTF-8') else: cardname2 = cardname Label(root,text=cardname2).grid(row=1,column=8) #Creating label abirth = int(birth) abirth2 = abirth%100 abirth3 = int(abirth/100%100) abirth4 = int(abirth/10000%10000) Label(root,text=" ("+str(abirth4)+") "+str(abirth4-543)+"-"+str(abirth3)+"-"+str(abirth2)+" ").grid(row=5,column=8) #Creating label else: print("ไม่พบ file kriid.csv") sv = StringVar(root,value='เปิด cardkri.py ผิดที่') print("start cardreader") a=Entry(root,textvariable=sv) #creating entry box a.grid(row=5,column=1) sv1 = StringVar(root,value="HN") aa=Entry(root,textvariable=sv1) #creating entry box aa.grid(row=5,column=6) print("HN")
from smartcard.System import readers from Crypto.Cipher import AES from smartcard.util import PACK,HexListToBinString, BinStringToHexList,toBytes r=readers() print(r[0]) ##### key = "31323334353637383132333435363738" key_as_binstring = HexListToBinString( toBytes( key ) ) KEYB="90 90 90 90 90 90" READER_AUTH_APDU="FF 82 00 00 06 " ##### ##### connection=r[0].createConnection() connection.connect() ##### def load_auth_key(): data,sw1,sw2=connection.transmit(toBytes(READER_AUTH_APDU+KEYB)) #print(data,sw1,sw2,"loaded auth keys")
def decrypt_user_data(user_data): print("data",user_data) cipher = AES.new(key_as_binstring,AES.MODE_ECB ) decrypted_as_string = cipher.decrypt(HexListToBinString(user_data)) return decrypted_as_string
m = bn(m) c = pow(m, e, n) c = nb(c, (n.bit_length() + 7) / 8) return c print "[+] Generating %s-bit RSA key..." % (args.keysize) s = now() p1 = (args.keysize >> 8) & 0xff p2 = args.keysize & 0xff send([0x00, 0x02, p1, p2, 0x00]) print "[+] Key generated in %s seconds!" % (timediff(s)) print "[+] Retrieving public key..." r = send([0x00, 0x06, 0x00, 0x00, 0x00]) n = bn(HexListToBinString(r)) print "[+] n=%s" % (n) r = send([0x00, 0x04, 0x00, 0x00, 0x00]) e = bn(HexListToBinString(r)) print "[+] e=%s" % (e) print "[?] Enter message to encrypt:", m = raw_input() c = encrypt(n, e, m) print "[+] Encrypted message:", c.encode('hex') print "[+] Sending ciphertext to card..." p1 = ord(c[0]) p2 = ord(c[1]) c = c[2:] s = now()
def run_aes(l_secretKey,l_input,l_mask, direction=0): lengthKey = len(l_secretKey) lengthInput= len(l_input) lengthMask = len(l_mask) print("Set Dir") # build complete Read APDU l_APDUSOSSESetDir_complete = [] l_APDUSOSSESetDir_complete += l_APDUSOSSEAESSetDir # unused P1 l_APDUSOSSESetDir_complete.append(0) # unused P2 l_APDUSOSSESetDir_complete.append(0) # Dir length l_APDUSOSSESetDir_complete.append(0x01) # Dir value (encrypt or decrypt) l_APDUSOSSESetDir_complete.append(direction) print("send APDU: %s" % toHexString(l_APDUSOSSESetDir_complete)) if USE_LEIA == True: r = reader.send_APDU(sl.create_APDU_from_bytes(l_APDUSOSSESetDir_complete)) response = r.data sw1 = r.sw1 sw2 = r.sw2 else: response,sw1,sw2 = o_deviceConnection.transmit(l_APDUSOSSESetDir_complete) print("data: %s" % toHexString(response)) print("sw : %s" % toHexString([sw1]) + toHexString([sw2])) ################################################## # send Write Command print("Set Key") # build complete Read APDU l_APDUSOSSESetKey_complete = [] l_APDUSOSSESetKey_complete += l_APDUSOSSESetKey # unused P1 l_APDUSOSSESetKey_complete.append(0) # unused P2 l_APDUSOSSESetKey_complete.append(0) # key length l_APDUSOSSESetKey_complete.append(lengthKey) # key value l_APDUSOSSESetKey_complete += l_secretKey # send Write APDU print("send APDU: %s" % toHexString(l_APDUSOSSESetKey_complete)) if USE_LEIA == True: r = reader.send_APDU(sl.create_APDU_from_bytes(l_APDUSOSSESetKey_complete)) response = r.data sw1 = r.sw1 sw2 = r.sw2 else: response,sw1,sw2 = o_deviceConnection.transmit(l_APDUSOSSESetKey_complete) print("data: %s" % toHexString(response)) print("sw : %s" % toHexString([sw1]) + toHexString([sw2])) ################################################## # send Read Command print("Read Key") # build complete Read APDU l_APDUSOSSEGetKey_complete = [] l_APDUSOSSEGetKey_complete += l_APDUSOSSEGetKey # unused P1 l_APDUSOSSEGetKey_complete.append(0) # unused P2 l_APDUSOSSEGetKey_complete.append(0) # key length l_APDUSOSSEGetKey_complete.append(lengthKey) # send Read APDU if USE_LEIA == True: r = reader.send_APDU(sl.create_APDU_from_bytes(l_APDUSOSSEGetKey_complete)) response = r.data sw1 = r.sw1 sw2 = r.sw2 else: response,sw1,sw2 = o_deviceConnection.transmit(l_APDUSOSSEGetKey_complete) print("data: %s" % toHexString(response)) print("sw : %s" % toHexString([sw1]) + toHexString([sw2])) ################################################## # send Write Command print("Set Input") # build complete Read APDU l_APDUSOSSESetInput_complete = [] l_APDUSOSSESetInput_complete += l_APDUSOSSESetInput # unused P1 l_APDUSOSSESetInput_complete.append(0) # unused P2 l_APDUSOSSESetInput_complete.append(0) # input length l_APDUSOSSESetInput_complete.append(lengthInput) # input value l_APDUSOSSESetInput_complete += l_input # send Write APDU print("send APDU: %s" % toHexString(l_APDUSOSSESetInput_complete)) if USE_LEIA == True: r = reader.send_APDU(sl.create_APDU_from_bytes(l_APDUSOSSESetInput_complete)) response = r.data sw1 = r.sw1 sw2 = r.sw2 else: response,sw1,sw2 = o_deviceConnection.transmit(l_APDUSOSSESetInput_complete) print("data: %s" % toHexString(response)) print("sw : %s" % toHexString([sw1]) + toHexString([sw2])) ################################################## # send Read Command print("Read Input") # build complete Read APDU l_APDUSOSSEGetInput_complete = [] l_APDUSOSSEGetInput_complete += l_APDUSOSSEGetInput # unused P1 l_APDUSOSSEGetInput_complete.append(0) # unused P2 l_APDUSOSSEGetInput_complete.append(0) # input length l_APDUSOSSEGetInput_complete.append(lengthInput) # send Read APDU if USE_LEIA == True: r = reader.send_APDU(sl.create_APDU_from_bytes(l_APDUSOSSEGetInput_complete)) response = r.data sw1 = r.sw1 sw2 = r.sw2 else: response,sw1,sw2 = o_deviceConnection.transmit(l_APDUSOSSEGetInput_complete) print("data: %s" % toHexString(response)) print("sw : %s" % toHexString([sw1]) + toHexString([sw2])) ################################################## # send Write Command print("Set Mask") # build complete Read APDU l_APDUSOSSESetMask_complete = [] l_APDUSOSSESetMask_complete += l_APDUSOSSESetMask # unused P1 l_APDUSOSSESetMask_complete.append(0) # unused P2 l_APDUSOSSESetMask_complete.append(0) # mask length l_APDUSOSSESetMask_complete.append(lengthMask) # mask value l_APDUSOSSESetMask_complete += l_mask # send Write APDU print("send APDU: %s" % toHexString(l_APDUSOSSESetMask_complete)) if USE_LEIA == True: r = reader.send_APDU(sl.create_APDU_from_bytes(l_APDUSOSSESetMask_complete)) response = r.data sw1 = r.sw1 sw2 = r.sw2 else: response,sw1,sw2 = o_deviceConnection.transmit(l_APDUSOSSESetMask_complete) print("data: %s" % toHexString(response)) print("sw : %s" % toHexString([sw1]) + toHexString([sw2])) ################################################## # send Read Command print("Read Mask") # build complete Read APDU l_APDUSOSSEGetMask_complete = [] l_APDUSOSSEGetMask_complete += l_APDUSOSSEGetMask # unused P1 l_APDUSOSSEGetMask_complete.append(0) # unused P2 l_APDUSOSSEGetMask_complete.append(0) # mask length l_APDUSOSSEGetMask_complete.append(lengthMask) # send Read APDU if USE_LEIA == True: r = reader.send_APDU(sl.create_APDU_from_bytes(l_APDUSOSSEGetMask_complete)) response = r.data sw1 = r.sw1 sw2 = r.sw2 else: response,sw1,sw2 = o_deviceConnection.transmit(l_APDUSOSSEGetMask_complete) print("data: %s" % toHexString(response)) print("sw : %s" % toHexString([sw1]) + toHexString([sw2])) ################################################## # send Launch AES Command print("Launch AES 128") # build complete Read APDU l_APDUSOSSEAESEnc_complete = [] l_APDUSOSSEAESEnc_complete += l_APDUSOSSEAESEnc # unused P1 l_APDUSOSSEAESEnc_complete.append(0) # unused P2 l_APDUSOSSEAESEnc_complete.append(0) # unused P3 l_APDUSOSSEAESEnc_complete.append(0) # send Read APDU if USE_LEIA == True: r = reader.send_APDU(sl.create_APDU_from_bytes(l_APDUSOSSEAESEnc_complete)) response = r.data sw1 = r.sw1 sw2 = r.sw2 else: response,sw1,sw2 = o_deviceConnection.transmit(l_APDUSOSSEAESEnc_complete) print("data: %s" % toHexString(response)) print("sw : %s" % toHexString([sw1]) + toHexString([sw2])) ################################################## # send Read Command print("Read Output") # build complete Read APDU l_APDUSOSSEGetOutput_complete = [] l_APDUSOSSEGetOutput_complete += l_APDUSOSSEGetOutput # unused P1 l_APDUSOSSEGetOutput_complete.append(0) # unused P2 l_APDUSOSSEGetOutput_complete.append(0) # output length l_APDUSOSSEGetOutput_complete.append(lengthInput) # send Read APDU if USE_LEIA == True: r = reader.send_APDU(sl.create_APDU_from_bytes(l_APDUSOSSEGetOutput_complete)) ciphertext = r.data sw1 = r.sw1 sw2 = r.sw2 else: ciphertext,sw1,sw2 = o_deviceConnection.transmit(l_APDUSOSSEGetOutput_complete) print("data: %s" % toHexString(ciphertext)) print("sw : %s" % toHexString([sw1]) + toHexString([sw2])) ################################################## # check encryption # transform the key in binary string keyBinaryString = HexListToBinString(l_secretKey) # define size of key AES.key_size = lengthKey # create an AES engine in ECB mode o_cryptoEngine = AES.new(bytes(l_secretKey), AES.MODE_ECB) # allocate memory for ciphertext2 ciphertext2 = [0]*lengthInput # compute the expected ciphertext if direction == 0: ciphertext2 = o_cryptoEngine.encrypt(bytes(l_input)) else: ciphertext2 = o_cryptoEngine.decrypt(bytes(l_input)) # display print("\n") if direction == 0: print("--- AES128 encryption ---") else: print("--- AES128 decryption ---") print("plaintext : %s" % toHexString(l_input)) print("key : %s" % toHexString(l_secretKey)) print("mask : %s" % toHexString(l_mask)) print("ciphertext: %s" % toHexString(ciphertext)) out = "" for b in ciphertext2: out += ("%02X " % b) print("checking : "+out) # check that ciphertext computed by the card is equal to ciphertext computed in Python if(bytes(ciphertext) == ciphertext2): print("operation ok :-)") print("\n") return 0 else: print("operation ko :-(") print("\n") return 1
print("Address: " + thai2unicode(data[0])) # PHOTO photo = getData(CMD_PHOTO1, req)[0] photo += getData(CMD_PHOTO2, req)[0] photo += getData(CMD_PHOTO3, req)[0] photo += getData(CMD_PHOTO4, req)[0] photo += getData(CMD_PHOTO5, req)[0] photo += getData(CMD_PHOTO6, req)[0] photo += getData(CMD_PHOTO7, req)[0] photo += getData(CMD_PHOTO8, req)[0] photo += getData(CMD_PHOTO9, req)[0] photo += getData(CMD_PHOTO10, req)[0] photo += getData(CMD_PHOTO11, req)[0] photo += getData(CMD_PHOTO12, req)[0] photo += getData(CMD_PHOTO13, req)[0] photo += getData(CMD_PHOTO14, req)[0] photo += getData(CMD_PHOTO15, req)[0] photo += getData(CMD_PHOTO16, req)[0] photo += getData(CMD_PHOTO17, req)[0] photo += getData(CMD_PHOTO18, req)[0] photo += getData(CMD_PHOTO19, req)[0] photo += getData(CMD_PHOTO20, req)[0] data = HexListToBinString(photo) f = open(cid + ".jpg", "wb") f.write(data.encode('charmap')) f.close # Exit program sys.exit()
def photoid13(): # Reset SELECT = [ 0x00, 0xA4, 0x04, 0x00, 0x08, 0xA0, 0x00, 0x00, 0x00, 0x54, 0x48, 0x00, 0x01 ] # CID COMMAND1 = [0x80, 0xb0, 0x00, 0x04, 0x02, 0x00, 0x0d] COMMAND2 = [0x00, 0xc0, 0x00, 0x00, 0x0d] # Fullname Thai + Eng + BirthDate + Sex COMMAND3 = [0x80, 0xb0, 0x00, 0x11, 0x02, 0x00, 0xd1] COMMAND4 = [0x00, 0xc0, 0x00, 0x00, 0xd1] # Address COMMAND5 = [0x80, 0xb0, 0x15, 0x79, 0x02, 0x00, 0x64] COMMAND6 = [0x00, 0xc0, 0x00, 0x00, 0x64] # issue/expire COMMAND7 = [0x80, 0xb0, 0x01, 0x67, 0x02, 0x00, 0x12] COMMAND8 = [0x00, 0xc0, 0x00, 0x00, 0x12] # Photo_Part1-20 req1 = [0x00, 0xc0, 0x00, 0x00] CMD_PHOTO1 = [0x80, 0xb0, 0x01, 0x7B, 0x02, 0x00, 0xFF] CMD_PHOTO2 = [0x80, 0xb0, 0x02, 0x7A, 0x02, 0x00, 0xFF] CMD_PHOTO3 = [0x80, 0xb0, 0x03, 0x79, 0x02, 0x00, 0xFF] CMD_PHOTO4 = [0x80, 0xb0, 0x04, 0x78, 0x02, 0x00, 0xFF] CMD_PHOTO5 = [0x80, 0xb0, 0x05, 0x77, 0x02, 0x00, 0xFF] CMD_PHOTO6 = [0x80, 0xb0, 0x06, 0x76, 0x02, 0x00, 0xFF] CMD_PHOTO7 = [0x80, 0xb0, 0x07, 0x75, 0x02, 0x00, 0xFF] CMD_PHOTO8 = [0x80, 0xb0, 0x08, 0x74, 0x02, 0x00, 0xFF] CMD_PHOTO9 = [0x80, 0xb0, 0x09, 0x73, 0x02, 0x00, 0xFF] CMD_PHOTO10 = [0x80, 0xb0, 0x0A, 0x72, 0x02, 0x00, 0xFF] CMD_PHOTO11 = [0x80, 0xb0, 0x0B, 0x71, 0x02, 0x00, 0xFF] CMD_PHOTO12 = [0x80, 0xb0, 0x0C, 0x70, 0x02, 0x00, 0xFF] CMD_PHOTO13 = [0x80, 0xb0, 0x0D, 0x6F, 0x02, 0x00, 0xFF] CMD_PHOTO14 = [0x80, 0xb0, 0x0E, 0x6E, 0x02, 0x00, 0xFF] CMD_PHOTO15 = [0x80, 0xb0, 0x0F, 0x6D, 0x02, 0x00, 0xFF] CMD_PHOTO16 = [0x80, 0xb0, 0x10, 0x6C, 0x02, 0x00, 0xFF] CMD_PHOTO17 = [0x80, 0xb0, 0x11, 0x6B, 0x02, 0x00, 0xFF] CMD_PHOTO18 = [0x80, 0xb0, 0x12, 0x6A, 0x02, 0x00, 0xFF] CMD_PHOTO19 = [0x80, 0xb0, 0x13, 0x69, 0x02, 0x00, 0xFF] CMD_PHOTO20 = [0x80, 0xb0, 0x14, 0x68, 0x02, 0x00, 0xFF] # get all the available readers r = readers() print "Available readers:", r reader = r[0] print "Using:", reader connection = reader.createConnection() connection.connect() # Reset data, sw1, sw2 = connection.transmit(SELECT) print data print "Select Applet: %02X %02X" % (sw1, sw2) data, sw1, sw2 = connection.transmit(COMMAND1) print data print "Command1: %02X %02X" % (sw1, sw2) # CID data, sw1, sw2 = connection.transmit(COMMAND2) print data print "testkri %s" % (data) kid = [] # kid2= [] for d in data: kid.append(chr(d)) print chr(d), print #print kid kid.append(",") print "Command2: %02X %02X" % (sw1, sw2) # Fullname Thai + Eng + BirthDate + Sex data, sw1, sw2 = connection.transmit(COMMAND3) print data print "Command3: %02X %02X" % (sw1, sw2) data, sw1, sw2 = connection.transmit(COMMAND4) print data for d in data: kid.append(chr(d)) print unicode(chr(d), "tis-620"), print kid.append(",") print "Command4: %02X %02X" % (sw1, sw2) # Address data, sw1, sw2 = connection.transmit(COMMAND5) print data print "Command5: %02X %02X" % (sw1, sw2) data, sw1, sw2 = connection.transmit(COMMAND6) print data for d in data: kid.append(chr(d)) print unicode(chr(d), "tis-620"), print kid.append(",") print "Command6: %02X %02X" % (sw1, sw2) # issue/expire data, sw1, sw2 = connection.transmit(COMMAND7) print data print "Command7: %02X %02X" % (sw1, sw2) data, sw1, sw2 = connection.transmit(COMMAND8) print data for d in data: kid.append(chr(d)) print unicode(chr(d), "tis-620"), print kid.append(",") f1 = open('./kriid.txt', 'w+') for item in kid: # f1.write("%s" %item.strip()) f1.write("%s" % item) f1.close print "Command8: %02X %02X" % (sw1, sw2) photo = [] data, sw1, sw2 = connection.transmit(CMD_PHOTO1) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO1[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO2) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO2[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO3) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO3[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO4) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO4[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO5) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO5[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO6) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO6[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO7) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO7[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO8) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO8[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO9) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO9[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO10) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO10[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO11) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO11[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO12) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO12[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO13) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO13[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO14) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO14[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO15) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO15[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO16) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO16[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO17) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO17[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO18) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO18[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO19) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO19[-1]]) photo += data data, sw1, sw2 = connection.transmit(CMD_PHOTO20) data, sw1, sw2 = connection.transmit(req1 + [CMD_PHOTO20[-1]]) photo += data dataa1 = HexListToBinString(photo) f1 = open('./kriid.txt', 'r') data = f1.readline() list = data.split(",") id13 = list[0] expire = list[3] kripic = id13 + expire + ".jpg" f = open(kripic, "wb") f.write(dataa1) f.flush() f.close sleep(0.1) ImageFile.LOAD_TRUNCATED_IMAGES = True f_src = open(id13 + expire + ".jpg", 'rb') f_dest = open("images.jpg", 'wb') shutil.copyfileobj(f_src, f_dest) sleep(0.1) imgkri(kripic) words = list[1].split("#") pre = words[0] name = words[1] # uniline22 = words[3].split() # surname = uniline22[0] surname = words[3].split()[0] # uniline6 = words[6].split() # uniline66 = uniline6[1] # birth = uniline66[0:8] birth = words[6].split()[1][0:8] # sex = uniline66[-1] sex = words[6].split()[1][-1] if sex == '1': kripre = '1' if sex == '2': if unicode(pre, "tis-620") == u'น.ส.' or unicode(pre, "tis-620") == u'ด.ญ.': kripre = '2' else: kripre = '3' words = list[2].split("#") address1 = words[0] + words[1] tumbon0 = words[5] # tumbon = tumbon0[4:30] # amphur = amphur0[5:30] # province = province0[7:30] thaitumbon = unicode(tumbon0, "tis-620") tumbon1 = thaitumbon.replace(u'ตำบล', "") tumbon = tumbon1.encode('tis-620') amphur0 = words[6] thaiamphur = unicode(amphur0, "tis-620") amphur1 = thaiamphur.replace(u'อำเภอ', "") amphur = amphur1.encode('tis-620') province0 = words[7].strip() print unicode(province0, "tis-620") thaiprovince = unicode(province0, "tis-620") province1 = thaiprovince.replace(u'จังหวัด', "") province = province1.encode('tis-620') f1.close csvfile = '../Dropbox/krifoxone/kriid.csv' if os.path.isfile(csvfile): f1 = open(csvfile, 'w+') f1.write(id13 + "," + pre + name + " " + surname + "," + birth + "," + sex + "," + address1 + "," + tumbon + "," + amphur + "," + province + "," + name + "," + surname + "," + kripre + "," + expire) f1.close sv = StringVar(root, value='เริ่ม อ่านบัตร') print "start cardreader" a = Entry(root, textvariable=sv) #creating entry box a.grid(row=5, column=1) sv1 = StringVar(root, value="HN") aa = Entry(root, textvariable=sv1) #creating entry box aa.grid(row=5, column=6) print "HN" cardname = pre + name + " " + surname cardname2 = unicode(cardname, 'tis-620') Label(root, text=cardname2).grid(row=1, column=8) #Creating label abirth = int(birth) abirth2 = abirth % 100 abirth3 = int(abirth / 100 % 100) abirth4 = int(abirth / 10000 % 10000) Label(root, text=" (" + str(abirth4) + ") " + str(abirth4 - 543) + "-" + str(abirth3) + "-" + str(abirth2) + " ").grid( row=5, column=8) #Creating label else: print("ไม่พบ file kriid.csv") sv = StringVar(root, value='เปิด cardkri.py ผิดที่') print "start cardreader" a = Entry(root, textvariable=sv) #creating entry box a.grid(row=5, column=1) sv1 = StringVar(root, value="HN") aa = Entry(root, textvariable=sv1) #creating entry box aa.grid(row=5, column=6) print "HN"
data = getData(CMD_ADDRESS, req) print("Address: " + thai2unicode(data[0])) # PHOTO photo = getData(CMD_PHOTO1, req)[0] photo += getData(CMD_PHOTO2, req)[0] photo += getData(CMD_PHOTO3, req)[0] photo += getData(CMD_PHOTO4, req)[0] photo += getData(CMD_PHOTO5, req)[0] photo += getData(CMD_PHOTO6, req)[0] photo += getData(CMD_PHOTO7, req)[0] photo += getData(CMD_PHOTO8, req)[0] photo += getData(CMD_PHOTO9, req)[0] photo += getData(CMD_PHOTO10, req)[0] photo += getData(CMD_PHOTO11, req)[0] photo += getData(CMD_PHOTO12, req)[0] photo += getData(CMD_PHOTO13, req)[0] photo += getData(CMD_PHOTO14, req)[0] photo += getData(CMD_PHOTO15, req)[0] photo += getData(CMD_PHOTO16, req)[0] photo += getData(CMD_PHOTO17, req)[0] photo += getData(CMD_PHOTO18, req)[0] photo += getData(CMD_PHOTO19, req)[0] photo += getData(CMD_PHOTO20, req)[0] data = HexListToBinString(photo) f = open(cid + ".jpg", "wb") f.write(data.encode('utf-8')) f.close # Exit program sys.exit()
def read_card(self): # try: # self.connection.connect() # except NoCardException: # print("no card la") # return atr = self.connection.getATR() print("ATR: " + toHexString(atr)) if (atr[0] == 0x3B & atr[1] == 0x67): req = [0x00, 0xc0, 0x00, 0x01] else: req = [0x00, 0xc0, 0x00, 0x00] # Check card data, sw1, sw2 = self.connection.transmit(self.SELECT + self.THAI_CARD) print("Select Applet: %02X %02X" % (sw1, sw2)) # CID data = self.getData(self.CMD_CID, req) cid = self.thai2unicode(data[0]) #print ("CID: " + cid) self.card_data["nationalID"] = cid # TH Fullname data = self.getData(self.CMD_THFULLNAME, req) #print ("TH Fullname: " + self.thai2unicode(data[0])) nth = self.thai2unicode(data[0]).replace("#", " ") self.card_data["nameTH"] = nth # EN Fullname data = self.getData(self.CMD_ENFULLNAME, req) #print ("EN Fullname: " + self.thai2unicode(data[0])) nen = self.thai2unicode(data[0]).replace("#", " ") self.card_data["nameEN"] = nen # Date of birth data = self.getData(self.CMD_BIRTH, req) #print( "Date of birth: " + self.thai2unicode(data[0])) temp = self.thai2unicode(data[0]) nyear = int(temp[:4]) - 543 ndate = "{}-{}-{}".format(str(nyear), temp[4:6], temp[6:]) self.card_data["birthdate"] = ndate # Gender data = self.getData(self.CMD_GENDER, req) #print ("Gender: " + self.thai2unicode(data[0])) if (self.thai2unicode(data[0]) == "1"): gd = "male" else: gd = "female" self.card_data["gender"] = gd # Card Issuer data = self.getData(self.CMD_ISSUER, req) #print ("Card Issuer: " + self.thai2unicode(data[0])) self.card_data["issuer"] = self.thai2unicode(data[0]) # Issue Date data = self.getData(self.CMD_ISSUE, req) #print ("Issue Date: " + self.thai2unicode(data[0])) temp = self.thai2unicode(data[0]) nyear = int(temp[:4]) - 543 ndate = "{}-{}-{}".format(str(nyear), temp[4:6], temp[6:]) self.card_data["issueDate"] = ndate # Expire Date data = self.getData(self.CMD_EXPIRE, req) #print ("Expire Date: " + self.thai2unicode(data[0])) temp = self.thai2unicode(data[0]) nyear = int(temp[:4]) - 543 ndate = "{}-{}-{}".format(str(nyear), temp[4:6], temp[6:]) self.card_data["expireDate"] = ndate # Address data = self.getData(self.CMD_ADDRESS, req) #print ("Address: " + self.thai2unicode(data[0])) naddr = self.thai2unicode(data[0]).replace("#", " ") self.card_data["address"] = naddr #print (self.card_data) # PHOTO photo = self.getData(self.CMD_PHOTO1, req)[0] photo += self.getData(self.CMD_PHOTO2, req)[0] photo += self.getData(self.CMD_PHOTO3, req)[0] photo += self.getData(self.CMD_PHOTO4, req)[0] photo += self.getData(self.CMD_PHOTO5, req)[0] photo += self.getData(self.CMD_PHOTO6, req)[0] photo += self.getData(self.CMD_PHOTO7, req)[0] photo += self.getData(self.CMD_PHOTO8, req)[0] photo += self.getData(self.CMD_PHOTO9, req)[0] photo += self.getData(self.CMD_PHOTO10, req)[0] photo += self.getData(self.CMD_PHOTO11, req)[0] photo += self.getData(self.CMD_PHOTO12, req)[0] photo += self.getData(self.CMD_PHOTO13, req)[0] photo += self.getData(self.CMD_PHOTO14, req)[0] photo += self.getData(self.CMD_PHOTO15, req)[0] photo += self.getData(self.CMD_PHOTO16, req)[0] photo += self.getData(self.CMD_PHOTO17, req)[0] photo += self.getData(self.CMD_PHOTO18, req)[0] photo += self.getData(self.CMD_PHOTO19, req)[0] photo += self.getData(self.CMD_PHOTO20, req)[0] data = HexListToBinString(photo) data = data.encode('iso-8859-1') print(data) with open('card_image.jpg', 'wb') as image_file: image_file.write(data) self.card_data["idCardPhoto"] = data #f = open(cid + ".jpg", "wb") #f.write (data) #f.close #Test reading card #c = cardreader() #c.read_card() # Exit program #sys.exit()
print "Error during selection of applet" sys.exit(-1) pin = BinStringToHexList(str(getpass("Enter pin:"))) data, sw1, sw2 = passmg.authorize(pin) if (sw1 != 0x90 or sw2 != 0x00): print "Authorisation failed" sys.exit(-1) if args.read: data, sw1, sw2 = passmg.read_password(args.read) name_len = data[0] username_len = data[1] password_len = data[2] print("Name: {}").format(HexListToBinString(data[3:3 + name_len])) print('Username: {}').format( HexListToBinString(data[3 + name_len:3 + name_len + username_len])) print('Password: {}').format( HexListToBinString(data[3 + name_len + username_len:3 + name_len + username_len + password_len])) if (sw1 != 0x90 and sw2 != 0x00): print("Error: sw1: {} sw2: {}").format(sw1, sw2) elif args.add: name = raw_input("Enter name of password: "******"Enter username: "******"Enter password: "******"Reenter password: ")) if password == password2: data, sw1, sw2 = passmg.add_password(name, user_name, password) if (sw1 != 0x90 and sw2 != 0x00):