最近需要对软件进行硬件加密,综合考虑决定使用圣天诺(赛孚耐)这款加密狗,其它品牌加密狗都大差不差,也可借鉴本章。除了本身的硬件锁外,我还需要读取到加密狗的id以及内存信息。
首先,准备一个已授权并且写入数据内存的加密狗,这个烧制授权、写入内存就不写了,本章主要是解析Unity如何读取加密狗信息。
进入正片:
1.首先准备以下5个dll。
- apidsp_windows
- apidsp_windows_x64
- hasp_net_windows
- hasp_windows_27393
- hasp_windows_x64_27393
这个dll获取方法:
- C:\Users\Administrator\Documents\ Thales \Sentinel LDK 8.4\Samples\Runtime\csharp\DotNet\bin 去找,
- 9.0后的版本路径为C:\Users\Administrator\Documents\Thales\Sentinel LDK 9.0\Samples\Licensing\csharp\DotNet\bin
- 可从我分享的链接下载unitydemo中获取
(很重要)
3.打开Sentinel LDK(加密狗主锁软件),按照图片依次操作
注:这个步骤非常重要,一定要看点“执行”是否显示“操作成功完成”,如果不是,请先折腾你的加密狗,确定没问题再来看本章内容。另外将生成的代码拷贝备用,待会替换我给出的代码中相关代码块。
4.在Unity新建脚本,脚本如下:
其中HaspFeature.FromFeature()参数id 需要换成自己加密狗的功能ID,vendorCode需要换成自己主锁软件中生成的
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Aladdin.HASP;
public class Test : MonoBehaviour
{
void Start()
{
//HaspFeature.FromFeature()参数id 需要换成加密狗的功能ID
//vendorCode需要换成主锁软件中生成的
HaspFeature feature = HaspFeature.FromFeature(101);
string vendorCode =
"w9efA/qALAFfpjktW/fvmDsMKcqdSOuvDBMC+Hs0qFsL20N5xiORV/XXKRcBa5JrH6zYd68BIy+Jccam" +
"3CFxcWrCRR7obZBodVFSCnYGYyA+Yr9kEPg5Ep4/L1IOGGd8A7GxGvzqfmBMUvkvkPcbuxU2iISSM7xF" +
"XTgaCFA8koEymmom4fXag/PggvCenxWjJx9ygaCsRiFf5HRMWL9QoP72+c7LaFaoQ31S8EpoS9hgK/lt" +
"TrW4RkfWvghwLf+vMrlTD5R+ghkbwxLBipGHxWiAdqVXit6JUJEtFQWnHTmBbncwlkEZ/X4c4yYigNan" +
"P7GQ0I7oJ1HdEKyLn4Kfch9Fq0A9pL2kZncz90DByffK/8hxh5bwz5u8FCNdeWUuqsP5M7ySL45UYmsV" +
"vaIbT9DjI14";
Hasp hasp = new Hasp(feature);
HaspStatus status = hasp.Login(vendorCode);
if (HaspStatus.StatusOk != status)
{
Debug.Log("连接失败,请查找原因");
return;
}
string scope =
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
"<haspscope/>";
string format =
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
"<haspformat root=\"hasp_info\">" +
" <hasp>" +
" <attribute name=\"id\" />" +
" <attribute name=\"type\" />" +
" <feature>" +
" <attribute name=\"id\" />" +
" </feature>" +
" </hasp>" +
"</haspformat>";
string info = "";
status = Hasp.GetInfo(scope, format, vendorCode, ref info);
if (HaspStatus.StatusOk != status)
{
Debug.Log("获取信息失败,请查找原因");
return;
}
Debug.Log("加密狗信息:" + info);
//hasp.GetFile(1)参数需要主锁软件里面动态内存id进行赋值
HaspFile file = hasp.GetFile(1);
int size = 48;
byte[] data = new byte[size];
status = file.Read(data, 0, data.Length);
string str = System.Text.Encoding.UTF8.GetString(data);
if (HaspStatus.StatusOk != status)
{
Debug.Log("获取内存失败,请查找原因");
return;
}
Debug.Log("加密狗内存数据:" + str);
status = hasp.Logout();
}
// Update is called once per frame
void Update()
{
}
}
备注:该示例是一个完整示例,主要目的是查看加密狗id 以及加密狗内存数据,您跑通示例以后,如需获取其它信息,可以使用toolbox工具,生成相应代码,复制到您的工程。会话函数login和logout是必须的,login检测加密狗信息,logout释放内存,他们中间您可以再调用其他函数进行不同操作,比如读写函数,加密解密函数等 。