Delphi读写Mifare Desfire Ev1 Ev2 EV3卡修改AES密钥源码

article/2025/7/18 10:01:06

本示例使用的发卡器: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.

 


http://www.hkcw.cn/article/aaZYNittsr.shtml

相关文章

第T10周:数据增强

import matplotlib.pyplot as plt import numpy as np #隐藏警告 import warnings warnings.filterwarnings(ignore)from tensorflow.keras import layers import tensorflow as tf gpus tf.config.list_physical_devices("GPU")if gpus:tf.config.experimental.set…

Python_day40

昨天我们介绍了图像数据的格式以及模型定义的过程&#xff0c;发现和之前结构化数据的略有不同&#xff0c;主要差异体现在2处 1. 模型定义的时候需要展平图像 2. 由于数据过大&#xff0c;需要将数据集进行分批次处理&#xff0c;这往往涉及到了dataset和dataloader来规范代码…

MSTNet:用于糖尿病视网膜病变分类的多尺度空间感知 Transformer 与多实例学习方法|文献速递-深度学习医疗AI最新文献

Title 题目 MSTNet: Multi-scale spatial-aware transformer with multi-instance learning for diabetic retinopathy classification MSTNet&#xff1a;用于糖尿病视网膜病变分类的多尺度空间感知 Transformer 与多实例学习方法 01 文献速递介绍 糖尿病视网膜病变&#…

Linux上安装MongoDB

目录 一、在Linux系统安装MongoDB服务器 1、下载MongoDB 2、上传MongoDB并解压 3、创建必要目录 4、配置环境变量 5、创建配置文件 6、启动命令 7、验证安装 二、在Linux系统安装MongoDB客户端Shell 1、下载MongoDB Shell 2、上传MongoDB Shell并解压 3、配置环境变…

muduo库的初步认识和基本使用,创建一个简单查询单词服务系统

小编在学习完muduo库之后&#xff0c;觉得对于初学者&#xff0c;muduo库还是有点不好理解&#xff0c;所以在此&#xff0c;小编来告诉大家muduo库的初步认识和基本使用&#xff0c;让初学者也可以更快的上手和使用muduo库。 Muduo由陈硕大佬开发&#xff0c;是⼀个基于 非阻塞…

格恩朗超声波水表 助力农业精准灌溉与振兴​

在农业现代化的征程中&#xff0c;水资源的精准利用至关重要&#xff0c;而这离不开高精度计量设备的支持。大连格恩朗品牌积极响应国家全面推进乡村振兴、加快农业农村现代化的号召&#xff0c;精心打造的超声波水表&#xff0c;凭借其超高精度&#xff0c;成为绿色灌溉领域的…

Nginx进阶篇(静态资源的缓存处理、Nginx中与浏览器缓存相关的指令、Nginx的跨域问题、静态资源防盗链)

文章目录 1. 静态资源的缓存处理1.1 什么是缓存1.2 什么是Web缓存1.3 Web缓存的种类1.3.1 客户端缓存1.3.2 服务端缓存 1.4 为什么要用浏览器缓存1.5 浏览器缓存的执行流程1.6 浏览器强缓存和弱缓存的区别1.6.1 强缓存&#xff08;Strong Cache&#xff09;1.6.2 弱缓存&#x…

云游戏混合架构

云游戏混合架构通过整合本地计算资源与云端能力&#xff0c;形成了灵活且高性能的技术体系&#xff0c;其核心架构及技术特征可概括如下&#xff1a; 一、混合架构的典型模式 分层混合模式‌ 前端应用部署于公有云&#xff08;如渲染流化服务&#xff09;&#xff0c;后端逻辑…

Origin教程010:Origin绘制同心圆图

文章目录 10、绘制同心圆弧图1、同心圆弧图绘制2、调整绘图顺序3、设置标签内容、样式4、指引线设置5、添加图形标题6、练习数据10、绘制同心圆弧图 本节要点: 同心圆弧图的绘制调整绘图顺序(对象管理器)设置标签内容、样式指引线设置添加图形标题1、同心圆弧图绘制 1️⃣拖…

rs485/232转profinet网关与长陆-UNI800称重显示控制仪通讯

