C#加密狗程序

在购买加密狗时,厂家通常会附带有开发手册和一张光盘。开发手册中介绍了加密狗的使用方法和开发资料。本例使用赛孚耐信息技术有限公司的加密狗产品,该产品提供了.NET中非托管的类库,来完成加密狗的数据读写功能。下面介绍有关加密狗的类库中的读写函数。

  ● DogWrite 函数

该函数将pdogData指向的数据写入加密狗中,从DogAddr地址开始写入,到DogBytes地址停止。

函数声明如下:

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

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

参数说明如下。

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\”文件夹中。

写一个对加密狗的读写类

ContractedBlock.gif ExpandedBlockStart.gif Code
[StructLayout(LayoutKind.Sequential)]

//这个类用于读写加密狗

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); //将数据写入加密狗

        }

    }

}

 

下面是读写方法
ContractedBlock.gif ExpandedBlockStart.gif Code
       /// <summary>
        
/// 写入加密狗
        
/// </summary>
        public void Dogwrite()
        {
            Dog dog 
= new Dog(100);
            dog.DogAddr 
= 0;
            dog.DogBytes 
= 10;
            
string str = "123456";
            
for (int i = 0; i < str.Length; i++)
            {
                dog.DogData[i] 
= (byte)str[i];
            }
            dog.WriteDog();

            
if (Dogred() == str)//写入后马上读出来 检测一下是否成功
            {
                MessageBox.Show(
"密码已成功写入加密狗!""成功提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            
else
            {
                MessageBox.Show(
"密码写入加密狗失败,请确认是否安装加密狗驱动!""失败提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        
/// <summary>
        
/// 读出密码
        
/// </summary>
        
/// <returns></returns>
        public string Dogred()
        {
            
string str1 = "123456";
            
string str = "";
            Dog dog 
= new Dog(100);

            dog.DogAddr 
= 0;

            dog.DogBytes 
= 10;

            dog.ReadDog();

            
if (dog.Retcode == 0)   //开始读加密狗数据
            {

                
char[] chTemp = new char[str1.Length];

                
for (int i = 0; i < str1.Length; i++)
                {

                    chTemp[i] 
= (char)dog.DogData[i];

                }

                 str 
= new String(chTemp);
            }
            
return str;
        }

转载于:https://www.cnblogs.com/bobofsj11/archive/2009/08/31/1557133.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值