NOX5硬件加密锁API Demo

unit Testfrm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Nox5AppApis;

type
TTestForm = class(TForm)
    ListBox: TListBox;
    btnTest: TButton;
    btnExit: TButton;
    btnplan: TButton;
    btncos: TButton;
    procedure btnTestClick(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure btnplanClick(Sender: TObject);
    procedure btncosClick(Sender: TObject);
private
    { Private declarations }
    Rtn:Integer;                  //函数返回值
    procedure ListMSG(S:String); //LISTBOX INSERT
    procedure RtnMSG(sTrue,sFalse:String); 
public
    { Public declarations }
end;

var
TestForm: TTestForm;

implementation

{$R *.dfm}

procedure TTestForm.btnTestClick(Sender: TObject);
var
nKeyHandle    :array [0..7] of Integer;
nKeyNumber    :Integer;
APPID         :Integer;              //应用程序标识
GUID          :array [0..31] of AnsiChar;//硬件唯一ID
sRandom       :array [0..63] of Byte;//随机数
uPin          :array [0..32] of AnsiChar;               //用户密码
StorageBuffer :array [0..1023] of AnsiChar;//掉电保持区数据
MemBuffer     :array [0..31] of AnsiChar; //内存区数据
str           :String;
i             :Integer; 
begin
ListBox.Items.Clear;
//NoxFind
APPID:=$FFFFFFFF;//应用程序标识,通过设号工具设置
Rtn:=NoxFind(APPID,@nKeyHandle[0],@nKeyNumber);
RtnMSG('Find『'+IntToStr(nKeyNumber)+'』NOX5!','Not find');

//NoxGetUID
Rtn:=NoxGetUID(nkeyHandle[0],@GUID); //硬件序列号为32位长
RtnMSG('GUID:'+GUID,'Get GUID fail' );

//NoxGenRandom
Rtn:=NoxGenRandom(nKeyHandle[0],@sRandom); //锁中生成64字节随机数,用户可截取部分使用
if Rtn=0 then
begin
    Str:='';
    for i :=0 to 15 do          //截取16个字节数据
      Str:=Str+IntToHex(sRandom[i],2);
end;
self.RtnMSG('Random:'+Str,'GenRandom fail');

//NoxOpen
uPin:='72a9f8917063ed9717dbfb39ce9704b5'; //用户密码通过设号工具设置,由超级密码和种子码共同生成
Rtn:=NoxOpen(nKeyHandle[0],@uPin);
RtnMSG('NoxOpen succeed!','NoxOpen fail' );

//NoxWriteStorage
strcopy(StorageBuffer,PAnsiChar('hello delphi'));
Rtn:=NoxWriteStorage(nKeyHandle[0],0,0,1024,@StorageBuffer); //在第一分页,从地址0开始,写入1024字节
RtnMSG('NoxWriteStorage succeed!','NoxWriteStorage fail');

//NoxReadStorage
Rtn:=NoxReadStorage(nKeyHandle[0],0,0,1024,@StorageBuffer);
RtnMSG('NoxReadStorage succeed! data:'+StorageBuffer,'NoxReadStorage fail');

//NoxWriteMemory
strcopy(MemBuffer,PAnsiChar('AABBCCDD'));//将部分随机数写入内存区
Rtn:= NoxWriteMemory(nKeyHandle[0],@MemBuffer);
RtnMSG('NoxWriteMemory succeed!','NoxWriteMemory fail');

//NoxReadMemory
Rtn:= NoxReadMemory(nKeyHandle[0],@MemBuffer);
RtnMSG('NoxReadMemory succeed!MemBuffer:'+MemBuffer,'NoxReadMemory fail');

//NoxClose
Rtn:=NoxClose(nKeyHandle[0]);
RtnMSG('NoxClose succeed!','NoxClose fail');
end;

procedure TTestForm.ListMSG(S: String);
begin
ListBox.Items.Add(FormatDateTime('tt',now)+':'+S);
end;

procedure TTestForm.RtnMSG(sTrue, sFalse: String);
begin
if Rtn=0 then ListMSG(sTrue) else ListMSG(sFalse+' ErrorCode:'+IntToStr(NoxGetLastError()));
end;

procedure TTestForm.btnExitClick(Sender: TObject);
begin
Close;
end;

procedure TTestForm.btnplanClick(Sender: TObject);
var
nKeyHandle    :array [0..7] of Integer;
nKeyNumber    :Integer;
APPID         :Integer;              //应用程序标识
uPin          :array [0..32] of AnsiChar;               //用户密码
IV            :array [0..8] of AnsiChar; //初始向量
datalen       :Integer;//3DES加密数据的长度
pDataBuffer   :array [0..32] of AnsiChar;//3DES加密的数据及结果
pEFile        :array [0..128] of AnsiChar;//3DES加密的文件
pDFile        :array [0..128] of AnsiChar;//3DES加密后的文件
pubkey        :array [0..131] of Byte;//RSA密钥
DataBuffer    :array [0..127] of AnsiChar;//RSA加密的数据及结果
msg           :array [0..31] of AnsiChar;//MD5消息
msglen        :Integer;//消息长度
digest        :array [0..15] of Byte;
MD5File       :array [0..128] of AnsiChar;
str           :String;
i             :Integer;
begin
ListBox.Items.Clear;
//NoxFind
APPID:=$FFFFFFFF;//应用程序标识,通过设号工具设置
Rtn:=NoxFind(APPID,@nKeyHandle[0],@nKeyNumber);
RtnMSG('Find『'+IntToStr(nKeyNumber)+'』NOX5!','Not find');

//NoxOpen
uPin:='72a9f8917063ed9717dbfb39ce9704b5'; //用户密码通过设号工具设置,由超级密码和种子码共同生成
Rtn:=NoxOpen(nKeyHandle[0],@uPin);
RtnMSG('NoxOpen succeed!','NoxOpen fail' );

//3DES------------------------------------------
//NOX5中支持的3DES算法密钥长度为192位,即24字节
//3DES算法为对称算法,加密解密使用同一密钥,密钥通过工具设置并保存在加密锁内
//3DES每次加密解密数据的长度必须为8的倍数,否则将返回无效参数的错误

//IV为3DES算法的8字节初始化向量,可为任意值,只是用来加强安全性,无需保密,但是加解密必须使用同样的IV
//Nox3DESEncrypt
for i := 0 to 7 do
    IV[i]:='F';
for i := 0 to 31 do
    pDataBuffer[i]:='F';
datalen:=32; //datalen为pDataBuffer的长度
Rtn:= Nox3DESEncrypt(nKeyHandle[0],@IV,datalen,@pDataBuffer);
self.RtnMSG('Nox3DESEncrypt succeed!','Nox3DESEncrypt fail');

Rtn:= Nox3DESDecrypt(nKeyHandle[0],@IV,datalen,@pDataBuffer);
self.RtnMSG('Nox3DESDecrypt succeed!pDataBuffer:'+pDataBuffer,'Nox3DESDecrypt fail');


//加密同一目录下的 3DESReadme.txt文件,加密后的文件为:3DESReadme.txt_3des
//NOX5使用3DES进行文件加解密操作,密钥通过设号工具来设置
//由于硬件的运行和通讯速度问题, 当对大文件进行操作时,有明显的延迟等待,建议不要对大于1M 的文件进行操作
StrCopy(pEFile,PAnsiChar('3DESReadme.txt'));
StrCopy(pDFile,PAnsiChar('3DESReadme.txt_3des'));
Rtn:=Nox3DESEncryptFile(nKeyHandle[0],@iv,@pEFile,@pDFile);
self.RtnMSG('Nox3DESEncryptFile succeed!','Nox3DESEncryptFile fail');

Rtn:=Nox3DESDecryptFile(nKeyHandle[0],@iv,@pDFile,@pEFile);
self.RtnMSG('Nox3DESDecryptFile succeed!','Nox3DESDecryptFile fail');


//RSA---------------------------------------- 执行RSA,必须用设号工具生成RSA密钥对
//NOX5中支持的RSA算法为1024位,即128字节
//RSA算法为非对称算法, 即加解密使用不同的密钥密,加密用公钥,解密用私钥
//RSA的密钥对需要通过设号工具来生成, 公钥可以导出,私钥不可导出
//RSA算法每次加解密数据的长度要求128字节
//NoxRSAExportPublicKey
Rtn:=NoxRSAExportPublicKey(nKeyHandle[0],@pubkey);
self.RtnMSG('NoxRSAExportPublicKey succeed','NoxRSAExportPublicKey fail');

//NoxRSAPublic
for i :=0 to 127 do
    DataBuffer[i]:='F';
Rtn:=NoxRSAPublic(nKeyHandle[0],@DataBuffer); //加密后将结果输出给DataBuffer
self.RtnMSG('NoxRSAPublic succeed!','NoxRSAPublic fail');

//NoxRSAPrivate
Rtn:=NoxRSAPrivate(nKeyHandle[0],@DataBuffer); //解密后将结果输出给DataBuffer
self.RtnMSG('NoxRSAPrivate succeed!','NoxRSAPrivate fail');


//MD5----------------------------------------
//NoxHASHMD5
for i := 0 to 31 do
    msg[i]:='F';
msglen:=32;
Rtn:= NoxHASHMD5(nkeyHandle[0],@msg,msglen,@digest); //msglen为MSG的长度
if Rtn=0 then
begin
    Str:='';
    for i := 0 to 15 do
      Str:=Str+lowercase(IntToHex(digest[i],2));
end;
self.RtnMSG('NoxHASHMD5 succeed!Digest:'+str,'NoxHASHMD5 fail');

//NoxHASHMD5File
//由于硬件的运行和通讯速度问题, 当对大文件进行操作时,有明显的延迟等待,建议不要对大于1M 的文件进行操作
StrCopy(MD5File,PAnsiChar('MD5Readme.txt'));
Rtn:= NoxHASHMD5File(nKeyHandle[0],@MD5File,@digest); //校验同一目录下的MD5Readme.txt文件,
if Rtn=0 then                                                       
begin
    Str:='';
    for i := 0 to 15 do
      Str:=Str+lowercase(IntToHex(digest[i],2));
end;
self.RtnMSG('NoxHASHMD5File succeed!Digest:'+str,'NoxHASHMD5File fail');

//NoxClose
Rtn:=NoxClose(nKeyHandle[0]);
RtnMSG('NoxClose succeed!','NoxClose fail');
end;

procedure TTestForm.btncosClick(Sender: TObject);
var
nKeyHandle    :array [0..7] of Integer;
nKeyNumber    :Integer;
APPID         :Integer;              //应用程序标识
uPin          :array [0..32] of AnsiChar;               //用户密码
Pram1,Pram2,Pram3,Pram4:Integer;//执行函数的四个参数
begin
ListBox.Items.Clear;
//NoxFind
APPID:=$FFFFFFFF;//应用程序标识,通过设号工具设置
Rtn:=NoxFind(APPID,@nKeyHandle[0],@nKeyNumber);
RtnMSG('Find『'+IntToStr(nKeyNumber)+'』NOX5!','Not find');

//NoxOpen
uPin:='72a9f8917063ed9717dbfb39ce9704b5'; //用户密码通过设号工具设置,由超级密码和种子码共同生成
Rtn:=NoxOpen(nKeyHandle[0],@uPin);
RtnMSG('NoxOpen succeed!','NoxOpen fail' );

//NoxExecute
//使用设号工具下载同一目录下的 冒泡算法.bin ,源代码为 冒泡算法.C
//执行加密锁中的下载的代码(需要通过编程工具进行编译下载)
//执行成功后,Para1,Para2,Para3,Para4中即保存了相应的执行结果
Pram1:=44;   //初始化参数
Pram2:=88;
Pram3:=30;
Pram4:=40;
Rtn:=NoxExecute(nKeyHandle[0],@Pram1,@Pram2,@Pram3,@Pram4); //执行此函数必须确保锁中已下载编译后的文件
RtnMSG('NoxExecute succeed!','NoxExecute fail');
if Rtn=0 then ListMSG('Param1:'+IntToStr(Pram1)+' Param2:'+IntToStr(Pram2)+' Param3:'+IntToStr(Pram3)+' Param4:'+IntToStr(Pram4));

//NoxClose
Rtn:=NoxClose(nKeyHandle[0]);
RtnMSG('NoxClose succeed!','NoxClose fail');
end;

end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值