.net密码写入与读出加密狗

2025-10-01 22:52:09

1、新建一个项目,命名随便,主窗体设置为Dog

.net密码写入与读出加密狗

2、向窗体中添加两个button与textbox控件,用于操作,如图所示:

.net密码写入与读出加密狗

3、在准备操作之前,要应用系统的一些方法。代码如下:

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

        }

    }

.net密码写入与读出加密狗

4、双击button1,添加如下代码。

  Dog dog = new Dog(100);

            dog.DogAddr = 0;        

            dog.DogBytes = 10;    

            string str = textBox1.Text;

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

            {

                dog.DogData[i] = (byte)str[i];

            }

            dog.WriteDog();

            MessageBox.Show("密码已成功写入加密狗!", "成功提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);

            textBox1.ReadOnly = true;

            button1.Enabled = false;

            button2.Enabled = true;

.net密码写入与读出加密狗

5、同样双击button2,添加

  Dog dog = new Dog(100);

            dog.DogAddr = 0;        

            dog.DogBytes = 10;    

            dog.ReadDog();

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

            {

                char[] chTemp = new char[textBox1.Text.Length];

                for (int i = 0; i < textBox1.Text.Length; i++)

                {

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

                }

                String str = new String(chTemp);

                textBox2.Text = str;

            }

            else

            {

                textBox2.Text = "2:" + dog.Retcode;

            }

            textBox1.ReadOnly = false;

            button2.Enabled = false;

            button1.Enabled = true;

.net密码写入与读出加密狗

6、记得把第一步骤的方法封装成dog类给予调用。编译通过实现。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