本示例使用設備:https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.6781645eXF3tm5&ft=t&id=959258149468
一、密鑰認證
procedure TForm1.Button21Click(Sender: TObject);
varctrlword:byte;passwordid:byte; //密鑰類型status:byte; //存放返回值authkeybuf:array[0..3] of byte;uidbuf:array[0..7] of byte; //UIDcardstr:string;
begincardstr:=trim(edit4.Text);if checkuidstr(cardstr ,8,uidbuf)=false then //檢測數據是否合法并放入數組緩沖beginApplication.MessageBox('需要操作的卡片UID錯誤,可先執行尋卡操作獲取UID!', '提示', MB_OK+MB_ICONSTOP);edit4.SetFocus ;exit;end;if checkuidstr(Trim(Edit5.Text) ,4,authkeybuf)=false then //檢測數據是否合法并放入數組緩沖beginApplication.MessageBox('卡片認證密鑰輸入錯誤,請輸入8位16進制卡認證密鑰!', '提示', MB_OK+MB_ICONSTOP);edit5.SetFocus ;exit;end;if ComboBox2.ItemIndex =4 thenpasswordid:=$10else if ComboBox2.ItemIndex =3 thenpasswordid:=$8else if ComboBox2.ItemIndex =2 thenpasswordid:=$4else if ComboBox2.ItemIndex =1 thenpasswordid:=$2else passwordid:=$1;ctrlword:=$4;status:=iso15693authkey(ctrlword,@uidbuf,passwordid,@authkeybuf);case status of0:beginpcdbeep(50);Application.MessageBox(PAnsiChar(AnsiString('UID '+cardstr+' 密鑰認證成功!')), '提示', MB_OK+MB_ICONINFORMATION);end;8:Application.MessageBox('異常代碼:8,卡不在感應區或指令UID不匹配該卡!', '提示', MB_OK+MB_ICONSTOP);20:Application.MessageBox('異常代碼:20,讀寫器不支持該指令!', '提示', MB_OK+MB_ICONSTOP);47:Application.MessageBox('異常代碼:47,該卡不支持加密功能,或加密功能未啟用,或密碼錯誤(該卡將不能再響應任何指令,只能拿開卡再重放)!', '提示', MB_OK+MB_ICONSTOP);else MessageDispInfo(status);end;
end;
?二、更改密鑰
procedure TForm1.Button22Click(Sender: TObject);
varctrlword:byte;passwordid:byte; //密鑰類型status:byte; //存放返回值newkeybuf:array[0..3] of byte;uidbuf:array[0..7] of byte; //UIDcardstr:string;
begincardstr:=trim(edit4.Text);if checkuidstr(cardstr ,8,uidbuf)=false then //檢測數據是否合法并放入數組緩沖beginApplication.MessageBox('需要操作的卡片UID錯誤,可先執行尋卡操作獲取UID!', '提示', MB_OK+MB_ICONSTOP);edit4.SetFocus ;exit;end;if checkuidstr(Trim(Edit10.Text) ,4,newkeybuf)=false then //檢測數據是否合法并放入數組緩沖beginApplication.MessageBox('卡片新密鑰輸入錯誤,請輸入8位16進制新密鑰!', '提示', MB_OK+MB_ICONSTOP);edit10.SetFocus ;exit;end;if ComboBox2.ItemIndex =4 thenpasswordid:=$10else if ComboBox2.ItemIndex =3 thenpasswordid:=$8else if ComboBox2.ItemIndex =2 thenpasswordid:=$4else if ComboBox2.ItemIndex =1 thenpasswordid:=$2else passwordid:=$1;ctrlword:=$4;status:=iso15693writekey(ctrlword,@uidbuf,passwordid,@newkeybuf);case status of0:beginpcdbeep(50);Application.MessageBox(PAnsiChar(AnsiString('UID '+cardstr+' 更改卡密鑰成功!')), '提示', MB_OK+MB_ICONINFORMATION);end;8:Application.MessageBox('異常代碼:8,卡不在感應區或指令UID不匹配該卡!', '提示', MB_OK+MB_ICONSTOP);20:Application.MessageBox('異常代碼:20,讀寫器不支持該指令!', '提示', MB_OK+MB_ICONSTOP);47:Application.MessageBox('異常代碼:47,上一條指令必須是認證密碼指令......!', '提示', MB_OK+MB_ICONSTOP);else MessageDispInfo(status);end;
end;
三、設置密鑰保護塊及密鑰認證方式
procedure TForm1.Button23Click(Sender: TObject);
varctrlword:byte;blockpoint,extprotectstatus:byte; //高低塊分隔地址,配置值status:byte; //存放返回值uidbuf:array[0..7] of byte; //UIDcardstr:string;
begincardstr:=trim(edit4.Text);if checkuidstr(cardstr ,8,uidbuf)=false then //檢測數據是否合法并放入數組緩沖beginApplication.MessageBox('需要操作的卡片UID錯誤,可先執行尋卡操作獲取UID!', '提示', MB_OK+MB_ICONSTOP);edit4.SetFocus ;exit;end;if(not isrightint(edit11.Text)) thenbeginApplication.MessageBox('分隔高低塊的塊地址輸入錯誤!', '警告', MB_OK+MB_ICONSTOP);edit11.SetFocus();edit11.SelectAll();exit;endelse blockpoint:=strtoint(edit11.Text);extprotectstatus:=ComboBox3.ItemIndex +ComboBox4.ItemIndex *16;ctrlword:=$4;status:=iso15693protectblock(ctrlword,@uidbuf,blockpoint,extprotectstatus);case status of0:beginpcdbeep(50);Application.MessageBox(PAnsiChar(AnsiString('UID '+cardstr+' 塊密碼保護設定成功!')), '提示', MB_OK+MB_ICONINFORMATION);end;8:Application.MessageBox('異常代碼:8,卡不在感應區或指令UID不匹配該卡!', '提示', MB_OK+MB_ICONSTOP);20:Application.MessageBox('異常代碼:20,讀寫器不支持該指令!', '提示', MB_OK+MB_ICONSTOP);47:Application.MessageBox('異常代碼:47,必須都認證讀取密碼和寫入密碼成功后再進行塊密碼保護設定!', '提示', MB_OK+MB_ICONSTOP);else MessageDispInfo(status);end;
end;
四、開啟隱私保護
procedure TForm1.Button24Click(Sender: TObject);
varctrlword:byte;status:byte; //存放返回值password:array[0..3] of byte;uidbuf:array[0..7] of byte; //UIDcardstr:string;
begincardstr:=trim(edit4.Text);if checkuidstr(cardstr ,8,uidbuf)=false then //檢測數據是否合法并放入數組緩沖beginApplication.MessageBox('需要操作的卡片UID錯誤,可先執行尋卡操作獲取UID!', '提示', MB_OK+MB_ICONSTOP);edit4.SetFocus ;exit;end;if checkuidstr(Trim(Edit5.Text) ,4,password)=false then //檢測數據是否合法并放入數組緩沖beginApplication.MessageBox('卡片隱私密鑰輸入錯誤,請輸入8位16進制卡認證隱私密鑰!', '提示', MB_OK+MB_ICONSTOP);edit5.SetFocus ;exit;end;if Application.MessageBox('卡片啟用隱私保護后將不再響應除認證隱私密鑰的任何指令,確認您已記住此卡的UID 及 隱私密鑰,否則此卡將報廢!', '嚴重警告', MB_OKCANCEL+MB_ICONQUESTION) <>1 then exit;ctrlword:=$4;status:=iso15693privacyen(ctrlword,@uidbuf,@password);case status of0:beginpcdbeep(50);Application.MessageBox(PAnsiChar(AnsiString('UID '+cardstr+' 啟用隱私保護成功,此卡不再響應除認證隱私密碼的任何指令,該卡必須 驗證隱私密碼 才能恢復非隱私狀態!')), '提示', MB_OK+MB_ICONINFORMATION);end;8:Application.MessageBox('異常代碼:8,卡不在感應區或指令UID不匹配該卡!', '提示', MB_OK+MB_ICONSTOP);20:Application.MessageBox('異常代碼:20,讀寫器不支持該指令!', '提示', MB_OK+MB_ICONSTOP);47:Application.MessageBox('異常代碼:47,該卡不支持加密功能,或加密功能未啟用,或密碼錯誤(該卡將不能再響應任何指令,只能拿開卡再重放)!', '提示', MB_OK+MB_ICONSTOP);else MessageDispInfo(status);end;
end;
五、讀取塊內數據
procedure TForm1.Button16Click(Sender: TObject);
vari:byte;status:byte;//存放返回值flags:byte;//控制標志startblock:byte; //起始扇區blocknum:byte; //讀扇區數revlen:array[0..1] of byte; //長度uidbuf:array[0..7] of byte;//UIDmypiccdata:array[0..60] of byte;//每次讀卡數據緩沖readbuf:array[0..8000] of byte;//全部卡數據緩沖j,p:integer;cardstr,carddatahex:string;begincardstr:=trim(edit4.Text);if checkuidstr(cardstr ,8,uidbuf)=false then //檢測數據是否合法并放入數組緩沖beginApplication.MessageBox('需要操作的卡片UID錯誤,可先執行尋卡操作獲取UID!', '提示', MB_OK+MB_ICONSTOP);edit4.SetFocus ;exit;end;edit9.Text :='22';flags := strtoint('$'+edit9.Text);//操作標志if(not isrightint(edit2.Text)) thenbeginApplication.MessageBox('讀寫起始塊地址輸入錯誤!', '警告', MB_OK+MB_ICONSTOP);edit2.SetFocus();edit2.SelectAll();exit;endelse startblock:=strtoint(edit2.Text);if(not isrightint(edit3.Text)) thenbeginApplication.MessageBox('讀寫塊數輸入錯誤!', '警告', MB_OK+MB_ICONSTOP);edit3.SetFocus();edit3.SelectAll();exit;endelse blocknum:=strtoint(edit3.Text );memo1.Text :='';j:=0;carddatahex:='';while j<blocknum dobeginstatus := iso15693readblock(flags,startblock+j,1,@uidbuf,@revlen,@mypiccdata); //讀一個塊數據if status=0 then //讀卡成功,獲取數據,指針加1beginfor i:=0 to revlen[0]-1 docarddatahex:=carddatahex+inttohex(mypiccdata[i],2)+' ';j:=j+1;endelse //讀卡失敗,指針跳到最后退出j:=blocknum;end;if status=0 thenbeginpcdbeep(50);memo1.Text:=carddatahex;if revlen[0]=8 then //根據讀一塊返回的字節數預判是什么類型的卡ComboBox1.ItemIndex:=1else if revlen[0]=32 thenComboBox1.ItemIndex:=2else ComboBox1.ItemIndex:=0;endelse if status=46 thenApplication.MessageBox('請先認證相應的密碼!', '提示', MB_OK+MB_ICONSTOP)elseMessageDispInfo(status);
end;
六、寫數據到塊內
procedure TForm1.Button17Click(Sender: TObject);
vari,j,p:integer;status:byte; //存放返回值flags:byte; //控制標志startblock:byte; //起始扇區blocknum:byte; //讀扇區數writelen:integer; //長度uidbuf:array[0..7] of byte;//UIDwritebuf:array[0..1024] of byte;//卡數據緩沖mypiccdata:array[0..31] of byte;writestr:string;cardstr:string;eachblocksize:integer;beginif ComboBox1.ItemIndex=0 theneachblocksize:=4else if ComboBox1.ItemIndex=1 theneachblocksize:=8else eachblocksize:=32;cardstr:=trim(edit4.Text);if checkuidstr(cardstr ,8,uidbuf)=false then //檢測數據是否合法并放入數組緩沖beginApplication.MessageBox('需要操作的卡片UID錯誤,可先執行尋卡操作獲取UID!', '提示', MB_OK+MB_ICONSTOP);edit4.SetFocus ;exit;end;if(not isrightint(edit2.Text)) thenbeginApplication.MessageBox('讀寫起始塊地址輸入錯誤!', '警告', MB_OK+MB_ICONSTOP);edit2.SetFocus();edit2.SelectAll();exit;endelse startblock:=strtoint(edit2.Text);if(not isrightint(edit3.Text)) thenbeginApplication.MessageBox('讀寫塊數輸入錯誤!', '警告', MB_OK+MB_ICONSTOP);edit3.SetFocus();edit3.SelectAll();exit;endelse blocknum:=strtoint(edit3.Text );writelen:=blocknum*eachblocksize;if checkuidstr(trim(Memo1.Text) ,writelen,writebuf)=false then //檢測數據是否合法并放入數組緩沖beginApplication.MessageBox('需要寫入的數據輸入錯誤,可先執行讀卡操作!', '提示', MB_OK+MB_ICONSTOP);Memo1.SetFocus ;exit;end;edit9.Text :='22';flags := strtoint('$'+edit9.Text);//操作標志j:=0;while j<blocknum dobeginfor p:=0 to eachblocksize-1 do mypiccdata[p]:=writebuf[j*eachblocksize+p];status := iso15693writeblock(flags,startblock+j,1,@uidbuf,eachblocksize,@mypiccdata);if status=0 then //寫卡成功,指針加1j:=j+1else //寫卡失敗,指針跳到最后退出j:=blocknum;end;if status=0 thenbeginpcdbeep(50);Application.MessageBox(PAnsiChar(AnsiString(cardstr+' 號卡寫卡成功!')), '提示', MB_OK+MB_ICONINFORMATION);endelse if status=46 thenApplication.MessageBox('請先認證相應的密碼!', '提示', MB_OK+MB_ICONSTOP)else MessageDispInfo(status);
end;