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

 时间:2026-02-15 14:05:14

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类给予调用。编译通过实现。

  • win10升级版本后AutoCAD2013出现闪退的解决办法
  • Win10怎么样禁止用户安装软件 如何限制安装应用
  • 梦幻新诛仙死亡沼泽奇缘任务怎么做
  • 如何快速还原系统,还能保证原来软件能用?
  • 如何通过MSDN下载微软官方的软件
  • 热门搜索
    五花肉的做法大全 韶怎么读 苹果派的做法 鸿运当头花怎么养 番茄炒蛋的做法 小孩食谱大全及做法 土豆炖鸡块的家常做法 灏怎么读 耳鸣怎么办 贺卡怎么做