⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit_pubfunc.pas

📁 ICCard读写程序,用来在windows环境下读写IC卡内数据!
💻 PAS
📖 第 1 页 / 共 4 页
字号:
unit Unit_PubFunc;

interface
uses
  SysUtils, Windows, Forms, Unit_DataDefs;

//Function Definitions
  procedure ShowSW1SW2( ReturnCode: TReturnCode; ErrMsg: String );
  procedure ShowMessageForm( MsgTips: String );
  procedure CloseMessageForm;
  function  ResetCard:SmallInt;
  function  SelectMainDirectory:SmallInt;
  function  SelectAppDirectory: SmallInt;
  function  GetEmployeeInfo:    SmallInt;
  function  SetEmployeeInfo:    SmallInt;
  function  CreateNewCard:      SmallInt;
  function  CreateMF:           SmallInt;
  function  CreateATRFile:      SmallInt;
  function  UpdateATRFile:      SmallInt;
  function  CreateDF:           SmallInt;
  function  CreateKeyFile:      SmallInt;
  function  AddPinKey:          SmallInt;
  function  AddExternalAuthKey: SmallInt;
  function  AddCashWithdrawKey: SmallInt;
  function  AddLoadKey:         SmallInt;
  function  AddUnloadKey:       SmallInt;
  function  AddTACKey:          SmallInt;
  function  AddApplicationKey:  SmallInt;
  function  AddPinUnlockKey:    SmallInt;
  function  AddPinReloadKey:    SmallInt;
  function  AddOverdrawKey:     SmallInt;
  function  CreatePubAppFile:   SmallInt;
  function  UpdatePubAppFile:   SmallInt;
  function  CreateEmpInfoFile:  SmallInt;
  function  UpdateEmpInfoFile:  SmallInt;
  function  CreateDetailFile:   SmallInt;
  function  CreateEPFile:       SmallInt;
  function  CreateEDFile:       SmallInt;
  function  CreateDFEnd:        SmallInt;
  function  CreateMFEnd:        SmallInt;
implementation
uses
  Unit_MsgTips, Unit_DllFunc;

procedure ShowSW1SW2( ReturnCode: TReturnCode; ErrMsg: String );
var
  SW1, SW2: array[0..2] of char;
  TempErrMsg:            String;
