Delphi读写Mifare Desfire Ev1 Ev2 EV3卡修改AES密钥源码
本示例使用的发卡器:https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.52de2c1b3bN1Vu&ft=t&id=917152255720
unit Unit2;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls,declaredll,strutils, ComCtrls, ExtCtrls;typeTForm2 = class(TForm)Label2: TLabel;Label4: TLabel;Button4: TButton;Button5: TButton;Button10: TButton;Button16: TButton;Edit6: TEdit;Button1: TButton;Label1: TLabel;Label3: TLabel;Edit1: TEdit;Button2: TButton;Button3: TButton;Button6: TButton;Label5: TLabel;Label6: TLabel;Label7: TLabel;ComboBox4: TComboBox;ComboBox1: TComboBox;ComboBox2: TComboBox;Label8: TLabel;Label9: TLabel;Label10: TLabel;Label11: TLabel;RichEdit2: TRichEdit;UpDown1: TUpDown;Edit4: TEdit;Edit2: TEdit;Edit3: TEdit;Button7: TButton;Button8: TButton;Label12: TLabel;ComboBox3: TComboBox;Label13: TLabel;ComboBox5: TComboBox;Label14: TLabel;ComboBox6: TComboBox;Label15: TLabel;ComboBox7: TComboBox;Label16: TLabel;Button9: TButton;Panel2: TPanel;Label18: TLabel;Button12: TButton;Panel1: TPanel;Label17: TLabel;Label19: TLabel;Label20: TLabel;Button11: TButton;Button13: TButton;Button14: TButton;Button15: TButton;Edit5: TEdit;Label21: TLabel;Label22: TLabel;Label23: TLabel;Label24: TLabel;Label25: TLabel;Label26: TLabel;ComboBox8: TComboBox;ComboBox9: TComboBox;ComboBox10: TComboBox;ComboBox11: TComboBox;ComboBox12: TComboBox;ComboBox13: TComboBox;UpDown3: TUpDown;Edit7: TEdit;Button17: TButton;Button18: TButton;Button19: TButton;Button20: TButton;Button21: TButton;Label27: TLabel;Edit8: TEdit;UpDown2: TUpDown;Label28: TLabel;Edit9: TEdit;UpDown4: TUpDown;Label29: TLabel;ComboBox14: TComboBox;Label30: TLabel;ComboBox15: TComboBox;Label31: TLabel;ComboBox16: TComboBox;Label32: TLabel;ComboBox17: TComboBox;Label33: TLabel;ComboBox18: TComboBox;Label34: TLabel;RadioButton1: TRadioButton;RadioButton2: TRadioButton;Label35: TLabel;Edit10: TEdit;UpDown5: TUpDown;Label36: TLabel;Edit11: TEdit;UpDown6: TUpDown;Button22: TButton;Button23: TButton;Button24: TButton;Button25: TButton;Button26: TButton;Memo1: TMemo;
// UpDown2: TUpDown;procedure Button4Click(Sender: TObject);procedure Button5Click(Sender: TObject);procedure Button16Click(Sender: TObject);procedure Button10Click(Sender: TObject);procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);procedure ComboBox4Change(Sender: TObject);procedure ComboBox1Change(Sender: TObject);procedure Button6Click(Sender: TObject);procedure Button3Click(Sender: TObject);procedure Button7Click(Sender: TObject);procedure Button8Click(Sender: TObject);procedure Button9Click(Sender: TObject);procedure Button11Click(Sender: TObject);procedure Button13Click(Sender: TObject);procedure Button14Click(Sender: TObject);procedure Button15Click(Sender: TObject);procedure Button17Click(Sender: TObject);procedure Button12Click(Sender: TObject);procedure Button18Click(Sender: TObject);procedure Button19Click(Sender: TObject);procedure Button20Click(Sender: TObject);procedure Button21Click(Sender: TObject);procedure Button24Click(Sender: TObject);procedure Button22Click(Sender: TObject);procedure Button23Click(Sender: TObject);procedure Button25Click(Sender: TObject);procedure Button26Click(Sender: TObject);private{ Private declarations }public{ Public declarations }end;varForm2: TForm2;MB_ICONQUESTION MB_ICONEXCLAMATION MB_ICONWARNING MB_ICONINFORMATION MB_ICONASTERISK MB_ICONHAND MB_ICONERROR MB_ICONSTOP
implementation{$R *.dfm}
procedure MessageDispInfo(Errcode:byte); //函数返回代码说明
begincase Errcode of0: Application.MessageBox ('操作成功!', '提示', MB_OK+MB_ICONINFORMATION); //MB_ICONQUESTION MB_ICONEXCLAMATION MB_ICONWARNING MB_ICONINFORMATION MB_ICONASTERISK MB_ICONHAND MB_ICONERROR MB_ICONSTOP8: Application.MessageBox('请重新拿开卡后再放到感应区!', '提示', MB_OK+MB_ICONINFORMATION);21: Application.MessageBox('没有动态库!', '提示', MB_OK+MB_ICONINFORMATION);22: Application.MessageBox('动态库或驱动程序异常!', '提示', MB_OK+MB_ICONINFORMATION);23: Application.MessageBox('驱动程序错误或尚未安装!', '提示', MB_OK+MB_ICONINFORMATION);24: Application.MessageBox('操作超时,一般是动态库没有反映!', '提示', MB_OK+MB_ICONINFORMATION);25: Application.MessageBox('发送字数不够!', '提示', MB_OK+MB_ICONINFORMATION);26: Application.MessageBox('发送的CRC错!', '提示', MB_OK+MB_ICONINFORMATION);27: Application.MessageBox('接收的字数不够!', '提示', MB_OK+MB_ICONINFORMATION);28: Application.MessageBox('接收的CRC错!', '提示', MB_OK+MB_ICONINFORMATION);50:Application.MessageBox('RATS错误,厂家调试代码,用户不需理会!', '提示', MB_OK+MB_ICONINFORMATION);51:Application.MessageBox('PPS错误,厂家调试代码,用户不需理会!', '提示', MB_OK+MB_ICONINFORMATION);52:Application.MessageBox('已进入了14443-4协议状态,可进行CPU卡功能所有操作了!', '提示', MB_OK+MB_ICONINFORMATION);53:Application.MessageBox('CPU卡功能通讯错误!', '提示', MB_OK+MB_ICONINFORMATION);54:Application.MessageBox('数据不足,需要接着发送未完成的数据至卡上!', '提示', MB_OK+MB_ICONINFORMATION);55:Application.MessageBox('发送ACK指令给卡,让卡接着发送数据回来!', '提示', MB_OK+MB_ICONINFORMATION);56:Application.MessageBox('清空根目录失败!', '提示', MB_OK+MB_ICONINFORMATION);57:Application.MessageBox('卡片不支持功能!', '提示', MB_OK+MB_ICONINFORMATION);58:Application.MessageBox('卡片初始化失败!', '提示', MB_OK+MB_ICONINFORMATION);59:Application.MessageBox('分配的空间不足!', '提示', MB_OK+MB_ICONINFORMATION);60:Application.MessageBox('本次操作的实体已存在!', '提示', MB_OK+MB_ICONINFORMATION);61:Application.MessageBox('无足够空间!', '提示', MB_OK+MB_ICONINFORMATION);62:Application.MessageBox('文件不存在!', '提示', MB_OK+MB_ICONINFORMATION);63:Application.MessageBox('权限不足,有可能是用只读密码认证,导致无法更改读写密码或无法写文件!', '提示', MB_OK+MB_ICONINFORMATION);64:Application.MessageBox('密码不存在,或密钥文件未创建!', '提示', MB_OK+MB_ICONINFORMATION);65:Application.MessageBox('传送长度错误!', '提示', MB_OK+MB_ICONINFORMATION);66:Application.MessageBox('Le错误,即接收的数据长度指定过大!', '提示', MB_OK+MB_ICONINFORMATION);67:Application.MessageBox('功能不支持或卡中无MF 或卡片已锁定!', '提示', MB_OK+MB_ICONINFORMATION);68:Application.MessageBox('密码认证错识次数过多,该密码已被锁死!', '提示', MB_OK+MB_ICONINFORMATION);86:Application.MessageBox('更改后的密码长度必须和创建时的长度一致!', '提示', MB_OK+MB_ICONINFORMATION);87:Application.MessageBox('应用目录不存在!', '提示', MB_OK+MB_ICONINFORMATION);88:Application.MessageBox('应用文件不存在!', '提示', MB_OK+MB_ICONINFORMATION);89:Application.MessageBox('文件号不能超过5!', '提示', MB_OK+MB_ICONINFORMATION);90:Application.MessageBox('读取文件时返回的长度不足,数据可能不正确!', '提示', MB_OK+MB_ICONINFORMATION);91:Application.MessageBox('一次读文件的长度不能超过255!', '提示', MB_OK+MB_ICONINFORMATION);92:Application.MessageBox('一次写文件的长度不能超过247!', '提示', MB_OK+MB_ICONINFORMATION);70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85:Application.MessageBox(PAnsiChar(AnsiString('密码认证错误,剩余认证次数为:'+inttostr(Errcode-70)+',如果为0,该密码将锁死,无法再认证!')), '提示', MB_OK+MB_ICONINFORMATION);elseApplication.MessageBox(PAnsiChar(AnsiString('异常错误代码:'+inttostr(Errcode))), '提示', MB_OK+MB_ICONINFORMATION);end;
end;function isrightint(textls:string):boolean;stdcall; //判断有无非法字符
begintryif(strtoint(textls) =0) thenbeginend;result := True;exceptresult := False;exit;end;
end;function checkhexstr(inputstr:string;hexlen:integer;var databuf:array of byte):boolean;stdcall;
var
i:integer;
begintryinputstr:=StringReplace(inputstr, ' ', '', [rfReplaceAll]);inputstr:=StringReplace(inputstr, '#$D', '', [rfReplaceAll]);inputstr:=StringReplace(inputstr, '#$A', '', [rfReplaceAll]);for i:=0 to hexlen-1 do databuf[i]:=strtoint('$'+midstr(inputstr,i*2+1,2));result := True;exceptresult := False;end;
end;function RetTextFromStr(inputstr:string):string;stdcall;
var
i:integer;
beginRetTextFromStr:='';if inputstr='9000' thenRetTextFromStr := '成功!'else if inputstr='9100' thenbeginpcdbeep(20);RetTextFromStr := '成功!';endelse if inputstr='6281' thenRetTextFromStr := '回送的数据可能错误!'else if inputstr='6283' thenRetTextFromStr := '选择文件无效,文件或密钥校验错误!'else if inputstr='6400' thenRetTextFromStr := '状态标志未改变!'else if inputstr='6581' thenRetTextFromStr := '写 EEPROM 不成功!'else if inputstr='6700' thenRetTextFromStr := '长度错误!'else if inputstr='6900' thenRetTextFromStr := 'CLA 与线路保护要求不匹配!'else if inputstr='6901' thenRetTextFromStr := '无效的状态!'else if inputstr='6981' thenRetTextFromStr := '命令与文件结构不相容!'else if inputstr='6982' thenRetTextFromStr := '不满足安全状态!'else if inputstr='6983' thenRetTextFromStr := '密钥被锁死!'else if inputstr='6984' thenRetTextFromStr := 'MAC格式不符合!'else if inputstr='6985' thenRetTextFromStr := '使用条件不满足!'else if inputstr='6986' thenRetTextFromStr := '请先选择文件!'else if inputstr='6987' thenRetTextFromStr := '无安全报文!'else if inputstr='6988' thenRetTextFromStr := '安全报文数据项不正确!'else if inputstr='6A80' thenRetTextFromStr := '数据域参数错误!'else if inputstr='6A81' thenRetTextFromStr := '功能不支持或卡中无MF 或卡片已锁定!'else if inputstr='6A82' thenRetTextFromStr := '文件未找到!'else if inputstr='6A83' thenRetTextFromStr := '记录未找到!'else if inputstr='6A84' thenRetTextFromStr := '文件无足够空间!'else if inputstr='6A86' thenRetTextFromStr := '参数P1 P2 错!'else if inputstr='6A88' thenRetTextFromStr := '密钥未找到!'else if inputstr='6B00' thenRetTextFromStr := '在达到Le/Lc 字节之前文件结束,偏移量错误!'else if inputstr='6E00' thenRetTextFromStr := '无效的CLA!'else if inputstr='6F00' thenRetTextFromStr := '数据无效!'else if inputstr='9302' thenRetTextFromStr := 'MAC 错误!'else if inputstr='9303' thenRetTextFromStr := '应用已被锁定!'else if inputstr='9401' thenRetTextFromStr := '金额不足!'else if inputstr='9403' thenRetTextFromStr := '密钥未找到!'else if inputstr='9406' thenRetTextFromStr := '所需的MAC 不可用!'else if inputstr='91AE' thenRetTextFromStr := '认证失败,请检查命行的参数和前期计算是否错误!'else if inputstr='91CA' thenRetTextFromStr := '上一个命令未完全完成!'else if inputstr='917E' thenRetTextFromStr := '指令长度错误!'else if inputstr='9140' thenRetTextFromStr := '当前目录或应用密钥不存在,请先选择正确的目录或应用!'else if inputstr='919D' thenRetTextFromStr := '处于未验证密码的状态,该指令无法操作!'else if inputstr='911E' thenRetTextFromStr := 'MAC错误!'else if inputstr='91F0' thenRetTextFromStr := '该文件号不存在!'else if inputstr='919E' thenRetTextFromStr := '参数无效!'else if inputstr='91BE' thenRetTextFromStr := '试图读取/写入的数据超出文件/记录的边界!'else if inputstr='91A0' thenRetTextFromStr := '请求的 AID 不存在!'elsebeginif midstr(inputstr,1,3)='63C' thenbegini:=strtoint('$'+midstr(inputstr,3,1));if i>0 thenRetTextFromStr := '再试 '+inttostr(i)+' 次错误密码将锁定!'elseRetTextFromStr := '密码已被锁定!' ;endelseRetTextFromStr:='未知的异常';end;end;procedure TForm2.Button4Click(Sender: TObject);
beginpcdbeep(50);
end;procedure TForm2.Button5Click(Sender: TObject);
vardevno:array[0..3] of byte;//设备编号status:byte;
beginstatus:=pcdgetdevicenumber(@devno);if status = 0 thenbeginpcdbeep(50);Application.MessageBox(PAnsiChar(AnsiString('设备编号:'+IntToStr(devno[0]) + '-' + IntToStr(devno[1]) + '-' + IntToStr(devno[2]) + '-' + IntToStr(devno[3]))), '提示', MB_OK+MB_ICONINFORMATION);endelse MessageDispInfo(status);end;procedure TForm2.Button16Click(Sender: TObject);
varAtqaSak:array[0..2] of byte;retsw:array[0..1] of byte;versionbuf:array[0..100] of byte;versionlen:array[0..1] of byte;cardtypestr:array[0..1024] of byte;stratqa:string;strsak:string;status:byte;strcardtype:string;i,verlen:integer;strversion:string;beginfor i:=Low(cardtypestr) to High(cardtypestr) do cardtypestr[i]:=0;for i:=Low(versionbuf) to High(versionbuf) do versionbuf[i]:=0;for i:=Low(AtqaSak) to High(AtqaSak) do AtqaSak[i]:=0;for i:=Low(retsw) to High(retsw) do retsw[i]:=0;for i:=Low(versionlen) to High(versionlen) do versionlen[i]:=0;status:=getmifareversion(@cardtypestr,@AtqaSak,@versionbuf,@versionlen,@retsw);if status=0 thenbeginpcdbeep(20);strcardtype:='';for i:=Low(cardtypestr) to High(cardtypestr) do strcardtype:=strcardtype+Chr(cardtypestr[i]);strversion:='';verlen:= versionlen[0]+versionlen[1]*256;if verlen>0 thenbeginstrversion:=',Version:';for i:=0 to verlen-1 do strversion:=strversion+inttohex(versionbuf[i],2);end;stratqa:=inttohex(AtqaSak[0],2)+inttohex(AtqaSak[1],2);strsak:=inttohex(AtqaSak[2],2);Application.MessageBox(PAnsiChar(AnsiString('ATQA:'+stratqa+',SAK:'+strsak+',型号:'+Trim(strcardtype)+strversion)), '提示', MB_OK+MB_ICONINFORMATION);endelsebeginMessageDispInfo(status);end;
end;procedure TForm2.Button10Click(Sender: TObject);
varstatus:byte;//存放返回值mypiccserial:array[0..6] of byte;//卡序列号myparam:array[0..3] of byte;AtqaSak:array[0..2] of byte;myver:array[0..0] of byte;mycode:array[0..0] of byte;cardnumber:Longword;//Longword为无符号32bit的整型i:integer;cardhohex,parastr,verstr,codestr:string;beginstatus := cpurequest1(@mypiccserial,@myparam,@myver,@mycode,@AtqaSak);if (status = 0) or (status =52) thenbeginpcdbeep(20);if(AtqaSak[0] div 64 >0) thenbegincardhohex:='';for i:=0 to 6 do cardhohex:=cardhohex+IntToHex(mypiccserial[i],2);parastr:='';for i:=0 to 3 do parastr:=parastr+IntToHex(myparam[i],2);verstr:=IntToHex(myver[0],2);codestr:=IntToHex(mycode[0],2);edit6.Text :=cardhohex;Application.MessageBox(PAnsiChar(AnsiString('激活Desfire卡成功,可以接着重复操作第二步进行调试了。' + #10+'16进制卡号:' + cardhohex + #10+ '参数:' + parastr + #10+'版本信息:' + verstr + #10 + '厂商代码(复旦为90):' + codestr)), '提示', MB_OK+MB_ICONINFORMATION);endelsebegincardhohex:='';for i:=0 to 3 do cardhohex:=cardhohex+IntToHex(mypiccserial[i],2);parastr:='';for i:=0 to 3 do parastr:=parastr+IntToHex(myparam[i],2);verstr:=IntToHex(myver[0],2);codestr:=IntToHex(mycode[0],2);edit6.Text :=cardhohex;Application.MessageBox(PAnsiChar(AnsiString('激活Fm1208CPU卡成功,可以接着重复操作第二步进行调试了。' + #10+'16进制卡号:' + cardhohex + #10+ '参数:' + parastr + #10+'版本信息:' + verstr + #10 + '厂商代码(复旦为90):' + codestr)), '提示', MB_OK+MB_ICONINFORMATION);end;endelse MessageDispInfo(status);
end;procedure TForm2.Button1Click(Sender: TObject);
varaidbuf:array[0..2] of byte;retsw:array[0..1] of byte;retstr:string;status:byte;beginif checkhexstr(Edit1.Text ,3,aidbuf)=false then //检测AID是否合法并放入数组缓冲beginApplication.MessageBox('十六进制AID输入错误,请输入3字节的16进制AID!', '提示', MB_OK+MB_ICONSTOP);Edit1.SetFocus ;exit;end;status := desfireselectapplication(@aidbuf, @retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status>0 thenApplication.MessageBox(PAnsiChar(AnsiString('选择卡内应用AID操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)elseApplication.MessageBox(PAnsiChar(AnsiString('选择卡内应用AID操作卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;procedure TForm2.Button2Click(Sender: TObject);
varauthkeybuf:array[0..23] of byte;retsw:array[0..1] of byte;retstr:string;status:byte;keylen:integer;keyid:byte;keytype:byte;beginif(not isrightint(RichEdit2.Lines[0])) thenbeginApplication.MessageBox('密钥号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);RichEdit2.SetFocus();RichEdit2.SelectAll();exit;endelse keyid:=StrToInt(RichEdit2.Lines[0]);keytype := ComboBox4.ItemIndex;case ComboBox4.ItemIndex of0:keylen:=8;1:keylen:=16;2:keylen:=24;elsekeylen:=16;end;if checkhexstr(Edit4.Text ,keylen,authkeybuf)=false thenbeginApplication.MessageBox(PAnsiChar(AnsiString('十六进制认证密钥输入错误,请输入 '+inttostr(keylen)+' 字节的16进制认证密钥!')), '提示', MB_OK+MB_ICONSTOP);exit;end;status := desfireauthkeyev1(@authkeybuf,keyid,keytype, @retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status>0 thenApplication.MessageBox(PAnsiChar(AnsiString('认证密码操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)elseApplication.MessageBox(PAnsiChar(AnsiString('认证密码操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;procedure TForm2.ComboBox4Change(Sender: TObject);
begin
case ComboBox4.ItemIndex of0:Edit4.text:='00 00 00 00 00 00 00 00';1:Edit4.text:='00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00';2:Edit4.text:='00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00';elseEdit4.text:='00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00';end;
end;procedure TForm2.ComboBox1Change(Sender: TObject);
begin
case ComboBox1.ItemIndex of0:beginEdit4.text:='00 00 00 00 00 00 00 00';Edit2.text:='00 00 00 00 00 00 00 00';end;1:beginEdit4.text:='00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00';Edit2.text:='00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00';end;2:beginEdit4.text:='00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00';Edit2.text:='00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00';end;elsebeginEdit4.text:='00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00';Edit2.text:='00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00';end; end;
end;procedure TForm2.Button6Click(Sender: TObject);
varauthkeybuf:array[0..15] of byte;retsw:array[0..1] of byte;retstr:string;status:byte;keylen:integer;keyid:byte;keytype:byte;beginkeylen:=16;if(not isrightint(RichEdit2.Lines[0])) thenbeginApplication.MessageBox('密钥号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);RichEdit2.SetFocus();RichEdit2.SelectAll();exit;endelse keyid:=StrToInt(RichEdit2.Lines[0]);keytype := ComboBox2.ItemIndex;if checkhexstr(Edit3.Text ,keylen,authkeybuf)=false thenbeginApplication.MessageBox(PAnsiChar(AnsiString('十六进制认证密钥输入错误,请输入 '+inttostr(keylen)+' 字节的16进制认证密钥!')), '提示', MB_OK+MB_ICONSTOP);exit;end;status := desfireauthkeyev2(@authkeybuf,keyid,keytype, @retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status>0 thenApplication.MessageBox(PAnsiChar(AnsiString('认证密码操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)elseApplication.MessageBox(PAnsiChar(AnsiString('认证密码操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);end;procedure TForm2.Button3Click(Sender: TObject);
varnewkeybuf:array[0..23] of byte;oldkeybuf:array[0..23] of byte;retsw:array[0..1] of byte;retstr:string;status:byte;keylen:integer;keyid:byte;keytype:byte;beginif(not isrightint(RichEdit2.Lines[0])) thenbeginApplication.MessageBox('密钥号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);RichEdit2.SetFocus();RichEdit2.SelectAll();exit;endelse keyid:=StrToInt(RichEdit2.Lines[0]);keytype := ComboBox1.ItemIndex;case ComboBox1.ItemIndex of0:keylen:=8;1:keylen:=16;2:keylen:=24;elsekeylen:=16;end;if checkhexstr(Edit2.Text ,keylen,newkeybuf)=false thenbeginApplication.MessageBox(PAnsiChar(AnsiString('十六进制新密钥输入错误,请输入 '+inttostr(keylen)+' 字节的16进制新密钥!')), '提示', MB_OK+MB_ICONSTOP);exit;end;if checkhexstr(Edit4.Text ,keylen,oldkeybuf)=false thenbeginApplication.MessageBox(PAnsiChar(AnsiString('十六进制旧密钥输入错误,请输入 '+inttostr(keylen)+' 字节的16进制旧密钥!')), '提示', MB_OK+MB_ICONSTOP);exit;end;status := desfirechangekeyev1(@newkeybuf,keyid,keytype,0,@oldkeybuf, @retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status>0 thenApplication.MessageBox(PAnsiChar(AnsiString('更改密码操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)elseApplication.MessageBox(PAnsiChar(AnsiString('更改密码操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);end;procedure TForm2.Button7Click(Sender: TObject);
varaidbuf:array[0..2] of byte;keysetting:array[0..0] of byte;keysize:array[0..0] of byte;retsw:array[0..1] of byte;retstr,strls1:string;status:byte;i,keynum:integer;beginfor i:=0 to 2 do aidbuf[i]:=0; //picc级aid为 00 00 00status := desfiregetkeysettings(@aidbuf,@keysetting,@keysize,@retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status = 0 thenbeginif (keysetting[0] and 8)>0 then ComboBox3.ItemIndex := 1 else ComboBox3.ItemIndex :=0;if (keysetting[0] and 4)>0 then ComboBox5.ItemIndex := 1 else ComboBox5.ItemIndex :=0;if (keysetting[0] and 2)>0 then ComboBox6.ItemIndex := 1 else ComboBox6.ItemIndex :=0;if (keysetting[0] and 1)>0 then ComboBox7.ItemIndex := 1 else ComboBox7.ItemIndex :=0;keynum:=keysize[0] mod 64;if (keysize[0] and $C0)=$80 then strls1 := 'AES'else if (keysize[0] and $C0)=$40 then strls1 := '3K3DES'else strls1 := 'DES 或 2K3DES' ;Application.MessageBox(PAnsiChar(AnsiString('读取PICC的密钥配置信息操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr)+ #10+'密钥配置:'+inttohex(keysetting[0],2)+ #10+'密钥数量:'+inttostr(keynum)+ #10+'加解密方法:'+strls1)), '提示', MB_OK+MB_ICONINFORMATION);endelse if status = 94 thenApplication.MessageBox('请先执行选择应用AID:00 00 00!', '警告', MB_OK+MB_ICONSTOP)elseApplication.MessageBox(PAnsiChar(AnsiString('读取PICC的配置信息操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
end;procedure TForm2.Button8Click(Sender: TObject);
varaidbuf:array[0..2] of byte;keysetting:byte;retsw:array[0..1] of byte;retstr,strls1:string;status:byte;i,keynum:integer;beginfor i:=0 to 2 do aidbuf[i]:=0; //picc级aid为 00 00 00if ComboBox3.ItemIndex <>1 thenbeginApplication.MessageBox('为防止测试导致卡片产生不可逆的后果,在本例子中不允许锁定配值!', '警告', MB_OK+MB_ICONSTOP);ComboBox3.ItemIndex:=1;exit;end;keysetting:= ComboBox3.ItemIndex *8+ComboBox5.ItemIndex *4+ComboBox6.ItemIndex *2+ComboBox7.ItemIndex ;status := desfirechangekeysettings(@aidbuf,keysetting,@retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status=0 thenApplication.MessageBox(PAnsiChar(AnsiString('更改PICC的密钥配置信息操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)else if status=94 thenApplication.MessageBox('请先执行选择应用AID:00 00 00!', '警告', MB_OK+MB_ICONSTOP)else if status=63 thenApplication.MessageBox('需要先验证当前应用的主密钥!', '警告', MB_OK+MB_ICONSTOP)elseApplication.MessageBox(PAnsiChar(AnsiString('更改PICC的配置信息操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;procedure TForm2.Button9Click(Sender: TObject);
varretsw:array[0..1] of byte;retstr:string;status:byte;beginstatus := desfireformatpicc(@retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status=0 thenApplication.MessageBox(PAnsiChar(AnsiString('初始格式化卡操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)elseApplication.MessageBox(PAnsiChar(AnsiString('初始格式化卡操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;procedure TForm2.Button11Click(Sender: TObject);
varaidbuf:array[0..56] of byte;aidsize:array[0..0] of byte;retsw:array[0..1] of byte;retstr,strls1:string;status:byte;j:integer;beginstatus := desfiregetapplicationids(@aidbuf,@aidsize,@retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status=0 thenbeginstrls1:='';if aidsize[0]>0 thenbeginstrls1:= '应用AID:'+ inttohex(aidbuf[0],2)+inttohex(aidbuf[1],2)+inttohex(aidbuf[2],2);for j:=1 to aidsize[0]-1 do strls1:=strls1+','+ inttohex(aidbuf[j*3],2)+inttohex(aidbuf[j*3+1],2)+inttohex(aidbuf[j*3+2],2);end;Application.MessageBox(PAnsiChar(AnsiString('搜索当前卡片内所有应用AID操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr)+ #10+'应用数:'+inttostr(aidsize[0])+ #10+strls1)), '提示', MB_OK+MB_ICONINFORMATION);endelseApplication.MessageBox(PAnsiChar(AnsiString('搜索当前卡片内所有应用AID操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;procedure TForm2.Button13Click(Sender: TObject);
varaidbuf:array[0..2] of byte;retsw:array[0..1] of byte;retstr,strls1:string;status,keysetting,keynum,Encry:byte;j:integer;beginif checkhexstr(Edit5.Text ,3,aidbuf)=false then //检测AID是否合法并放入数组缓冲beginApplication.MessageBox('十六进制AID输入错误,请输入3字节的16进制AID!', '提示', MB_OK+MB_ICONSTOP);Edit5.SetFocus ;exit;end;if aidbuf[0] + aidbuf[1] + aidbuf[2] = 0 thenbeginApplication.MessageBox('在这里不能操作PICC级AID为00 00 00的主应用!', '提示', MB_OK+MB_ICONSTOP);Edit5.Text :='00 00 01' ;exit;end;if(not isrightint(edit7.Text)) thenbeginApplication.MessageBox('密钥数有非法数值!', '警告', MB_OK+MB_ICONSTOP);edit7.SetFocus();edit7.SelectAll();exit;endelse keynum:=StrToInt(edit7.Text);Encry:=ComboBox11.ItemIndex;keysetting:=ComboBox9.ItemIndex *16+ComboBox10.ItemIndex *8+ComboBox11.ItemIndex *4+ComboBox12.ItemIndex *2+ComboBox13.ItemIndex ;status := desfirecreateapplication(@aidbuf,keysetting,keynum,Encry,@retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status=0 thenApplication.MessageBox(PAnsiChar(AnsiString('创建新应用操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)elseApplication.MessageBox(PAnsiChar(AnsiString('创建新应用操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;procedure TForm2.Button14Click(Sender: TObject);
varaidbuf:array[0..2] of byte;keysetting:array[0..0] of byte;keysize:array[0..0] of byte;retsw:array[0..1] of byte;retstr,strls1:string;status:byte;keynum:integer;beginif checkhexstr(Edit5.Text ,3,aidbuf)=false then //检测AID是否合法并放入数组缓冲beginApplication.MessageBox('十六进制AID输入错误,请输入3字节的16进制AID!', '提示', MB_OK+MB_ICONSTOP);Edit5.SetFocus ;exit;end;if aidbuf[0] + aidbuf[1] + aidbuf[2] = 0 thenbeginApplication.MessageBox('在这里不能操作PICC级AID为00 00 00的主应用!', '提示', MB_OK+MB_ICONSTOP);Edit5.Text :='00 00 01';exit;end;status := desfiregetkeysettings(@aidbuf,@keysetting,@keysize,@retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status = 0 thenbeginComboBox9.ItemIndex := keysetting[0] div 16;if (keysetting[0] and 8)>0 then ComboBox10.ItemIndex := 1 else ComboBox10.ItemIndex :=0;if (keysetting[0] and 4)>0 then ComboBox11.ItemIndex := 1 else ComboBox11.ItemIndex :=0;if (keysetting[0] and 2)>0 then ComboBox12.ItemIndex := 1 else ComboBox12.ItemIndex :=0;if (keysetting[0] and 1)>0 then ComboBox13.ItemIndex := 1 else ComboBox13.ItemIndex :=0;keynum:=keysize[0] mod 64;edit7.Text :=inttostr(keynum);if (keysize[0] and $C0)=$80 then ComboBox8.ItemIndex :=3else if (keysize[0] and $C0)=$40 then ComboBox8.ItemIndex :=2else ComboBox8.ItemIndex :=0 ; 也可能是ComboBox8.SelectedIndex = 1Application.MessageBox(PAnsiChar(AnsiString('读取应用的密钥配置信息操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr)+ #10+'密钥配置:'+inttohex(keysetting[0],2)+ #10+'密钥数量:'+inttostr(keynum))), '提示', MB_OK+MB_ICONINFORMATION);endelse if status = 94 thenApplication.MessageBox(PAnsiChar(AnsiString('请先执行选择应用AID:'+Edit5.Text)), '警告', MB_OK+MB_ICONSTOP)elseApplication.MessageBox(PAnsiChar(AnsiString('读取应用的密钥配置信息操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)end;procedure TForm2.Button15Click(Sender: TObject);
varaidbuf:array[0..2] of byte;keysetting: byte;keysize:array[0..0] of byte;retsw:array[0..1] of byte;retstr,strls1:string;status:byte;keynum:integer;beginif checkhexstr(Edit5.Text ,3,aidbuf)=false then //检测AID是否合法并放入数组缓冲beginApplication.MessageBox('十六进制AID输入错误,请输入3字节的16进制AID!', '提示', MB_OK+MB_ICONSTOP);Edit5.SetFocus ;exit;end;if aidbuf[0] + aidbuf[1] + aidbuf[2] = 0 thenbeginApplication.MessageBox('在这里不能操作PICC级AID为00 00 00的主应用!', '提示', MB_OK+MB_ICONSTOP);Edit5.Text :='00 00 01';exit;end;keysetting:= ComboBox9.ItemIndex *16+ComboBox10.ItemIndex *8+ComboBox11.ItemIndex *4+ComboBox12.ItemIndex*2+ComboBox13.ItemIndex ;status := desfirechangekeysettings(@aidbuf,keysetting,@retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status=0 thenApplication.MessageBox(PAnsiChar(AnsiString('更改应用密钥配置信息操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)else if status=94 thenApplication.MessageBox(PAnsiChar(AnsiString('请先执行选择应用AID:'+Edit5.Text)), '警告', MB_OK+MB_ICONSTOP)else if status=63 thenApplication.MessageBox('需要先验证当前应用的主密钥!', '警告', MB_OK+MB_ICONSTOP)elseApplication.MessageBox(PAnsiChar(AnsiString('更改应用密钥配置信息操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);end;procedure TForm2.Button17Click(Sender: TObject);
varaidbuf:array[0..2] of byte;keysetting: byte;keysize:array[0..0] of byte;retsw:array[0..1] of byte;retstr,strls1:string;status:byte;keynum:integer;beginif checkhexstr(Edit5.Text ,3,aidbuf)=false then //检测AID是否合法并放入数组缓冲beginApplication.MessageBox('十六进制AID输入错误,请输入3字节的16进制AID!', '提示', MB_OK+MB_ICONSTOP);Edit5.SetFocus ;exit;end;if aidbuf[0] + aidbuf[1] + aidbuf[2] = 0 thenbeginApplication.MessageBox('在这里不能操作PICC级AID为00 00 00的主应用!', '提示', MB_OK+MB_ICONSTOP);Edit5.Text :='00 00 01';exit;end;status := desfiredeleteapplication(@aidbuf,@retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status=0 thenApplication.MessageBox(PAnsiChar(AnsiString('删除卡内应用操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)elseApplication.MessageBox(PAnsiChar(AnsiString('更删除卡内应用操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);end;procedure TForm2.Button12Click(Sender: TObject);
varfidbuf:array[0..15] of byte; //文件ID号数据缓冲,每个ID号占用1个字节,最多16个文件ID号fidsize:array[0..0] of byte;retsw:array[0..1] of byte;retstr,strls1:string;status:byte;j:integer;beginstatus := desfiregetfileids(@fidbuf,@fidsize,@retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status=0 thenbeginstrls1:='';if fidsize[0]>0 thenbeginstrls1:= '文件ID号:'+ inttostr(fidbuf[0]);for j:=1 to fidsize[0]-1 do strls1:=strls1+','+ inttostr(fidbuf[j]);end;Application.MessageBox(PAnsiChar(AnsiString('搜索当前应用内所有文件ID操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr)+ #10+'文件数量:'+inttostr(fidsize[0])+ #10+strls1)), '提示', MB_OK+MB_ICONINFORMATION);endelseApplication.MessageBox(PAnsiChar(AnsiString('搜索当前应用内所有文件ID操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);end;procedure TForm2.Button18Click(Sender: TObject);
varaccessrights:array[0..1] of byte;retsw:array[0..1] of byte;retstr,strls1:string;status,fileid,comset:byte;filebytes:integer;beginif(not isrightint(edit8.Text)) thenbeginApplication.MessageBox('文件ID有非法数值!', '警告', MB_OK+MB_ICONSTOP);edit8.SetFocus();edit8.SelectAll();exit;endelse fileid:=StrToInt(edit8.Text);if ComboBox14.ItemIndex <2 thencomset:=ComboBox14.ItemIndexelse comset:=3;accessrights[1]:=ComboBox15.ItemIndex *16+ComboBox16.ItemIndex;accessrights[0]:=ComboBox17.ItemIndex *16+ComboBox18.ItemIndex;if(not isrightint(edit9.Text)) thenbeginApplication.MessageBox('文件空间有非法数值!', '警告', MB_OK+MB_ICONSTOP);edit9.SetFocus();edit9.SelectAll();exit;endelse filebytes:=StrToInt(edit9.Text);if RadioButton1.Checked thenstatus:=desfirecreatestddatafile(fileid,comset,@accessrights,filebytes,@retsw)elsestatus:=desfircreatebackupdatafile(fileid,comset,@accessrights,filebytes,@retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status=0 thenApplication.MessageBox(PAnsiChar(AnsiString('在应用内创建新文件操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)else if status=53 thenApplication.MessageBox(PAnsiChar(AnsiString('在应用内创建新文件操作返回异常:'+inttostr(status)+',说明:CPU卡功能通讯错误,可能是卡拿开重放后未激活或卡不在感应区!')), '提示', MB_OK+MB_ICONINFORMATION)else if status=57 thenbeginif retstr='91DE' thenApplication.MessageBox(PAnsiChar(AnsiString('在应用内创建新文件操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',可能的原因:1 应用没选择对,2 密码未认证,3 文件已存在!' )), '提示', MB_OK+MB_ICONINFORMATION)elseApplication.MessageBox(PAnsiChar(AnsiString('在应用内创建新文件操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',请选择正确的应用!' )), '提示', MB_OK+MB_ICONINFORMATION)endelseApplication.MessageBox(PAnsiChar(AnsiString('在应用内创建新文件操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',请选择正确的应用!' )), '提示', MB_OK+MB_ICONINFORMATION);
end;procedure TForm2.Button19Click(Sender: TObject);
varrevbuflen:array[0..3] of byte;filesettingsbuf:array[0..31] of byte;retsw:array[0..1] of byte;retstr,strls1:string;status,fileid:byte;revlen:PInteger;j,filesize:integer;beginif(not isrightint(edit8.Text)) thenbeginApplication.MessageBox('文件ID有非法数值!', '警告', MB_OK+MB_ICONSTOP);edit8.SetFocus();edit8.SelectAll();exit;endelse fileid:=StrToInt(edit8.Text);status:=desfiregetfilesettings(fileid,@filesettingsbuf,@revbuflen,@retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status=0 thenbeginrevlen:=@revbuflen[0]; //将数组转换成整数指针,revlen^ 就是转换后的整数strls1:='';for j:=0 to revlen^-1 do strls1:=strls1+inttohex(filesettingsbuf[j],2);if filesettingsbuf[1] mod 4=3 then //解析通信模式ComboBox14.ItemIndex :=2else if filesettingsbuf[1] mod 4=1 thenComboBox14.ItemIndex :=1else ComboBox14.ItemIndex :=0;ComboBox15.ItemIndex:= filesettingsbuf[3] div 16; //只读时需要的密码号ComboBox16.ItemIndex:= filesettingsbuf[3] mod 16; //只写时需要的密码号ComboBox17.ItemIndex:= filesettingsbuf[2] div 16; //读写时需要的密码号ComboBox18.ItemIndex:= filesettingsbuf[2] mod 16; //更改本配置时需要的密码号filesize:= filesettingsbuf[4] + filesettingsbuf[5] * 256 + filesettingsbuf[6] * 65536;Edit9.Text :=inttostr(filesize);Application.MessageBox(PAnsiChar(AnsiString('读取文件配置操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr)+#10+'16进制文件配置信息:'+strls1)), '提示', MB_OK+MB_ICONINFORMATION);endelseApplication.MessageBox(PAnsiChar(AnsiString('读取文件配置信息返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;procedure TForm2.Button20Click(Sender: TObject);
varaccessrights:array[0..1] of byte;retsw:array[0..1] of byte;retstr,strls1:string;status,fileid,comset:byte;filebytes:integer;beginif(not isrightint(edit8.Text)) thenbeginApplication.MessageBox('文件ID有非法数值!', '警告', MB_OK+MB_ICONSTOP);edit8.SetFocus();edit8.SelectAll();exit;endelse fileid:=StrToInt(edit8.Text);if ComboBox14.ItemIndex <2 thencomset:=ComboBox14.ItemIndexelse comset:=3;accessrights[1]:=ComboBox15.ItemIndex *16+ComboBox16.ItemIndex;accessrights[0]:=ComboBox17.ItemIndex *16+ComboBox18.ItemIndex;status:=desfirechangefilesettings(fileid,comset,@accessrights,@retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status=0 thenApplication.MessageBox(PAnsiChar(AnsiString('更改文件配置操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)else if status=53 thenApplication.MessageBox(PAnsiChar(AnsiString('更改文件配置操作返回异常:'+inttostr(status)+ ',说明:CPU卡功能通讯错误,可能是卡拿开重放后未激活或卡不在感应区!')), '提示', MB_OK+MB_ICONINFORMATION)elseApplication.MessageBox(PAnsiChar(AnsiString('更改文件配置操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);end;procedure TForm2.Button21Click(Sender: TObject);
varretsw:array[0..1] of byte;retstr,strls1:string;status,fileid:byte;beginif(not isrightint(edit8.Text)) thenbeginApplication.MessageBox('文件ID有非法数值!', '警告', MB_OK+MB_ICONSTOP);edit8.SetFocus();edit8.SelectAll();exit;endelse fileid:=StrToInt(edit8.Text);status:=desfiredeletefile(fileid, @retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status=0 thenApplication.MessageBox(PAnsiChar(AnsiString('删除文件操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)elseApplication.MessageBox(PAnsiChar(AnsiString('删除文件操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);end;procedure TForm2.Button24Click(Sender: TObject);
vari,datalen:integer;strls2:string;
begindatalen:=StrToInt(edit11.Text);for i:=0 to datalen-1 do strls2:=strls2+IntToHex(i,2)+' ';memo1.Text :=strls2;
end;procedure TForm2.Button22Click(Sender: TObject);
varcmd:byte;databuf:array[0..8191] of byte;retsw:array[0..1] of byte;retstr,datastr:string;status,fileid:byte;rwbegin,rwlen,j:integer;begincmd:=$BD; //参数为&HBD表示读数据,为&hBB表示读记录if(not isrightint(edit8.Text)) thenbeginApplication.MessageBox('文件ID有非法数值!', '警告', MB_OK+MB_ICONSTOP);edit8.SetFocus();edit8.SelectAll();exit;endelse fileid:=StrToInt(edit8.Text);rwbegin:=StrToInt(edit10.Text);rwlen:=StrToInt(edit11.Text);status := desfirereaddata(cmd,fileid,rwbegin,rwlen,@databuf,@retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status=0 thenbegindatastr:='';for j:=0 to rwlen-1 do datastr:=datastr+ inttohex(databuf[j],2)+' ';memo1.Text :=datastr;Application.MessageBox(PAnsiChar(AnsiString('读文件操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)endelse if status=63 thenApplication.MessageBox(PAnsiChar(AnsiString('读文件操作返回异常:'+inttostr(status)+ ',说明:权限不足,请先用正确的密钥号认证!')), '提示', MB_OK+MB_ICONINFORMATION)elseApplication.MessageBox(PAnsiChar(AnsiString('读文件操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;procedure TForm2.Button23Click(Sender: TObject);
varcmd:byte;databuf:array[0..8191] of byte;retsw:array[0..1] of byte;retstr,datastr:string;status,fileid:byte;rwbegin,rwlen,j:integer;begincmd:=$3D; //参数为&H3D表示写数据,为&h3B表示写记录if(not isrightint(edit8.Text)) thenbeginApplication.MessageBox('文件ID有非法数值!', '警告', MB_OK+MB_ICONSTOP);edit8.SetFocus();edit8.SelectAll();exit;endelse fileid:=StrToInt(edit8.Text);rwbegin:=StrToInt(edit10.Text);rwlen:=StrToInt(edit11.Text);if checkhexstr(memo1.Text ,rwlen,databuf)=false then //检测数据是否合法并放入数组缓冲beginApplication.MessageBox('写入数据输入不足或错误,请输入正确的写卡数据!', '提示', MB_OK+MB_ICONSTOP);memo1.SetFocus ;exit;end;status:=desfirewritedata(cmd,fileid,rwbegin,rwlen,@databuf,@retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status=0 thenApplication.MessageBox(PAnsiChar(AnsiString('写文件操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)else if status=63 thenApplication.MessageBox(PAnsiChar(AnsiString('写文件操作返回异常:'+inttostr(status)+ ',说明:权限不足,请先用正确的密钥号认证!')), '提示', MB_OK+MB_ICONINFORMATION)elseApplication.MessageBox(PAnsiChar(AnsiString('写文件操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);end;procedure TForm2.Button25Click(Sender: TObject);
varretsw:array[0..1] of byte;retstr:string;status:byte;
beginstatus:= desfirecommittransaction(@retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status=0 thenApplication.MessageBox(PAnsiChar(AnsiString('提交事务使备份生效操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)elseApplication.MessageBox(PAnsiChar(AnsiString('提交事务使备份生效操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);end;procedure TForm2.Button26Click(Sender: TObject);
varretsw:array[0..1] of byte;retstr:string;status:byte;
beginstatus:= desfireaborttransaction(@retsw);retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);if status=0 thenApplication.MessageBox(PAnsiChar(AnsiString('取消事务操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)elseApplication.MessageBox(PAnsiChar(AnsiString('取消事务返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);end;end.