Sentinel LDK加密狗操作类总结(C++版和Java版)
文章分为以下几个部分:
1.前言
2.库文件
3.示例文件
4.开发商代码
5.Java版代码示例
6.C++版代码示例
前言
本文介绍的加密狗操作类只适用于Sentinel LDK加密狗。使用之前需要先安装Sentinel LDK Vendor Suite。并且需要有一个主狗,导入API,具体导入流程这里就不介绍了,可以根据软件界面上的相关选项操作。
本文主要用到安装目录下的3个文件夹,如下图。其中API里面放的是库文件。Samples里面放的是demo,VendorCodes里面放的是开发商代码文件。
其中API文件夹和Samples文件夹中都有一个Runtime,这是我们主要用到的文件夹。对于加密狗的查找、读取、写入这些基本操作的库文件和demo都在对应的Runtime文件夹里。
库文件
C++的库文件在此目录下,我这里没有主狗所以dll和lib都不是正式版的,只是让程序能够运行的demo版,通过主狗导入API之后,在这个目录下就能找到正式版的。Demo版用:hasp_windows_demo.dll和hasp_windows_demo.lib正式版不带demo字样。
Java的用上图这个jar文件。
示例文件
示例文件在这个目录下,选择自己使用的语言类型。
C++可以看这个:
Java可以看这个:
不过demo里面代码比较多,可能阅读起来有点麻烦。
开发商的代码
在如上图的目录下,我这里只有demo版的开发商代码,正式版的需要用主狗导入API才能获取到,右键用记事本打开就能看到具体的开发商代码,是一长串字符。这个东西在程序中需要用到,是必不可少的东西。
Java版代码
创建项目之后先把jar包引进去
import java.nio.*;
import java.io.*;
import Aladdin.Hasp;
import Aladdin.HaspTime;
import Aladdin.HaspStatus;
import Aladdin.HaspApiVersion;
public class DogUtil {
//开发商代码,通过软件用主锁导入API之后在Sentinel LDK 7.10\VendorCodes目录下可以找到
public static final String vendorCode = new String(
"AzIceaqfA1hX5wS+M8cGnYh5ceevUnOZIzJBbXFD6dgf3tBkb9cvUF/Tkd/iKu2fsg9wAysYKw7RMA" +
"sVvIp4KcXle/v1RaXrLVnNBJ2H2DmrbUMOZbQUFXe698qmJsqNpLXRA367xpZ54i8kC5DTXwDhfxWT" +
"OZrBrh5sRKHcoVLumztIQjgWh37AzmSd1bLOfUGI0xjAL9zJWO3fRaeB0NS2KlmoKaVT5Y04zZEc06" +
"waU2r6AU2Dc4uipJqJmObqKM+tfNKAS0rZr5IudRiC7pUwnmtaHRe5fgSI8M7yvypvm+13Wm4Gwd4V" +
"nYiZvSxf8ImN3ZOG9wEzfyMIlH2+rKPUVHI+igsqla0Wd9m7ZUR9vFotj1uYV0OzG7hX0+huN2E/Id" +
"gLDjbiapj1e2fKHrMmGFaIvI6xzzJIQJF9GiRZ7+0jNFLKSyzX/K3JAyFrIPObfwM+y+zAgE1sWcZ1" +
"YnuBhICyRHBhaJDKIZL8MywrEfB2yF+R3k9wFG1oN48gSLyfrfEKuB/qgNp+BeTruWUk0AwRE9XVMU" +
"uRbjpxa4YA67SKunFEgFGgUfHBeHJTivvUl0u4Dki1UKAT973P+nXy2O0u239If/kRpNUVhMg8kpk7" +
"s8i6Arp7l/705/bLCx4kN5hHHSXIqkiG9tHdeNV8VYo5+72hgaCx3/uVoVLmtvxbOIvo120uTJbuLV" +
"TvT8KtsOlb3DxwUrwLzaEMoAQAFk6Q9bNipHxfkRQER4kR7IYTMzSoW5mxh3H9O8Ge5BqVeYMEW36q" +
"9wnOYfxOLNw6yQMf8f9sJN4KhZty02xm707S7VEfJJ1KNq7b5pP/3RjE0IKtB2gE6vAPRvRLzEohu0" +
"m7q1aUp8wAvSiqjZy7FLaTtLEApXYvLvz6PEJdj4TegCZugj7c8bIOEqLXmloZ6EgVnjQ7/ttys7VF" +
"ITB3mazzFiyQuKf4J6+b/a/Y");
public static final int DEMO_MEMBUFFER_SIZE = 128;
//登录加密狗(一般用此函数查找加密狗)
public int login()
{
int status;
Hasp hasp = new Hasp(Hasp.HASP_DEFAULT_FID);
hasp.login(vendorCode);
//获取返回值,HASP_STATUS_OK代表成功找到加密狗,值为0
status = hasp.getLastError();
switch (status) {
case HaspStatus.HASP_STATUS_OK:
System.out.println("OK");
break;
case HaspStatus.HASP_FEATURE_NOT_FOUND:
System.out.println("no Sentinel DEMOMA key found");
break;
case HaspStatus.HASP_HASP_NOT_FOUND:
System.out.println("Sentinel key not found");
break;
case HaspStatus.HASP_OLD_DRIVER:
System.out.println("outdated driver version or no driver installed");
break;
case HaspStatus.HASP_NO_DRIVER:
System.out.println("Sentinel key not found");
break;
case HaspStatus.HASP_INV_VCODE:
System.out.println("invalid vendor code");
break;
default:
System.out.println("login to default feature failed");
}
return status;
}
//写入
public int write(String str)
{
int status;
int fsize = 0;
int i;
Hasp hasp = new Hasp(Hasp.HASP_DEFAULT_FID);
//先获取空间大小
fsize = getSize();
//String转换成byte[]
byte[] membuffer = str.getBytes();
// System.out.println("\nincrementing every byte in memory buffer");
// for (i = 0; i < fsize; i++) membuffer[i]++;
System.out.println("\nwriting " + fsize + " bytes to memory : ");
hasp.write(Hasp.HASP_FILEID_RW, 0, membuffer);
status = hasp.getLastError();
switch (status) {
case HaspStatus.HASP_STATUS_OK:
System.out.println("OK");
break;
case HaspStatus.HASP_INV_HND:
System.out.println("handle not active");
break;
case HaspStatus.HASP_INV_FILEID:
System.out.println("invalid file id");
break;
case HaspStatus.HASP_MEM_RANGE:
System.out.println("beyond memory range of attached Sentinel key");
break;
case HaspStatus.HASP_HASP_NOT_FOUND:
System.out.println("Sentinel key not found");
break;
default:
System.out.println("write memory failed");
}
return status;
}
//读取
public String read()
{
int status = -1;
int fsize = 0;
Hasp hasp = new Hasp(Hasp.HASP_DEFAULT_FID);
//准备一个byte数组用来存读取到的数据
byte[] membuffer = new byte[DEMO_MEMBUFFER_SIZE];
fsize = getSize();
if (fsize != 0) {
if (fsize > DEMO_MEMBUFFER_SIZE) fsize = DEMO_MEMBUFFER_SIZE;
System.out.println("\nreading " + fsize + " bytes from memory : ");
hasp.read(Hasp.HASP_FILEID_RW, 0, membuffer);
status = hasp.getLastError();
switch (status) {
case HaspStatus.HASP_STATUS_OK:
System.out.println("OK");
dump(membuffer, " ");
break;
case HaspStatus.HASP_INV_HND:
System.out.println("handle not active");
break;
case HaspStatus.HASP_INV_FILEID:
System.out.println("invalid file id");
break;
case HaspStatus.HASP_MEM_RANGE:
System.out.println("beyond memory range of attached Sentinel key");
break;
case HaspStatus.HASP_HASP_NOT_FOUND:
System.out.println("hasp not found");
break;
default:
System.out.println("read memory failed\n");
}
}
String content = byteToString(membuffer);
//String content = new String(membuffer);
return content;
}
//byte[]转String
private String byteToString(byte[] bytes) {
if (null == bytes || bytes.length == 0) {
return "";
}
String strContent = "";
try {
strContent = new String(bytes, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return strContent;
}
//获取空间大小
public int getSize()
{
int status;
int fsize = 0;
if(login()==0)
return 0;
Hasp hasp = new Hasp(Hasp.HASP_DEFAULT_FID);
System.out.println("\nretrieving the key's memory size : ");
fsize = hasp.getSize(Hasp.HASP_FILEID_RW);
status = hasp.getLastError();
switch (status) {
case HaspStatus.HASP_STATUS_OK:
System.out.println("memory size is " + fsize + " bytes");
break;
case HaspStatus.HASP_INV_HND:
System.out.println("handle not active");
break;
case HaspStatus.HASP_INV_FILEID:
System.out.println("invalid file id");
break;
case HaspStatus.HASP_HASP_NOT_FOUND:
System.out.println("Sentinel key not found");
break;
default:
System.out.println("could not retrieve memory size");
}
return fsize;
}
/************************************************************************
*
* helper function: dumps a given block of data, in hex and ascii
*/
/*
* Converts a byte to hex digit and writes to the supplied buffer
*/
private static void byte2hex(byte b, StringBuffer buf)
{
char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
int high = ((b & 0xf0) >> 4);
int low = (b & 0x0f);
buf.append(hexChars[high]);
buf.append(hexChars[low]);
}
/*
* Converts a byte array to hex string
*/
private static String toHexString(byte[] block)
{
StringBuffer buf = new StringBuffer();
int len = block.length;
for (int i = 0; i < len; i++) {
byte2hex(block[i], buf);
if (i < len - 1) {
buf.append(":");
}
}
return buf.toString();
}
public static void dump(byte[] data, String margin)
{
int i, j;
byte b;
byte[] s = new byte[16];
byte hex[] = {0};
String shex;
String PrtString;
if (data.length == 0) return;
s[0] = 0;
j = 0;
for (i = 0; i < data.length; i++) {
if (j == 0) System.out.print(margin);
b = data[i];
if ((b < 32) || (b > 127)) s[j] = '.'; else s[j] = b;
if (j < 15)
s[j+1] = 0;
hex[0] = b; shex = toHexString(hex);
System.out.print(shex + " ");
j++;
if (((j & 3) == 0) && (j < 15)) System.out.print("| ");
PrtString = new String(s);
if (j > 15) {
System.out.println("[" + PrtString + "]");
j = 0;
s[0] = 0;
}
}
if (j != 0) {
while (j < 16) {
System.out.print(" ");
j++;
if (((j & 3) == 0) && (j < 15)) System.out.print("| ");
}
PrtString = new String(s);
System.out.println(" [" + PrtString + "]");
}
}
}
C++版代码
创建项目之后先引用dll和lib
#ifndef DOGUTIL_H
#define DOGUTIL_H
#include "hasp_api.h"
#include <QString>
#include <QDebug>
#include <QMessageBox>
#include <QDateTime>
class DogUtil
{
public:
DogUtil();
int login();
void write(QString str);
QString read();
void get_sessioninfo();
private:
hasp_handle_t handle;
hasp_status_t status;
hasp_size_t fsize;
QString vendor_code;
unsigned char membuffer[128];
void getSize();
};
#endif // DOGUTIL_H
#include "dogutil.h"
DogUtil::DogUtil()
{
}
//登录U盾
int DogUtil::login()
{
//开发商代码(每个主狗对应一个开发商代码)
vendor_code =
"zaaRcv4f85+8c8u6MRfXOV2n8xT7iTfQ6+I8+X/PdwpSUtLai4Vcw04RDgoGlxNAVqRHBRtXVT411MtT"
"lKgTJojamcEssE9ITyOXANrYD0A/qWfHEUKKsDwwzzzAXcT3hZhS8K/M7XpZ6tSVUw+otCXhFP5izls"
"zHqr8nXDvidq/oJRNdRUOp63Al3RegoDWvNwHQApF4IWZxE/YURaStkjecrjF8WQ5YRr8M3nngawosGo"
"dYXKx1/0wdSwSGqHCvLkiNuHZGaP6m/sM6pZDL1TebUO2sssbdQwDlX8pAih7SupIRa1fv4Og8/M+gj"
"XKc+aLPTaTIJvhBlXRPAoYiq4+h2RHklgeQRQBx+RlTZmb66VXrPs5KEJDgEEqiNp19r3gyHZ6Mh/hFR"
"6jDadQ3NTFblfEtKYU27elR8a03PB2Yb2YaQ/YPZmXbpyNkn39ov2c7pXqEe/Thz2CuYdQJD+XIH0bFn"
"fpUqgsWhlASfXcTiRZ8cgPcEY132NQOc1VszP5IXHy0x8dd2j7/mI0gOIKiAxWReSxbEgYW/MntndWkQ"
"+F3oz1d5gidESh78hAJUQX8LioODnbr46GsTEfyTAbbk4MaBNEHccqbLCdp2iu8Hc1s4VlBXbJHD0U6D"
"2DtSIGMcENphxkrUPT++IXWTGChezUR2y+5/8m688Y12ivgKr5J67VwootMtjaPlo6StC/rUWu0eikCJ"
"63SKf8sdCiTSDcPZkbCkxv1L3v9t+mxOchcBhc2qas4EF3t5nPRNxLirWRG7rpz2cqUoAh1Dd89IhQsu"
"OH4lOwdaq5vcDL/Dl+exiZKCGvcHTczH6/cAyLS9kCxd1ZH5ZWMwic7p8gyPKqVBr7XsSsEeBwnE2Ywh"
"VuiQl6jk2qm0CoY6rW1wqw7soqaNVXKm4V7gO62eGSvNmOa+UsTUY7LItwpYPAXwWQ8UXQTuniF2l/sV"
"dCCZ13VU0m95bwMLwqY5ag==";
//****是加密狗特征id
status = hasp_login(****, (hasp_vendor_code_t)vendor_code.toLatin1().data(), &handle);
/* check if operation was successful */
switch (status)
{
case HASP_STATUS_OK:
qDebug()<<("OK\n");
break;
case HASP_FEATURE_NOT_FOUND:
qDebug()<<("login to default feature failed\n");
break;
case HASP_CONTAINER_NOT_FOUND:
qDebug()<<("no sentinel key with vendor code DEMOMA found\n");
break;
case HASP_OLD_DRIVER:
qDebug()<<("outdated driver version installed\n");
break;
case HASP_NO_DRIVER:
qDebug()<<("sentinel driver not installed\n");
break;
case HASP_INV_VCODE:
qDebug()<<("invalid vendor code\n");
break;
default:
qDebug()<<("login to default feature failed with status %d\n", status);
}
if(status)
{
QString errStr = "未找到U盾!errorcode:"+QString::number(status);
QMessageBox box(QMessageBox::Warning,"错误",errStr);
box.exec();
exit(-1);
}
return status;
}
//获取U盾中存储的字符串
QString DogUtil::read()
{
login();
status = hasp_read(handle,
HASP_FILEID_RW, /* read/write file ID */
0, /* offset */
fsize, /* length */
&membuffer[0]); /* file data */
switch (status)
{
case HASP_STATUS_OK:
qDebug()<<("OK\n");
qDebug()<<QString(QLatin1String((const char*)membuffer)) ;
break;
case HASP_INV_HND:
qDebug()<<("handle not active\n");
break;
case HASP_INV_FILEID:
qDebug()<<("invalid file id\n");
break;
case HASP_MEM_RANGE:
qDebug()<<("beyond memory range of attached sentinel key\n");
break;
case HASP_CONTAINER_NOT_FOUND:
qDebug()<<("key/license container not available\n");
break;
default:
qDebug()<<("read memory failed\n");
}
if (status) {
hasp_logout(handle);
//exit(-1);
}
QString str = QString(QLatin1String((const char*)membuffer));
return str;
}
//向U盾写入数据
void DogUtil::write(QString str)
{
login();
QByteArray ba;
ba = str.toLatin1();
memcpy(membuffer,ba.data(),ba.size()+1);
status = hasp_write(handle,
HASP_FILEID_RW,
0, /* offset */
str.length(), /* length */
&membuffer);
switch (status)
{
case HASP_STATUS_OK:
qDebug()<<("OK\n");
break;
case HASP_INV_HND:
qDebug()<<("handle not active\n");
break;
case HASP_INV_FILEID:
qDebug()<<("invalid file id\n");
break;
case HASP_MEM_RANGE:
qDebug()<<("beyond memory range of attached sentinel key\n");
break;
case HASP_CONTAINER_NOT_FOUND:
qDebug()<<("key/license container not available\n");
break;
default:
qDebug()<<("write memory failed\n");
}
if (status) {
hasp_logout(handle);
//exit(-1);
}
}
void DogUtil::get_sessioninfo()
{
}
//获取字节大小
void DogUtil::getSize()
{
status = hasp_get_size(handle,
HASP_FILEID_RW,
&fsize);
qDebug()<<(unsigned int)fsize;
switch (status)
{
case HASP_STATUS_OK:
//printf("sentinel memory size is %d bytes\n", fsize);
break;
case HASP_INV_HND:
//printf("handle not active\n");
break;
case HASP_INV_FILEID:
//printf("invalid file id\n");
break;
case HASP_CONTAINER_NOT_FOUND:
//printf("key/license container not available\n");
break;
//default:
//printf("could not retrieve memory size\n");
}
if (status) {
hasp_logout(handle);
//exit(-1);
}
}
{{ cmt.username }}
{{ cmt.content }}
{{ cmt.commentDate | formatDate('YYYY.MM.DD hh:mm') }}