begin
  FillChar( SW1, sizeof( SW1 ), 0 );
  FillChar( SW2, sizeof( SW2 ), 0 );
  hex_asc( ReturnCode.SW1, SW1, 1 );
  hex_asc( ReturnCode.SW2, SW2, 1 );

  TempErrMsg:= Format('%s。'#10'错误码[%s%s]', [ErrMsg, SW1, SW2] );
  MessageBox(Application.Handle, PChar( TempErrMsg ), '错误', MB_OK+MB_ICONERROR);
end;

procedure ShowMessageForm( MsgTips: String );
begin
  Form_MsgTips.LBL_MSGTIPS.Caption:=MsgTips;
  Form_MsgTips.Show;
  Form_MsgTips.Update;
end;

procedure CloseMessageForm;
begin
  Form_MsgTips.Close;
end;

function  ResetCard:SmallInt;
var
  recv_buf: array[0..100] of Byte;
  ReturnValue: SmallInt;
begin
  FillChar( recv_buf, sizeof( recv_buf ), 0 );
  ReturnValue:= cpu_reset( icdev, recv_buf[0] );
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

  if( ((recv_buf[5]) and $ff) shr 4 = 0 ) then
    CardStatus:= csNotBeenPersonalized;

  if( ((recv_buf[5]) and $ff) shr 4 = 1 ) then
    CardStatus:= csLockedNotCompleted;

  if( ((recv_buf[5]) and $ff) shr 4 = 7 ) then
    CardStatus:= csLockedCompleted;

  if( ((recv_buf[5]) and $ff) shr 4 = 6 ) then
    CardStatus:= csPersonalizedCompleted;

  if( ((recv_buf[5]) and $ff) shr 4 = 2 ) then
    CardStatus:= csPersonalizedNotCompleted;

  Result:=0;
end;

function  SelectMainDirectory:SmallInt;
var
  send_buf:      array[0..20]  of Byte;
  recv_buf:      array[0..100] of Byte;
  ReturnValue:   SmallInt;
  i:             Integer;
begin
  FillChar( send_buf, sizeof(send_buf), 0 );
  FillChar( recv_buf, sizeof(send_buf), 0 );

  send_buf[0]:= $0  ;
  send_buf[1]:= $40 ;
  send_buf[2]:= $7  ;
  send_buf[3]:= $0  ;
  send_buf[4]:= $a4 ;
  send_buf[5]:= $0  ;
  send_buf[6]:= $0  ;
  send_buf[7]:= $02 ;
  send_buf[8]:= $3f ;
  send_buf[9]:= $0  ;
  send_buf[10]:=$0  ;

  for i:=0 to 9 do
    send_buf[10]:= send_buf[i] XOR send_buf[10];

  ReturnValue:= cpu_protocol( icdev, send_buf[2]+4, send_buf[0], recv_buf[0] );
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

  if not((recv_buf[recv_buf[2]+1]=$90)and(recv_buf[recv_buf[2]+2]=$0)) then
  begin
    ReturnCode.SW1:= recv_buf[recv_buf[2]+1];
    ReturnCode.SW2:= recv_buf[recv_buf[2]+2];
    Result:= -1;
  end;

  Result:=0;
end;

function  SelectAppDirectory: SmallInt;
var
  send_buf:      array[0..20]  of Byte;
  recv_buf:      array[0..100] of Byte;
  ReturnValue:   SmallInt;
  i:             Integer;
begin
  FillChar( send_buf, sizeof(send_buf), 0 );
  FillChar( recv_buf, sizeof(send_buf), 0 );

  send_buf[0]:= $00 ;
  send_buf[1]:= $40 ;
  send_buf[2]:= $07 ;
  send_buf[3]:= $00 ;
  send_buf[4]:= $a4 ;
  send_buf[5]:= $00 ;
  send_buf[6]:= $00 ;
  send_buf[7]:= $02 ;
  send_buf[8]:= $2f ;
  send_buf[9]:= $01 ;
  send_buf[10]:=$00 ;

  for i:=0 to 9 do
    send_buf[10]:= send_buf[i] XOR send_buf[10];

  ReturnValue:= cpu_protocol( icdev, send_buf[2]+4, send_buf[0], recv_buf[0] );
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

  if not((recv_buf[recv_buf[2]+1]=$90)and(recv_buf[recv_buf[2]+2]=$0)) then
  begin
    ReturnCode.SW1:= recv_buf[recv_buf[2]+1];
    ReturnCode.SW2:= recv_buf[recv_buf[2]+2];
    ShowSW1SW2( ReturnCode, '选择应用文件失败' );
    Result:= -1;
  end;

  Result:=0;
end;

function  GetEmployeeInfo:    SmallInt;
var
  send_buf:      array[0..20]  of Byte;
  recv_buf:      array[0..100] of Byte;
  char_buf:      array[0..100] of char;
  ReturnValue:   SmallInt;
  i:             Integer;
begin
  FillChar( send_buf, sizeof(send_buf), 0 );
  FillChar( recv_buf, sizeof(send_buf), 0 );
  FillChar( char_buf, sizeof(char_buf), 0 );

  ReturnValue:= ResetCard;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

  if( CardStatus = csNotBeenPersonalized ) then
  begin
     MessageBox( Application.Handle, '该卡未个人化', '错误', MB_OK+MB_ICONERROR );
     Result:= -1;
     Exit;
  end;

  ReturnValue:= SelectAppDirectory;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

  send_buf[0]:= $0  ;
  send_buf[1]:= $0 ;
  send_buf[2]:= $5  ;
  send_buf[3]:= $0  ;
  send_buf[4]:= $b0 ;
  send_buf[5]:= $96 ;
  send_buf[6]:= $0  ;
  send_buf[7]:= $56 ;
  send_buf[8]:= $0  ;

  for i:=0 to 7 do
    send_buf[8]:= send_buf[i] XOR send_buf[8];

  ReturnValue:= cpu_protocol( icdev, send_buf[2]+4, send_buf[0], recv_buf[0] );
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

  if (recv_buf[recv_buf[2]+1]<>$90)OR(recv_buf[recv_buf[2]+2]<>$0) then
  begin
    ReturnCode.SW1:= recv_buf[recv_buf[2]+1];
    ReturnCode.SW2:= recv_buf[recv_buf[2]+2];
    ShowSW1SW2( ReturnCode, '读取柜员信息失败' );
    Result:= -1;
    Exit;
  end;

  for i:=0 to recv_buf[2]-2 do
    char_buf[i]:= chr( recv_buf[i+3] );

  FillChar( EmployeeInfo, sizeof(EmployeeInfo), 0 );
  StrPLCopy( EmployeeInfo.JGM, Copy( char_buf, 1, 10 ), 10);
  StrPLCopy( EmployeeInfo.GYH, Copy( char_buf, 12, 6 ), 6 );
  StrPLCopy( EmployeeInfo.XM,  Copy( char_buf, 19, 10 ), 10 );
  StrPLCopy( EmployeeInfo.ZJLX, Copy( char_buf, 30, 1 ), 1 );
  StrPLCopy( EmployeeInfo.ZJHM, Copy( char_buf, 32, 20), 20 );
  StrPLCopy( EmployeeInfo.KZT, Copy( char_buf, 53, 1 ), 1);
  StrPLCopy( EmployeeInfo.KH, Copy( char_buf, 55, 14 ), 14);
  Result:=0;
end;

function  SetEmployeeInfo:    SmallInt;
var
  send_buf:      array[0..100] of Byte;
  recv_buf:      array[0..100] of Byte;
  char_buf:      array[0..100] of char;
  ReturnValue:   SmallInt;
  i, Len:        Integer;
begin
  FillChar( send_buf, sizeof(send_buf), 0 );
  FillChar( recv_buf, sizeof(send_buf), 0 );
  FillChar( char_buf, sizeof(char_buf), 0 );

  FillChar( EmployeeInfo.BL, sizeof( EmployeeInfo.BL )-1, '0' );
  StrPCopy( char_buf, Format('%10s %6s %-10s %1s %-20s %1s %14s %17s',
                             [ EmployeeInfo.JGM,
                               EmployeeInfo.GYH,
                               EmployeeInfo.XM,
                               EmployeeInfo.ZJLX,
                               EmployeeInfo.ZJHM,
                               EmployeeInfo.KZT,
                               EmployeeInfo.KH,
                               EmployeeInfo.BL ] ));
  Len:= Length( char_buf );
  for i:=0 to Len do
    send_buf[8+i]:= ord(char_buf[i]);

  ReturnValue:= ResetCard;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

  ReturnValue:= SelectMainDirectory;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

  ReturnValue:= SelectAppDirectory;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

  send_buf[0]:= $0  ;
  send_buf[1]:= $0 ;
  send_buf[2]:= $5+$56 ;
  send_buf[3]:= $0  ;
  send_buf[4]:= $d6 ;
  send_buf[5]:= $96 ;
  send_buf[6]:= $0  ;
  send_buf[7]:= $56 ;

  for i:=0 to send_buf[2]+2 do
    send_buf[send_buf[2]+3]:= send_buf[i] XOR send_buf[send_buf[2]+3];

  ReturnValue:= cpu_protocol( icdev, send_buf[2]+4, send_buf[0], recv_buf[0] );
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

  if (recv_buf[recv_buf[2]+1]<>$90)OR(recv_buf[recv_buf[2]+2]<>$0) then
  begin
    ReturnCode.SW1:= recv_buf[recv_buf[2]+1];
    ReturnCode.SW2:= recv_buf[recv_buf[2]+2];
    Result:= -1;
  end;

  Result:=0;
end;

function CreateNewCard:    SmallInt;
var
  send_buf:      array[0..100] of Byte;
  recv_buf:      array[0..100] of Byte;
  ReturnValue:   SmallInt;
  i:             integer;
begin

  ReturnValue:= ResetCard;
  if ( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

  if( CardStatus <> csNotBeenPersonalized ) then
  begin
    MessageBox( Application.Handle, '卡状态不对', '错误', MB_OK+MB_ICONERROR );
    Result:= -1;
    Exit;
  end;

//Create Master File
  ReturnValue:= CreateMF;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

//Create ATR File
  ReturnValue:= CreateATRFile;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

//Update ATR File
  ReturnValue:= UpdateATRFile;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

//Create DF
  ReturnValue:= CreateDF;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

  
//Create Key File
  ReturnValue:= CreateKeyFile;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

//Add Pin Key
  ReturnValue:= AddPinKey;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

//Add External Authentication Key
  ReturnValue:= AddExternalAuthKey;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

//Add Cash Withdraw Key
  ReturnValue:= AddCashWithdrawKey;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

//Add Credit For Load Key
  ReturnValue:= AddLoadKey;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

//Add Credit For Unload Key
  ReturnValue:= AddUnloadKey;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;


//Add TAC Key
  ReturnValue:= AddTACKey;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

//Add Application Maintain Key
  ReturnValue:= AddApplicationKey;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

//Add PIN Unlock Key
  ReturnValue:= AddPinUnlockKey;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;


//Add PIN Reload Key
  ReturnValue:= AddPinReloadKey;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

//Add Update Overdraw Limit Key
  ReturnValue:= AddOverdrawKey;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

//Create Public Application File
  ReturnValue:= CreatePubAppFile;
  if( ReturnValue<>0 ) then
  begin
    Result:= ReturnValue;
    Exit;
  end;

//Update Public Application File

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -