Delphi修改Mifare Desfire Ev1 Ev2 EV3卡DES、2K3DES、3K3DES、AES密钥,创建应用、读写文件源码

本示例使用的发卡器:https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.52de2c1b3bN1Vu&ft=t&id=917152255720

一、函数声明

unit declaredll;

interface
      //驱动蜂鸣器函数声明
      function pcdbeep(xms:integer):byte;stdcall;external 'OUR_MIFARE.dll';

      //读出设备编号函数声明
      function pcdgetdevicenumber(pdevicenumber:pbyte):byte;stdcall;external 'OUR_MIFARE.dll';

      //发送显示
      function lcddispfull(lcdstr:PChar):byte;stdcall;external 'OUR_MIFARE.dll';

      //读取Mifare系例卡类型
      function getmifareversion(cardtypestr:pbyte;AtqaSak:pbyte;versionbuf:pbyte;versionlen:pbyte;retsw:pbyte):byte;stdcall;external 'OUR_MIFARE.dll';

      function cpurequest1(serial:pbyte; param:pbyte;cosver:pbyte;code:pbyte;AtqaSak:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfireselectapplication(aidbuf:pbyte; retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfireauthkeyev1(authkeybuf:pbyte;keyid:byte;keytype:byte;retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfireauthkeyev2(authkeybuf:pbyte;keyid:byte;keytype:byte;retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfirechangekeyev1(newkeybuf:pbyte;keyid:byte;keytype:byte;zeno:byte;oldkeybuf:pbyte;retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfiregetkeysettings(aidbuf:pbyte;keysetting:pbyte;keysize:pbyte; retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfirechangekeysettings(aidbuf:pbyte;keysetting:byte;retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfireformatpicc(retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfiregetapplicationids(aidbuf:pbyte;aidsize:pbyte;retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfirecreateapplication(aidbuf:pbyte;keysetting:byte;keynumber:byte;destype:byte;retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfiredeleteapplication(aidbuf:pbyte;retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfiregetfileids(fidbuf:pbyte;fidsize:pbyte;retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfirecreatestddatafile(fidbuf:byte;comset:byte;accessrights:pbyte;filesize:integer;retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfircreatebackupdatafile(fidbuf:byte;comset:byte;accessrights:pbyte;filesize:integer;retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfiregetfilesettings(fidbuf:byte;filesettingsbuf:pbyte;revbuflen:pbyte;retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfirechangefilesettings(fidbuf:byte;comset:byte;accessrights:pbyte;retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfiredeletefile(fidbuf:byte;retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfirereaddata(cmd:byte;fidbuf:byte;rwbegin:integer;rwlen:integer;databuf:pbyte;retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfirewritedata(cmd:byte;fidbuf:byte;rwbegin:integer;rwlen:integer;databuf:pbyte;retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfirecommittransaction(retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';
      function desfireaborttransaction(retsw:pbyte): Byte;stdcall;external 'OUR_MIFARE.dll';


implementation


end.

 二、激活发卡器上的Desfire卡

procedure TForm2.Button10Click(Sender: TObject);
var
  status: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;

begin
    status := cpurequest1(@mypiccserial,@myparam,@myver,@mycode,@AtqaSak);
    if (status = 0) or (status =52) then
    begin
        pcdbeep(20);
        if(AtqaSak[0] div 64 >0) then
        begin
            cardhohex:='';
            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);
        end
        else
        begin
            cardhohex:='';
            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;
    end
    else MessageDispInfo(status);
end;

三、选择卡内已有应用AID

procedure TForm2.Button1Click(Sender: TObject);
var
  aidbuf:array[0..2] of byte;
  retsw:array[0..1] of byte;
  retstr:string;
  status:byte;

begin
   if checkhexstr(Edit1.Text ,3,aidbuf)=false then    //检测AID是否合法并放入数组缓冲
   begin
       Application.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 then
      Application.MessageBox(PAnsiChar(AnsiString('选择卡内应用AID操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
   else
      Application.MessageBox(PAnsiChar(AnsiString('选择卡内应用AID操作卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;

四、密钥认证

procedure TForm2.Button2Click(Sender: TObject);
var
  authkeybuf:array[0..23] of byte;
  retsw:array[0..1] of byte;
  retstr:string;
  status:byte;
  keylen:integer;
  keyid:byte;
  keytype:byte;

begin
  if(not isrightint(RichEdit2.Lines[0])) then
  begin
       Application.MessageBox('密钥号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
       RichEdit2.SetFocus();
       RichEdit2.SelectAll();
       exit;
  end
  else keyid:=StrToInt(RichEdit2.Lines[0]);

  keytype := ComboBox4.ItemIndex;

  case ComboBox4.ItemIndex of
      0:
        keylen:=8;
      1:
        keylen:=16;
      2:
        keylen:=24;
      else
        keylen:=16;
  end;

  if checkhexstr(Edit4.Text ,keylen,authkeybuf)=false then
  begin
       Application.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 then
      Application.MessageBox(PAnsiChar(AnsiString('认证密码操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
   else
      Application.MessageBox(PAnsiChar(AnsiString('认证密码操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;

五、更改密钥

procedure TForm2.Button3Click(Sender: TObject);
var
  newkeybuf: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;

begin
  if(not isrightint(RichEdit2.Lines[0])) then
  begin
       Application.MessageBox('密钥号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
       RichEdit2.SetFocus();
       RichEdit2.SelectAll();
       exit;
  end
  else keyid:=StrToInt(RichEdit2.Lines[0]);

  keytype := ComboBox1.ItemIndex;

  case ComboBox1.ItemIndex of
      0:
        keylen:=8;
      1:
        keylen:=16;
      2:
        keylen:=24;
      else
        keylen:=16;
  end;

  if checkhexstr(Edit2.Text ,keylen,newkeybuf)=false then
  begin
       Application.MessageBox(PAnsiChar(AnsiString('十六进制新密钥输入错误,请输入 '+inttostr(keylen)+' 字节的16进制新密钥!')), '提示', MB_OK+MB_ICONSTOP);
       exit;
  end;

  if checkhexstr(Edit4.Text ,keylen,oldkeybuf)=false then
  begin
       Application.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 then
      Application.MessageBox(PAnsiChar(AnsiString('更改密码操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
  else
      Application.MessageBox(PAnsiChar(AnsiString('更改密码操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;

六、搜索卡内所有应用AID

procedure TForm2.Button11Click(Sender: TObject);
var
  aidbuf:array[0..56] of byte;
  aidsize:array[0..0] of byte;
  retsw:array[0..1] of byte;
  retstr,strls1:string;
  status:byte;
  j:integer;

begin
  status := desfiregetapplicationids(@aidbuf,@aidsize,@retsw);
  retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);
  if status=0 then
  begin
      strls1:='';
      if aidsize[0]>0 then
      begin
          strls1:= '应用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);
  end
  else
      Application.MessageBox(PAnsiChar(AnsiString('搜索当前卡片内所有应用AID操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;

七、在卡内创建新的应用

procedure TForm2.Button13Click(Sender: TObject);
var
  aidbuf:array[0..2] of byte;
  retsw:array[0..1] of byte;
  retstr,strls1:string;
  status,keysetting,keynum,Encry:byte;
  j:integer;

begin
  if checkhexstr(Edit5.Text ,3,aidbuf)=false then   //检测AID是否合法并放入数组缓冲
  begin
       Application.MessageBox('十六进制AID输入错误,请输入3字节的16进制AID!', '提示', MB_OK+MB_ICONSTOP);
       Edit5.SetFocus ;
       exit;
  end;
  if aidbuf[0] + aidbuf[1] + aidbuf[2] = 0 then
  begin
       Application.MessageBox('在这里不能操作PICC级AID为00 00 00的主应用!', '提示', MB_OK+MB_ICONSTOP);
       Edit5.Text :='00 00 01' ;
       exit;
  end;
  if(not isrightint(edit7.Text)) then
  begin
       Application.MessageBox('密钥数有非法数值!', '警告', MB_OK+MB_ICONSTOP);
       edit7.SetFocus();
       edit7.SelectAll();
       exit;
  end
  else 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 then
      Application.MessageBox(PAnsiChar(AnsiString('创建新应用操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
  else
      Application.MessageBox(PAnsiChar(AnsiString('创建新应用操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;

八、搜索当前应用内的所有文件

procedure TForm2.Button12Click(Sender: TObject);
var
  fidbuf: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;

begin
  status := desfiregetfileids(@fidbuf,@fidsize,@retsw);
  retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);
  if status=0 then
  begin
      strls1:='';
      if fidsize[0]>0 then
      begin
          strls1:= '文件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);
  end
  else
      Application.MessageBox(PAnsiChar(AnsiString('搜索当前应用内所有文件ID操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;

九、在当前应用内创建新的文件

procedure TForm2.Button18Click(Sender: TObject);
var
  accessrights:array[0..1] of byte;
  retsw:array[0..1] of byte;
  retstr,strls1:string;
  status,fileid,comset:byte;
  filebytes:integer;

begin
  if(not isrightint(edit8.Text)) then
  begin
       Application.MessageBox('文件ID有非法数值!', '警告', MB_OK+MB_ICONSTOP);
       edit8.SetFocus();
       edit8.SelectAll();
       exit;
  end
  else fileid:=StrToInt(edit8.Text);

  if ComboBox14.ItemIndex <2 then
      comset:=ComboBox14.ItemIndex
  else  comset:=3;

  accessrights[1]:=ComboBox15.ItemIndex *16+ComboBox16.ItemIndex;
  accessrights[0]:=ComboBox17.ItemIndex *16+ComboBox18.ItemIndex;

  if(not isrightint(edit9.Text)) then
  begin
       Application.MessageBox('文件空间有非法数值!', '警告', MB_OK+MB_ICONSTOP);
       edit9.SetFocus();
       edit9.SelectAll();
       exit;
  end
  else filebytes:=StrToInt(edit9.Text);
  
  if RadioButton1.Checked then
      status:=desfirecreatestddatafile(fileid,comset,@accessrights,filebytes,@retsw)
  else
      status:=desfircreatebackupdatafile(fileid,comset,@accessrights,filebytes,@retsw);
  retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);
  if status=0 then
      Application.MessageBox(PAnsiChar(AnsiString('在应用内创建新文件操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
  else if status=53 then
      Application.MessageBox(PAnsiChar(AnsiString('在应用内创建新文件操作返回异常:'+inttostr(status)+',说明:CPU卡功能通讯错误,可能是卡拿开重放后未激活或卡不在感应区!')), '提示', MB_OK+MB_ICONINFORMATION)
  else if status=57 then
      begin
          if retstr='91DE' then
              Application.MessageBox(PAnsiChar(AnsiString('在应用内创建新文件操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',可能的原因:1 应用没选择对,2 密码未认证,3 文件已存在!' )), '提示', MB_OK+MB_ICONINFORMATION)
          else
              Application.MessageBox(PAnsiChar(AnsiString('在应用内创建新文件操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',请选择正确的应用!' )), '提示', MB_OK+MB_ICONINFORMATION)
      end
  else
      Application.MessageBox(PAnsiChar(AnsiString('在应用内创建新文件操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',请选择正确的应用!' )), '提示', MB_OK+MB_ICONINFORMATION);
end;

十、修改文件密钥配置信息

procedure TForm2.Button20Click(Sender: TObject);
var
  accessrights:array[0..1] of byte;
  retsw:array[0..1] of byte;
  retstr,strls1:string;
  status,fileid,comset:byte;
  filebytes:integer;

begin
  if(not isrightint(edit8.Text)) then
  begin
       Application.MessageBox('文件ID有非法数值!', '警告', MB_OK+MB_ICONSTOP);
       edit8.SetFocus();
       edit8.SelectAll();
       exit;
  end
  else fileid:=StrToInt(edit8.Text);

  if ComboBox14.ItemIndex <2 then
      comset:=ComboBox14.ItemIndex
  else  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 then
      Application.MessageBox(PAnsiChar(AnsiString('更改文件配置操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
  else if status=53 then
      Application.MessageBox(PAnsiChar(AnsiString('更改文件配置操作返回异常:'+inttostr(status)+ ',说明:CPU卡功能通讯错误,可能是卡拿开重放后未激活或卡不在感应区!')), '提示', MB_OK+MB_ICONINFORMATION)
  else
      Application.MessageBox(PAnsiChar(AnsiString('更改文件配置操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;

十一、读取文件内数据

procedure TForm2.Button22Click(Sender: TObject);
var
  cmd:byte;
  databuf:array[0..8191] of byte;
  retsw:array[0..1] of byte;
  retstr,datastr:string;
  status,fileid:byte;
  rwbegin,rwlen,j:integer;

begin
  cmd:=$BD;            //参数为&HBD表示读数据,为&hBB表示读记录
  if(not isrightint(edit8.Text)) then
  begin
       Application.MessageBox('文件ID有非法数值!', '警告', MB_OK+MB_ICONSTOP);
       edit8.SetFocus();
       edit8.SelectAll();
       exit;
  end
  else 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 then
  begin
      datastr:='';
      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)
  end
  else if status=63 then
       Application.MessageBox(PAnsiChar(AnsiString('读文件操作返回异常:'+inttostr(status)+ ',说明:权限不足,请先用正确的密钥号认证!')), '提示', MB_OK+MB_ICONINFORMATION)
  else
       Application.MessageBox(PAnsiChar(AnsiString('读文件操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;

十二、写数据到文件内

procedure TForm2.Button23Click(Sender: TObject);
var
  cmd:byte;
  databuf:array[0..8191] of byte;
  retsw:array[0..1] of byte;
  retstr,datastr:string;
  status,fileid:byte;
  rwbegin,rwlen,j:integer;

begin
  cmd:=$3D;            //参数为&H3D表示写数据,为&h3B表示写记录
  if(not isrightint(edit8.Text)) then
  begin
       Application.MessageBox('文件ID有非法数值!', '警告', MB_OK+MB_ICONSTOP);
       edit8.SetFocus();
       edit8.SelectAll();
       exit;
  end
  else fileid:=StrToInt(edit8.Text);

  rwbegin:=StrToInt(edit10.Text);
  rwlen:=StrToInt(edit11.Text);

  if checkhexstr(memo1.Text ,rwlen,databuf)=false then    //检测数据是否合法并放入数组缓冲
  begin
       Application.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 then
       Application.MessageBox(PAnsiChar(AnsiString('写文件操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
  else if status=63 then
       Application.MessageBox(PAnsiChar(AnsiString('写文件操作返回异常:'+inttostr(status)+ ',说明:权限不足,请先用正确的密钥号认证!')), '提示', MB_OK+MB_ICONINFORMATION)
  else
       Application.MessageBox(PAnsiChar(AnsiString('写文件操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;

十三、完整代码

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,declaredll,strutils, ComCtrls, ExtCtrls;

type
  TForm2 = 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;

var
  Form2: TForm2;

////MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
implementation

{$R *.dfm}
procedure MessageDispInfo(Errcode:byte);                       //函数返回代码说明
begin
     case Errcode of
          0: Application.MessageBox ('操作成功!', '提示', MB_OK+MB_ICONINFORMATION);             //////MB_ICONQUESTION  MB_ICONEXCLAMATION  MB_ICONWARNING  MB_ICONINFORMATION  MB_ICONASTERISK  MB_ICONHAND  MB_ICONERROR  MB_ICONSTOP
          8: 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);
          else
          Application.MessageBox(PAnsiChar(AnsiString('异常错误代码:'+inttostr(Errcode))), '提示', MB_OK+MB_ICONINFORMATION);
     end;
end;

function isrightint(textls:string):boolean;stdcall;   //判断有无非法字符
begin
       try
          if(strtoint(textls) =0) then
          begin
          end;
          result := True;
       except
          result := False;
          exit;
       end;
end;

function checkhexstr(inputstr:string;hexlen:integer;var databuf:array of byte):boolean;stdcall;
var
i:integer;
begin
  try
       inputstr:=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;
  except
        result := False;
   end;
end;

function  RetTextFromStr(inputstr:string):string;stdcall;
var
i:integer;
begin
  RetTextFromStr:='';
  if inputstr='9000' then
      RetTextFromStr := '成功!'
  else if inputstr='9100' then
       begin
          pcdbeep(20);
          RetTextFromStr := '成功!';
       end
  else if inputstr='6281' then
       RetTextFromStr := '回送的数据可能错误!'
  else if inputstr='6283' then
       RetTextFromStr := '选择文件无效,文件或密钥校验错误!'
  else if inputstr='6400' then
       RetTextFromStr := '状态标志未改变!'
  else if inputstr='6581' then
       RetTextFromStr := '写 EEPROM 不成功!'
  else if inputstr='6700' then
       RetTextFromStr := '长度错误!'
  else if inputstr='6900' then
       RetTextFromStr := 'CLA 与线路保护要求不匹配!'
  else if inputstr='6901' then
       RetTextFromStr := '无效的状态!'
  else if inputstr='6981' then
       RetTextFromStr := '命令与文件结构不相容!'
  else if inputstr='6982' then
       RetTextFromStr := '不满足安全状态!'
  else if inputstr='6983' then
       RetTextFromStr := '密钥被锁死!'
  else if inputstr='6984' then
       RetTextFromStr := 'MAC格式不符合!'
  else if inputstr='6985' then
       RetTextFromStr := '使用条件不满足!'
  else if inputstr='6986' then
       RetTextFromStr := '请先选择文件!'
  else if inputstr='6987' then
       RetTextFromStr := '无安全报文!'
  else if inputstr='6988' then
       RetTextFromStr := '安全报文数据项不正确!'
  else if inputstr='6A80' then
       RetTextFromStr := '数据域参数错误!'
  else if inputstr='6A81' then
       RetTextFromStr := '功能不支持或卡中无MF 或卡片已锁定!'
  else if inputstr='6A82' then
       RetTextFromStr := '文件未找到!'
  else if inputstr='6A83' then
       RetTextFromStr := '记录未找到!'
  else if inputstr='6A84' then
       RetTextFromStr := '文件无足够空间!'
  else if inputstr='6A86' then
       RetTextFromStr := '参数P1 P2 错!'
  else if inputstr='6A88' then
       RetTextFromStr := '密钥未找到!'
  else if inputstr='6B00' then
       RetTextFromStr := '在达到Le/Lc 字节之前文件结束,偏移量错误!'
  else if inputstr='6E00' then
       RetTextFromStr := '无效的CLA!'
  else if inputstr='6F00' then
       RetTextFromStr := '数据无效!'
  else if inputstr='9302' then
       RetTextFromStr := 'MAC 错误!'
  else if inputstr='9303' then
       RetTextFromStr := '应用已被锁定!'
  else if inputstr='9401' then
       RetTextFromStr := '金额不足!'
  else if inputstr='9403' then
       RetTextFromStr := '密钥未找到!'
  else if inputstr='9406' then
       RetTextFromStr := '所需的MAC 不可用!'
  else if inputstr='91AE' then
       RetTextFromStr := '认证失败,请检查命行的参数和前期计算是否错误!'
  else if inputstr='91CA' then
       RetTextFromStr := '上一个命令未完全完成!'
  else if inputstr='917E' then
       RetTextFromStr := '指令长度错误!'
  else if inputstr='9140' then
       RetTextFromStr := '当前目录或应用密钥不存在,请先选择正确的目录或应用!'
  else if inputstr='919D' then
       RetTextFromStr := '处于未验证密码的状态,该指令无法操作!'
  else if inputstr='911E' then
       RetTextFromStr := 'MAC错误!'
  else if inputstr='91F0' then
       RetTextFromStr := '该文件号不存在!'
  else if inputstr='919E' then
       RetTextFromStr := '参数无效!'
  else if inputstr='91BE' then
       RetTextFromStr := '试图读取/写入的数据超出文件/记录的边界!'
  else if inputstr='91A0' then
       RetTextFromStr := '请求的 AID 不存在!'
  else
  begin
       if midstr(inputstr,1,3)='63C' then
       begin
           i:=strtoint('$'+midstr(inputstr,3,1));
           if i>0 then
               RetTextFromStr :=  '再试 '+inttostr(i)+' 次错误密码将锁定!'
           else
               RetTextFromStr := '密码已被锁定!' ;
       end
       else
          RetTextFromStr:='未知的异常';
  end;

end;

procedure TForm2.Button4Click(Sender: TObject);
begin
  pcdbeep(50);
end;

procedure TForm2.Button5Click(Sender: TObject);
var
   devno:array[0..3] of byte;//设备编号
   status:byte;
begin
    status:=pcdgetdevicenumber(@devno);
    if status = 0 then
    begin
        pcdbeep(50);
        Application.MessageBox(PAnsiChar(AnsiString('设备编号:'+IntToStr(devno[0]) + '-' + IntToStr(devno[1]) + '-' + IntToStr(devno[2]) + '-' + IntToStr(devno[3]))), '提示', MB_OK+MB_ICONINFORMATION);
    end
    else MessageDispInfo(status);

end;

procedure TForm2.Button16Click(Sender: TObject);
var
  AtqaSak: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;

begin
    for 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  then
    begin
        pcdbeep(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 then
        begin
            strversion:=',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);
    end
    else
    begin
          MessageDispInfo(status);
    end;
end;

procedure TForm2.Button10Click(Sender: TObject);
var
  status: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;

begin
    status := cpurequest1(@mypiccserial,@myparam,@myver,@mycode,@AtqaSak);
    if (status = 0) or (status =52) then
    begin
        pcdbeep(20);
        if(AtqaSak[0] div 64 >0) then
        begin
            cardhohex:='';
            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);
        end
        else
        begin
            cardhohex:='';
            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;
    end
    else MessageDispInfo(status);
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  aidbuf:array[0..2] of byte;
  retsw:array[0..1] of byte;
  retstr:string;
  status:byte;

begin
   if checkhexstr(Edit1.Text ,3,aidbuf)=false then    //检测AID是否合法并放入数组缓冲
   begin
       Application.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 then
      Application.MessageBox(PAnsiChar(AnsiString('选择卡内应用AID操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
   else
      Application.MessageBox(PAnsiChar(AnsiString('选择卡内应用AID操作卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;

procedure TForm2.Button2Click(Sender: TObject);
var
  authkeybuf:array[0..23] of byte;
  retsw:array[0..1] of byte;
  retstr:string;
  status:byte;
  keylen:integer;
  keyid:byte;
  keytype:byte;

begin
  if(not isrightint(RichEdit2.Lines[0])) then
  begin
       Application.MessageBox('密钥号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
       RichEdit2.SetFocus();
       RichEdit2.SelectAll();
       exit;
  end
  else keyid:=StrToInt(RichEdit2.Lines[0]);

  keytype := ComboBox4.ItemIndex;

  case ComboBox4.ItemIndex of
      0:
        keylen:=8;
      1:
        keylen:=16;
      2:
        keylen:=24;
      else
        keylen:=16;
  end;

  if checkhexstr(Edit4.Text ,keylen,authkeybuf)=false then
  begin
       Application.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 then
      Application.MessageBox(PAnsiChar(AnsiString('认证密码操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
   else
      Application.MessageBox(PAnsiChar(AnsiString('认证密码操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;

procedure TForm2.ComboBox4Change(Sender: TObject);
begin
case ComboBox4.ItemIndex of
      0:
        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';
      else
        Edit4.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 of
      0:
        begin
            Edit4.text:='00 00 00 00 00 00 00 00';
            Edit2.text:='00 00 00 00 00 00 00 00';
        end;
      1:
        begin
            Edit4.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:
        begin
            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';
            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;
      else
        begin
            Edit4.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);
var
  authkeybuf:array[0..15] of byte;
  retsw:array[0..1] of byte;
  retstr:string;
  status:byte;
  keylen:integer;
  keyid:byte;
  keytype:byte;

begin
  keylen:=16;
  if(not isrightint(RichEdit2.Lines[0])) then
  begin
       Application.MessageBox('密钥号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
       RichEdit2.SetFocus();
       RichEdit2.SelectAll();
       exit;
  end
  else keyid:=StrToInt(RichEdit2.Lines[0]);

  keytype := ComboBox2.ItemIndex;

  if checkhexstr(Edit3.Text ,keylen,authkeybuf)=false then
  begin
       Application.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 then
      Application.MessageBox(PAnsiChar(AnsiString('认证密码操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
   else
      Application.MessageBox(PAnsiChar(AnsiString('认证密码操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);


end;

procedure TForm2.Button3Click(Sender: TObject);
var
  newkeybuf: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;

begin
  if(not isrightint(RichEdit2.Lines[0])) then
  begin
       Application.MessageBox('密钥号输入框中有非法字符!', '警告', MB_OK+MB_ICONSTOP);
       RichEdit2.SetFocus();
       RichEdit2.SelectAll();
       exit;
  end
  else keyid:=StrToInt(RichEdit2.Lines[0]);

  keytype := ComboBox1.ItemIndex;

  case ComboBox1.ItemIndex of
      0:
        keylen:=8;
      1:
        keylen:=16;
      2:
        keylen:=24;
      else
        keylen:=16;
  end;

  if checkhexstr(Edit2.Text ,keylen,newkeybuf)=false then
  begin
       Application.MessageBox(PAnsiChar(AnsiString('十六进制新密钥输入错误,请输入 '+inttostr(keylen)+' 字节的16进制新密钥!')), '提示', MB_OK+MB_ICONSTOP);
       exit;
  end;

  if checkhexstr(Edit4.Text ,keylen,oldkeybuf)=false then
  begin
       Application.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 then
      Application.MessageBox(PAnsiChar(AnsiString('更改密码操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
  else
      Application.MessageBox(PAnsiChar(AnsiString('更改密码操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);

end;

procedure TForm2.Button7Click(Sender: TObject);
var
  aidbuf: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;

begin
  for i:=0 to 2 do aidbuf[i]:=0;        //picc级aid为 00 00 00
  status := desfiregetkeysettings(@aidbuf,@keysetting,@keysize,@retsw);
  retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);
  if status = 0 then
  begin
     if (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);
  end
  else if status = 94 then
      Application.MessageBox('请先执行选择应用AID:00 00 00!', '警告', MB_OK+MB_ICONSTOP)
  else
      Application.MessageBox(PAnsiChar(AnsiString('读取PICC的配置信息操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
end;

procedure TForm2.Button8Click(Sender: TObject);
var
  aidbuf:array[0..2] of byte;
  keysetting:byte;
  retsw:array[0..1] of byte;
  retstr,strls1:string;
  status:byte;
  i,keynum:integer;

begin
  for i:=0 to 2 do aidbuf[i]:=0;        //picc级aid为 00 00 00
  if ComboBox3.ItemIndex <>1 then
  begin
      Application.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 then
      Application.MessageBox(PAnsiChar(AnsiString('更改PICC的密钥配置信息操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
  else if status=94 then
      Application.MessageBox('请先执行选择应用AID:00 00 00!', '警告', MB_OK+MB_ICONSTOP)
  else if status=63 then
      Application.MessageBox('需要先验证当前应用的主密钥!', '警告', MB_OK+MB_ICONSTOP)
  else
      Application.MessageBox(PAnsiChar(AnsiString('更改PICC的配置信息操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;

procedure TForm2.Button9Click(Sender: TObject);
var
  retsw:array[0..1] of byte;
  retstr:string;
  status:byte;

begin
  status := desfireformatpicc(@retsw);
  retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);
  if status=0 then
      Application.MessageBox(PAnsiChar(AnsiString('初始格式化卡操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
  else
      Application.MessageBox(PAnsiChar(AnsiString('初始格式化卡操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;

procedure TForm2.Button11Click(Sender: TObject);
var
  aidbuf:array[0..56] of byte;
  aidsize:array[0..0] of byte;
  retsw:array[0..1] of byte;
  retstr,strls1:string;
  status:byte;
  j:integer;

begin
  status := desfiregetapplicationids(@aidbuf,@aidsize,@retsw);
  retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);
  if status=0 then
  begin
      strls1:='';
      if aidsize[0]>0 then
      begin
          strls1:= '应用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);
  end
  else
      Application.MessageBox(PAnsiChar(AnsiString('搜索当前卡片内所有应用AID操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;

procedure TForm2.Button13Click(Sender: TObject);
var
  aidbuf:array[0..2] of byte;
  retsw:array[0..1] of byte;
  retstr,strls1:string;
  status,keysetting,keynum,Encry:byte;
  j:integer;

begin
  if checkhexstr(Edit5.Text ,3,aidbuf)=false then   //检测AID是否合法并放入数组缓冲
  begin
       Application.MessageBox('十六进制AID输入错误,请输入3字节的16进制AID!', '提示', MB_OK+MB_ICONSTOP);
       Edit5.SetFocus ;
       exit;
  end;
  if aidbuf[0] + aidbuf[1] + aidbuf[2] = 0 then
  begin
       Application.MessageBox('在这里不能操作PICC级AID为00 00 00的主应用!', '提示', MB_OK+MB_ICONSTOP);
       Edit5.Text :='00 00 01' ;
       exit;
  end;
  if(not isrightint(edit7.Text)) then
  begin
       Application.MessageBox('密钥数有非法数值!', '警告', MB_OK+MB_ICONSTOP);
       edit7.SetFocus();
       edit7.SelectAll();
       exit;
  end
  else 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 then
      Application.MessageBox(PAnsiChar(AnsiString('创建新应用操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
  else
      Application.MessageBox(PAnsiChar(AnsiString('创建新应用操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;

procedure TForm2.Button14Click(Sender: TObject);
var
  aidbuf: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;

begin
  if checkhexstr(Edit5.Text ,3,aidbuf)=false then   //检测AID是否合法并放入数组缓冲
  begin
       Application.MessageBox('十六进制AID输入错误,请输入3字节的16进制AID!', '提示', MB_OK+MB_ICONSTOP);
       Edit5.SetFocus ;
       exit;
  end;
  if aidbuf[0] + aidbuf[1] + aidbuf[2] = 0 then
  begin
       Application.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 then
  begin
     ComboBox9.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 :=3
     else if (keysize[0] and $C0)=$40 then ComboBox8.ItemIndex :=2
     else ComboBox8.ItemIndex :=0 ;     ////也可能是ComboBox8.SelectedIndex = 1

     Application.MessageBox(PAnsiChar(AnsiString('读取应用的密钥配置信息操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr)+ #10+'密钥配置:'+inttohex(keysetting[0],2)+ #10+'密钥数量:'+inttostr(keynum))), '提示', MB_OK+MB_ICONINFORMATION);
  end
  else if status = 94 then
      Application.MessageBox(PAnsiChar(AnsiString('请先执行选择应用AID:'+Edit5.Text)), '警告', MB_OK+MB_ICONSTOP)
  else
      Application.MessageBox(PAnsiChar(AnsiString('读取应用的密钥配置信息操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)

end;

procedure TForm2.Button15Click(Sender: TObject);
var
  aidbuf: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;

begin
  if checkhexstr(Edit5.Text ,3,aidbuf)=false then     //检测AID是否合法并放入数组缓冲
  begin
       Application.MessageBox('十六进制AID输入错误,请输入3字节的16进制AID!', '提示', MB_OK+MB_ICONSTOP);
       Edit5.SetFocus ;
       exit;
  end;
  if aidbuf[0] + aidbuf[1] + aidbuf[2] = 0 then
  begin
       Application.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 then
      Application.MessageBox(PAnsiChar(AnsiString('更改应用密钥配置信息操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
  else if status=94 then
      Application.MessageBox(PAnsiChar(AnsiString('请先执行选择应用AID:'+Edit5.Text)), '警告', MB_OK+MB_ICONSTOP)
  else if status=63 then
      Application.MessageBox('需要先验证当前应用的主密钥!', '警告', MB_OK+MB_ICONSTOP)
  else
      Application.MessageBox(PAnsiChar(AnsiString('更改应用密钥配置信息操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);

end;

procedure TForm2.Button17Click(Sender: TObject);
var
  aidbuf: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;

begin
  if checkhexstr(Edit5.Text ,3,aidbuf)=false then     //检测AID是否合法并放入数组缓冲
  begin
       Application.MessageBox('十六进制AID输入错误,请输入3字节的16进制AID!', '提示', MB_OK+MB_ICONSTOP);
       Edit5.SetFocus ;
       exit;
  end;
  if aidbuf[0] + aidbuf[1] + aidbuf[2] = 0 then
  begin
       Application.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 then
      Application.MessageBox(PAnsiChar(AnsiString('删除卡内应用操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
  else
      Application.MessageBox(PAnsiChar(AnsiString('更删除卡内应用操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);

end;

procedure TForm2.Button12Click(Sender: TObject);
var
  fidbuf: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;

begin
  status := desfiregetfileids(@fidbuf,@fidsize,@retsw);
  retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);
  if status=0 then
  begin
      strls1:='';
      if fidsize[0]>0 then
      begin
          strls1:= '文件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);
  end
  else
      Application.MessageBox(PAnsiChar(AnsiString('搜索当前应用内所有文件ID操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);

end;

procedure TForm2.Button18Click(Sender: TObject);
var
  accessrights:array[0..1] of byte;
  retsw:array[0..1] of byte;
  retstr,strls1:string;
  status,fileid,comset:byte;
  filebytes:integer;

begin
  if(not isrightint(edit8.Text)) then
  begin
       Application.MessageBox('文件ID有非法数值!', '警告', MB_OK+MB_ICONSTOP);
       edit8.SetFocus();
       edit8.SelectAll();
       exit;
  end
  else fileid:=StrToInt(edit8.Text);

  if ComboBox14.ItemIndex <2 then
      comset:=ComboBox14.ItemIndex
  else  comset:=3;

  accessrights[1]:=ComboBox15.ItemIndex *16+ComboBox16.ItemIndex;
  accessrights[0]:=ComboBox17.ItemIndex *16+ComboBox18.ItemIndex;

  if(not isrightint(edit9.Text)) then
  begin
       Application.MessageBox('文件空间有非法数值!', '警告', MB_OK+MB_ICONSTOP);
       edit9.SetFocus();
       edit9.SelectAll();
       exit;
  end
  else filebytes:=StrToInt(edit9.Text);
  
  if RadioButton1.Checked then
      status:=desfirecreatestddatafile(fileid,comset,@accessrights,filebytes,@retsw)
  else
      status:=desfircreatebackupdatafile(fileid,comset,@accessrights,filebytes,@retsw);
  retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);
  if status=0 then
      Application.MessageBox(PAnsiChar(AnsiString('在应用内创建新文件操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
  else if status=53 then
      Application.MessageBox(PAnsiChar(AnsiString('在应用内创建新文件操作返回异常:'+inttostr(status)+',说明:CPU卡功能通讯错误,可能是卡拿开重放后未激活或卡不在感应区!')), '提示', MB_OK+MB_ICONINFORMATION)
  else if status=57 then
      begin
          if retstr='91DE' then
              Application.MessageBox(PAnsiChar(AnsiString('在应用内创建新文件操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',可能的原因:1 应用没选择对,2 密码未认证,3 文件已存在!' )), '提示', MB_OK+MB_ICONINFORMATION)
          else
              Application.MessageBox(PAnsiChar(AnsiString('在应用内创建新文件操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',请选择正确的应用!' )), '提示', MB_OK+MB_ICONINFORMATION)
      end
  else
      Application.MessageBox(PAnsiChar(AnsiString('在应用内创建新文件操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',请选择正确的应用!' )), '提示', MB_OK+MB_ICONINFORMATION);
end;

procedure TForm2.Button19Click(Sender: TObject);
var
  revbuflen: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;

begin
  if(not isrightint(edit8.Text)) then
  begin
       Application.MessageBox('文件ID有非法数值!', '警告', MB_OK+MB_ICONSTOP);
       edit8.SetFocus();
       edit8.SelectAll();
       exit;
  end
  else fileid:=StrToInt(edit8.Text);

  status:=desfiregetfilesettings(fileid,@filesettingsbuf,@revbuflen,@retsw);
  retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);
  if status=0 then
  begin
      revlen:=@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 :=2
      else if filesettingsbuf[1] mod 4=1 then
          ComboBox14.ItemIndex :=1
      else 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);
  end
  else
      Application.MessageBox(PAnsiChar(AnsiString('读取文件配置信息返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;

procedure TForm2.Button20Click(Sender: TObject);
var
  accessrights:array[0..1] of byte;
  retsw:array[0..1] of byte;
  retstr,strls1:string;
  status,fileid,comset:byte;
  filebytes:integer;

begin
  if(not isrightint(edit8.Text)) then
  begin
       Application.MessageBox('文件ID有非法数值!', '警告', MB_OK+MB_ICONSTOP);
       edit8.SetFocus();
       edit8.SelectAll();
       exit;
  end
  else fileid:=StrToInt(edit8.Text);

  if ComboBox14.ItemIndex <2 then
      comset:=ComboBox14.ItemIndex
  else  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 then
      Application.MessageBox(PAnsiChar(AnsiString('更改文件配置操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
  else if status=53 then
      Application.MessageBox(PAnsiChar(AnsiString('更改文件配置操作返回异常:'+inttostr(status)+ ',说明:CPU卡功能通讯错误,可能是卡拿开重放后未激活或卡不在感应区!')), '提示', MB_OK+MB_ICONINFORMATION)
  else
      Application.MessageBox(PAnsiChar(AnsiString('更改文件配置操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);

end;

procedure TForm2.Button21Click(Sender: TObject);
var
  retsw:array[0..1] of byte;
  retstr,strls1:string;
  status,fileid:byte;

begin
  if(not isrightint(edit8.Text)) then
  begin
       Application.MessageBox('文件ID有非法数值!', '警告', MB_OK+MB_ICONSTOP);
       edit8.SetFocus();
       edit8.SelectAll();
       exit;
  end
  else fileid:=StrToInt(edit8.Text);

  status:=desfiredeletefile(fileid, @retsw);
  retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);
  if status=0 then
      Application.MessageBox(PAnsiChar(AnsiString('删除文件操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
  else
      Application.MessageBox(PAnsiChar(AnsiString('删除文件操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);

end;

procedure TForm2.Button24Click(Sender: TObject);
var
  i,datalen:integer;
  strls2:string;
begin
  datalen:=StrToInt(edit11.Text);
  for i:=0 to datalen-1  do  strls2:=strls2+IntToHex(i,2)+' ';
  memo1.Text :=strls2;
end;

procedure TForm2.Button22Click(Sender: TObject);
var
  cmd:byte;
  databuf:array[0..8191] of byte;
  retsw:array[0..1] of byte;
  retstr,datastr:string;
  status,fileid:byte;
  rwbegin,rwlen,j:integer;

begin
  cmd:=$BD;            //参数为&HBD表示读数据,为&hBB表示读记录
  if(not isrightint(edit8.Text)) then
  begin
       Application.MessageBox('文件ID有非法数值!', '警告', MB_OK+MB_ICONSTOP);
       edit8.SetFocus();
       edit8.SelectAll();
       exit;
  end
  else 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 then
  begin
      datastr:='';
      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)
  end
  else if status=63 then
       Application.MessageBox(PAnsiChar(AnsiString('读文件操作返回异常:'+inttostr(status)+ ',说明:权限不足,请先用正确的密钥号认证!')), '提示', MB_OK+MB_ICONINFORMATION)
  else
       Application.MessageBox(PAnsiChar(AnsiString('读文件操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
end;

procedure TForm2.Button23Click(Sender: TObject);
var
  cmd:byte;
  databuf:array[0..8191] of byte;
  retsw:array[0..1] of byte;
  retstr,datastr:string;
  status,fileid:byte;
  rwbegin,rwlen,j:integer;

begin
  cmd:=$3D;            //参数为&H3D表示写数据,为&h3B表示写记录
  if(not isrightint(edit8.Text)) then
  begin
       Application.MessageBox('文件ID有非法数值!', '警告', MB_OK+MB_ICONSTOP);
       edit8.SetFocus();
       edit8.SelectAll();
       exit;
  end
  else fileid:=StrToInt(edit8.Text);

  rwbegin:=StrToInt(edit10.Text);
  rwlen:=StrToInt(edit11.Text);

  if checkhexstr(memo1.Text ,rwlen,databuf)=false then    //检测数据是否合法并放入数组缓冲
  begin
       Application.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 then
       Application.MessageBox(PAnsiChar(AnsiString('写文件操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
  else if status=63 then
       Application.MessageBox(PAnsiChar(AnsiString('写文件操作返回异常:'+inttostr(status)+ ',说明:权限不足,请先用正确的密钥号认证!')), '提示', MB_OK+MB_ICONINFORMATION)
  else
       Application.MessageBox(PAnsiChar(AnsiString('写文件操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);

end;

procedure TForm2.Button25Click(Sender: TObject);
var
  retsw:array[0..1] of byte;
  retstr:string;
  status:byte;
begin
   status:= desfirecommittransaction(@retsw);
   retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);
   if status=0 then
       Application.MessageBox(PAnsiChar(AnsiString('提交事务使备份生效操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
   else
       Application.MessageBox(PAnsiChar(AnsiString('提交事务使备份生效操作返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);
   
end;

procedure TForm2.Button26Click(Sender: TObject);
var
  retsw:array[0..1] of byte;
  retstr:string;
  status:byte;
begin
   status:= desfireaborttransaction(@retsw);
   retstr:=inttohex(retsw[0],2)+inttohex(retsw[1],2);
   if status=0 then
       Application.MessageBox(PAnsiChar(AnsiString('取消事务操作,卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION)
   else
       Application.MessageBox(PAnsiChar(AnsiString('取消事务返回异常:'+inttostr(status)+',卡片返回代码:' + retstr + ',说明:' + RetTextFromStr(retstr))), '提示', MB_OK+MB_ICONINFORMATION);

end;

end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vx_13822155058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值