rs485/232转profinet网关与长陆-UNI800称重显示控制仪通讯 在现代工业自动化系统中&#xff0c;RS485转Profinet网关作为一种关键的通信接口设备&#xff0c;其重要性不言而喻。它能够将传统的RS485接口设备接入先进的Profinet网络&#xff0c;实现不同协议之间的无缝转换和数…

【速通RAG实战:进阶】20、改进RAG检索质量有哪些诀窍?

一、数据层优化:构建高质量检索基础 (一)动态语义分块技术 传统固定长度分块易切断完整语义,采用基于相似度的动态分块策略可显著提升上下文连贯性。通过LangChain的SemanticChunker实现语义边界检测,当相邻文本相似度低于0.4时自动切分,避免将“设备型号-参数-操作步骤…

Nginx网站服务:从入门到LNMP架构实战

&#x1f3e1;作者主页&#xff1a;点击&#xff01; Nginx-从零开始的服务器之旅专栏&#xff1a;点击&#xff01; &#x1f427;Linux高级管理防护和群集专栏&#xff1a;点击&#xff01; ⏰️创作时间&#xff1a;2025年5月30日14点22分 前言 说起Web服务器&#xff0c…

早发现=早安心!超导心磁图如何捕捉早期病变信号?

随着生活节奏的加快&#xff0c;心血管疾病已成为威胁人们健康的“隐形杀手”。据国家心血管病中心发布的《中国心血管健康与疾病报告2022》显示&#xff0c;我国心血管病现患者人数已高达3.3亿&#xff0c;每5例死亡中就有2例死于心血管病。这一数据触目惊心&#xff0c;提醒我…

AI感知与行动:考拉悠然发布空间智能世界模型,让AI走进物理世界

本文转自&#xff1a;《封面新闻》 5月&#xff0c;2025福布斯中国人工智能科技企业TOP50评选结果发布&#xff0c;成都考拉悠然科技有限公司成功入选&#xff0c;成为榜单中唯一专注“空间智能”的企业。 &#xff0c;时长02:55 而在近日&#xff0c;考拉悠然发布了面向空间…

Arduino学习-跑马灯

1、效果 2、代码 /**** 2025-5-30 跑马灯的小程序 */ //时间间隔 int intervaltime200; //初始化函数 void setup() {// put your setup code here, to run once://设置第3-第7个引脚为输出模式for(int i3;i<8;i){pinMode(i,OUTPUT);} }//循环执行 void loop() {// put you…

CAD多边形密堆积2D插件

插件介绍 CAD多边形密堆积2D插件可在AutoCAD内建立模拟重力堆积状态的随机多边形颗粒及界面过渡区&#xff08;ITZ&#xff09;模型。 模型可分为多边形颗粒、界面过渡区&#xff08;ITZ&#xff09;、长方形试件三部分&#xff0c;各部分在CAD内分图层绘制&#xff0c;可批…

特伦斯 S75:重塑钢琴体验的数码钢琴之选

当传统钢琴的典雅质感与现代科技的精准赋能相遇&#xff0c;特伦斯 S75 立式数码钢琴应运而生。这款专为追求品质的演奏者与音乐爱好者设计的高端乐器&#xff0c;以 “还原三角钢琴灵魂&#xff0c;革新数字钢琴体验” 为核心理念&#xff0c;在音色、触感、音质与智能交互间达…

methods的实现原理

一、直观的感受methods的使用 首先直观的感受methods方法的使用&#xff0c;同样以计数器为例&#xff0c; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widt…

晨控CK-FR03与TwinCAT3配置EtherCAT通讯连接手册

晨控CK-FR03与TwinCAT3配置EtherCAT通讯连接手册 晨控CK-FR03系列作为晨控智能工业级别RFID读写器,支持大部分工业协议如RS232、RS485、以太网。支持工业协议Modbus RTU、Modbus TCP、Profinet、EtherNet/lP、EtherCat以及自由协议TCP/IP等。 本期主题&#xff1a;围绕CK-FR03…

大模型-attention汇总解析之-MLA

一、核心思想 先看下初始的MLA的一般性公式&#xff1a; 我们一般会缓存的是投影后的k_i, v_i而不是投影前的x_i, c_i &#xff0c;根据 MLA 的这个做法&#xff0c;通过不同的投影矩阵再次让所有的 K、V Head 都变得各不相同&#xff0c;那么 KV Cache 的大小就恢复成跟 MHA …