Files
ADP/加密狗/加密狗驱动类.cs

145 lines
5.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace
{
public class
{
public static void Uninitialization()
{
API类.VikeyUninitialization();
}
// 保留原有方法,但添加新方法
public static int (ushort Index, string , string )
{
// 保持不变
uint = 0;
var re = API类.VikeyFind(ref );
if (re != 0) return -1;
API类.VikeyAdminLogin(Index, Encoding.Default.GetBytes());
SHA256 sHA256 = SHA256.Create();
API类.VikeyGetHID(Index, out uint pid);
byte[] HID = BitConverter.GetBytes(pid);
var pswd = Encoding.UTF8.GetBytes();
byte[] = HID.Concat(pswd).ToArray();
var = sHA256.ComputeHash();
API类.VikeyWriteData(Index, 0, 32, );
byte[] randomBytes = new byte[96];
Random random = new Random();
random.NextBytes(randomBytes);
API类.VikeyWriteData(Index, 32, 96, randomBytes);
return 0;
}
/// <summary>
/// 新方法:仅检查加密狗时间,不验证密码和数据
/// </summary>
public static int ()
{
uint = 0;
var re = API类.VikeyFind(ref );
if (re != 0 || == 0) return -1;
// 直接返回第一个找到的加密狗
return 0;
}
/// <summary>
/// 原有方法:寻找加密狗并进行完整验证
/// </summary>
public static int (string ,string )
{
uint = 0;
var re = API类.VikeyFind(ref );
if (re != 0) return -1;
int = -1;
for (ushort i = 0; i < ; i++)
{
re = API类.VikeyUserLogin(i, Encoding.Default.GetBytes());
if ((i, )) return i;
}
return (int);
}
public static bool (ushort Index, string )
{
// 保持不变
SHA256 sHA256 = SHA256.Create();
API类.VikeyGetHID(Index, out uint pid);
byte[] HID = BitConverter.GetBytes(pid);
var pswd = Encoding.UTF8.GetBytes();
byte[] = HID.Concat(pswd).ToArray();
var = sHA256.ComputeHash();
var buffer = new byte[32];
API类.VikeyReadData(Index, 0, 32, buffer);
return buffer.SequenceEqual();
}
public static TimeSpan (int )
{
ushort = (ushort);
try
{
// 读时间前必须先登录(用户权限即可)
uint re = API类.VikeyUserLogin(, Encoding.Default.GetBytes("11111111"));
if (re != 0)
{
Console.WriteLine($"VikeyUserLogin 失败: 0x{re:X}");
return TimeSpan.Zero;
}
re = API类.VikeyGetTime(, out API类.SVikeyTime );
if (re != 0)
{
Console.WriteLine($"VikeyGetTime 失败: 0x{re:X}");
return TimeSpan.Zero;
}
re = API类.VikeyGetValidTime(, out API类.SVikeyTime );
if (re != 0)
{
Console.WriteLine($"VikeyGetValidTime 失败: 0x{re:X}");
return TimeSpan.Zero;
}
re = API类.VikeyCheckValidTime(, out byte isValid);
Console.WriteLine($"VikeyCheckValidTime: ret=0x{re:X}, isValid={isValid}");
// cYear 是 byteViKey 用距 2000 年的偏移量
DateTime _现在时间 = new DateTime(.cYear + 2000, .cMonth, .cDay,
.cHour, .cMinute, .cSecond);
DateTime _到期时间 = new DateTime(.cYear + 2000, .cMonth, .cDay,
.cHour, .cMinute, .cSecond);
Console.WriteLine($"加密狗当前时间: {_现在时间:yyyy-MM-dd HH:mm:ss}");
Console.WriteLine($"加密狗到期时间: {_到期时间:yyyy-MM-dd HH:mm:ss}");
return _到期时间 - _现在时间;
}
catch (Exception ex)
{
Console.WriteLine($"检查剩余时间异常: {ex.Message}");
return TimeSpan.Zero;
}
}
public static uint SN(Byte[] buffer)
{
return API类.VikeySetUserSN(buffer);
}
public static uint APIkey(ushort index, Byte[] buffer)
{
return API类.VikeySetApiKey(index, buffer);
}
}
}