Hetter

加密狗

今天看了一下项目中涉及到的知识点,察觉到该项目中里边有许多知识以前从未接触过,顺便将其整理下来.

加密狗

主要是用来加密个人重要的数据.我们对其的操作主要就是读写加密狗里边的信息.

介绍一下赛孚耐信息技术有限公司的加密狗产品,该产品提供了.NET中非托管的类库,来完成加密狗的数据读写功能。

 ● DogWrite 函数(写如加密狗)

 [DllImport("Win32dll.dll", CharSet = CharSet.Ansi)]

    public static unsafe extern uint DogWrite(uint idogBytes, uint idogAddr, byte* pdogData);

将PdogData指向的数据块写入加密狗中,从idogAddr处开始,长度共长idogBytes.

参数说明如下。

l     idogAddr:对软件狗读写操作时用户区中的首地址。取值范围为0~99。

l     IdogBytes:对软件狗读写操作时的字节长度。读写时取值范围为1~100,并且与idogAddr之和不能超过100。

l     pdogData:指针型变量。指向读写操作或变换的数据缓冲区。

l     返回值:0表示操作成功,其他值是错误码。


● DogRead函数(读加密狗里边的信息)

该函数从加密狗中的idogAddr开始的存储区读出数据,存入pdogData指定的缓冲区,读出字节数为idogBytes。切记,缓冲区大小要足够长。

函数声明如下:

[DllImport("Win32dll.dll", CharSet = CharSet.Ansi)]

    public static unsafe extern uint DogRead(uint idogBytes, uint idogAddr, byte* pdogData);

参数说明如下。

l     idogAddr:对软件狗读写操作时用户区中的首地址。取值范围为0~99。

l     idogBytes:对软件狗读写操作时的字节长度。读写时取值范围为1~100,并且与idogAddr之和不能超过100。

l     pdogData:指针型变量。指向读写操作或变换的数据缓冲区。

l     返回值:0表示操作成功,其他值是错误码。

 注意以下几点。

在使用这个函数之前,必须将随加密狗附带的安装程序安装完整,并将安装目录下的Win32dll.dll文件复制到系统目录下。例如:

在Windows 2003下将安装目录下的“\SafeNet China\SoftDog SDK V3.1\Win32\Win32dll\HighDll\ Win32dll.dll”文件复制到“C:\WINDOWS\system32\”文件夹中。

也不知道其他公司产品的加密狗的原理是不是也是这样的,不过我感觉加密狗的基本原理都是这样的,公司提供加密狗的API接口.只不过不同的公司所提供的API接口不一样罢了.

实例:

C#:


using System.Runtime.InteropServices ;

加密狗类:

public unsafe class Dog

{

    public uint DogBytes, DogAddr;  //设置加密狗字节长度和起始地址

    public byte[] DogData;  //设置数据的长度

    public uint Retcode;

    [DllImport("Win32dll.dll", CharSet = CharSet.Ansi)]

    public static unsafe extern uint DogRead(uint idogBytes, uint idogAddr, byte* pdogData);

    [DllImport("Win32dll.dll", CharSet = CharSet.Ansi)]

    public static unsafe extern uint DogWrite(uint idogBytes, uint idogAddr, byte* pdogData);

    public unsafe Dog(ushort num)

    {

        DogBytes = num;

        DogData = new byte[DogBytes]; //设置数据的长度

    }

    public unsafe void ReadDog()

    {

        fixed (byte* pDogData = &DogData[0])

        {

            Retcode = DogRead(DogBytes, DogAddr, pDogData);  //将数据读出加密狗

        }

    }

    public unsafe void WriteDog()

    {

        fixed (byte* pDogData = &DogData[0])

        {

            Retcode = DogWrite(DogBytes, DogAddr, pDogData); //将数据写入加密狗

        }

    }

}

向加密狗里边写入数据:Dog d=new Dog(10);d.DogAddr=0;for(int i=0;i<d.DogData.length;i++)d.DogData[i]=i;d.WriteDog();

读取加密狗里边的数据:Dog d=new Dog(10);byte[] b;d.ReadDog();b=new byte[d.DogData.length];

for (int i=0;i<d.DogData.lengh;i++)b[i]=d.DogData[i];

extern:表明我们用到的该方法是在外部声明的,即该方法是在win32dll.dll程序集里边;

unsafe:表明该加密狗类是不安全的.


 

 

 

 

posted on 2009-03-28 15:03  Hetter  阅读(3008)  评论(5)    收藏  举报

导航