博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

赛孚耐(SafeNet)加密狗 C#调用代码

Posted on 2021-01-06 13:30  PHP-张工  阅读(1533)  评论(0编辑  收藏  举报

赛孚耐(SafeNet)加密狗

购买链接:https://item.jd.com/21653904499.html

需求:

加密狗作用:如果未插入加密狗,则程序无法运行。
我们会给每个客户一个加密狗,希望通过加密狗编号自动对应客户。

加密狗制作流程:

1. 安装《超级加密狗工具包》,通过开发商向导安装函数库。
2. 插入加密狗driver和开发狗master
3. 使用《外壳加密工具》加密程序
4. 使用《授权管理工具》授权加密狗有效期

官方给的API调用文档,太复杂了,看得人晕头转向。整理下备忘。

using SuperDog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinFrmTest
{
    public partial class Form1 : Form
    {
        // C:\Program Files (x86)\Gemalto\SuperDog\2.5\VendorCodes\FFWWC.hvc
        const string vcode = "xxxx";

        public Form1()
        {
            InitializeComponent();
        }

        private void showInfo(string str)
        {
            textBox1.AppendText(str + System.Environment.NewLine);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DogStatus res = DogStatus.StatusOk;

            showInfo("获取加密狗信息");

            string a1 = ""+
            "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
            "<dogscope/>";

            string a2 = "" +
            "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
            "<dogformat root=\"dog_info\">" +
            "    <dog>" +
            "        <attribute name=\"id\" />" +
            "        <attribute name=\"type\" />" +
            "        <feature>" +
            "            <attribute name=\"id\" />" +
            "        </feature>" +
            "    </dog>" +
            "</dogformat>";

            string info = "";
            res = Dog.GetInfo(a1, a2, vcode, ref info);

            if (res != DogStatus.StatusOk)
            {
                showInfo("错误:" + res.ToString());
                return;
            }

            showInfo(info);

            using (var dog = new Dog(new DogFeature(DogFeature.FromFeature(0).Feature)))
            {
                showInfo("验证加密狗");
                res = dog.Login(vcode);
                if (res != DogStatus.StatusOk)
                {
                    showInfo("错误:" + res.ToString());
                }
                else
                {
                    showInfo("获取时间");
                    DateTime dt = new DateTime();
                    dog.GetTime(ref dt);
                    showInfo(dt.AddHours(8).ToString());
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }
    }
}