加密狗添加,io板卡映射修改

This commit is contained in:
hsc
2026-07-22 15:51:56 +08:00
parent be7b459385
commit ab7936c4ed
10 changed files with 490 additions and 8 deletions

View File

@@ -43,6 +43,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchMovementModule", "Benc
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CANModule", "CANModule\CANModule.csproj", "{AF2533DD-599D-49D7-942B-B0C26534D15F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "加密狗", "加密狗\加密狗.csproj", "{A35CA316-7B5B-4183-8636-4ECA532B1489}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -125,6 +127,10 @@ Global
{AF2533DD-599D-49D7-942B-B0C26534D15F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF2533DD-599D-49D7-942B-B0C26534D15F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF2533DD-599D-49D7-942B-B0C26534D15F}.Release|Any CPU.Build.0 = Release|Any CPU
{A35CA316-7B5B-4183-8636-4ECA532B1489}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A35CA316-7B5B-4183-8636-4ECA532B1489}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A35CA316-7B5B-4183-8636-4ECA532B1489}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A35CA316-7B5B-4183-8636-4ECA532B1489}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -33,6 +33,7 @@
<ProjectReference Include="..\UIShare\UIShare.csproj" />
<ProjectReference Include="..\UpdateInfoMoudle\UpdateInfoMoudle.csproj" />
<ProjectReference Include="..\ZLGUSBCANFD\ZLGUSBCANFD.csproj" />
<ProjectReference Include="..\加密狗\加密狗.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -23,6 +23,7 @@ using DeviceCommand.Base;
using AutoMapper;
using Microsoft.Extensions.Logging.Abstractions;
using ADP.Profiles;
using ;
namespace ADP
{
@@ -75,6 +76,52 @@ namespace ADP
RegionManager.SetRegionManager(Application.Current.MainWindow, re);
login.Show();
}
protected override void OnStartup(StartupEventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached)
{
base.OnStartup(e);
return;
}
string strUserSn = "ACA89B2C1194650D4E93D97BE189FFCC";
string strApiKey = "C6D01BCD442347822876F8232245DE9422643C167B8C61CE4DF6049990E7395588675A8FC6AA4BF7AD353D669C993236BEA83F77551753885A47AF34185CC3A7";
Byte[] buffer = new Byte[256];
buffer = System.Text.Encoding.Default.GetBytes(strUserSn);
uint retcode;
retcode = .SN(buffer);
if (retcode != 0)
{
MessageBox.Show(string.Format("设置UserSN失败 error code: 0x{0:x}", retcode));
return;
}
uint count = 0;
retcode = API类.VikeyFind(ref count);
if (retcode != 0 || count == 0)
{
MessageBox.Show("未找到加密狗");
return;
}
buffer = System.Text.Encoding.Default.GetBytes(strApiKey);
retcode = .APIkey(0, buffer);
if (retcode != 0)
{
MessageBox.Show(string.Format("设置api密钥失败 error code: 0x{0:x}", retcode));
return;
}
var remaining = .(0);
if (remaining <= TimeSpan.Zero)
{
MessageBox.Show("加密狗已到期或未能读取时间,请联系供应商。", "加密狗验证失败", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
base.OnStartup(e);
}
protected override void RegisterRequiredTypes(IContainerRegistry containerRegistry)
{
base.RegisterRequiredTypes(containerRegistry);

View File

@@ -53,49 +53,49 @@ namespace DeviceCommand.Devices
{ ., new IoPortInfo(1, 5) }, // Y6 -> 5
{ ., new IoPortInfo(1, 0) }, // Y1 -> 0
{ ., new IoPortInfo(1, 1) }, // Y2 -> 1
{ ., new IoPortInfo(2, 8) } // Y9 -> 8
{ ., new IoPortInfo(2, 15) } // Y9 -> 8
}},
{ 2, new Dictionary<, IoPortInfo> {
{ ., new IoPortInfo(1, 6) }, // Y7 -> 6
{ ., new IoPortInfo(1, 2) }, // Y3 -> 2
{ ., new IoPortInfo(1, 1) }, // Y2 -> 1
{ ., new IoPortInfo(2, 9) } // Y10 -> 9
{ ., new IoPortInfo(2, 14) } // Y10 -> 9
}},
{ 3, new Dictionary<, IoPortInfo> {
{ ., new IoPortInfo(1, 7) }, // Y8 -> 7
{ ., new IoPortInfo(1, 3) }, // Y4 -> 3
{ ., new IoPortInfo(1, 1) }, // Y2 -> 1
{ ., new IoPortInfo(2, 10) } // Y11 -> 10
{ ., new IoPortInfo(2, 13) } // Y11 -> 10
}},
{ 4, new Dictionary<, IoPortInfo> {
{ ., new IoPortInfo(1, 8) }, // Y9 -> 8
{ ., new IoPortInfo(1, 4) }, // Y5 -> 4
{ ., new IoPortInfo(1, 1) }, // Y2 -> 1
{ ., new IoPortInfo(2, 11) } // Y12 -> 11
{ ., new IoPortInfo(2, 12) } // Y12 -> 11
}},
{ 5, new Dictionary<, IoPortInfo> {
{ ., new IoPortInfo(2, 4) }, // Y5 -> 4
{ ., new IoPortInfo(2, 0) }, // Y1 -> 0
{ ., new IoPortInfo(1, 1) }, // Y2 -> 1
{ ., new IoPortInfo(2, 12) } // Y13 -> 12
{ ., new IoPortInfo(2, 11) } // Y13 -> 12
}},
{ 6, new Dictionary<, IoPortInfo> {
{ ., new IoPortInfo(2, 5) }, // Y6 -> 5
{ ., new IoPortInfo(2, 1) }, // Y2 -> 1
{ ., new IoPortInfo(1, 1) }, // Y2 -> 1
{ ., new IoPortInfo(2, 13) } // Y14 -> 13
{ ., new IoPortInfo(2, 10) } // Y14 -> 13
}},
{ 7, new Dictionary<, IoPortInfo> {
{ ., new IoPortInfo(2, 6) }, // Y7 -> 6
{ ., new IoPortInfo(2, 2) }, // Y3 -> 2
{ ., new IoPortInfo(1, 1) }, // Y2 -> 1
{ ., new IoPortInfo(2, 14) } // Y15 -> 14
{ ., new IoPortInfo(2, 9) } // Y15 -> 14
}},
{ 8, new Dictionary<, IoPortInfo> {
{ ., new IoPortInfo(2, 7) }, // Y8 -> 7
{ ., new IoPortInfo(2, 3) }, // Y4 -> 3
{ ., new IoPortInfo(1, 1) }, // Y2 -> 1
{ ., new IoPortInfo(2, 15) } // Y16 -> 15
{ ., new IoPortInfo(2, 8) } // Y16 -> 15
}}
};

BIN
加密狗/ViKey.dll Normal file

Binary file not shown.

BIN
加密狗/ViKey7.4.4.dll Normal file

Binary file not shown.

View File

@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<!-- 配置 ViKey.dll 复制到输出目录 -->
<None Include="ViKey.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,252 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace
{
public enum VikeyType
{
ViKeyAPP = 0, //实用型加密狗
ViKeySTD = 1, //标准型加密狗
ViKeyNET = 2, //网络型加密狗
ViKeyPRO = 3, //专业型加密狗
ViKeyWEB = 4, //身份认证型加密狗
ViKeyTIME = 5, //时钟型加密狗,内部独立时钟
ViKeyMultiFunctional = 0x0A, //多功能加密狗 支持软件加密 支持文档加密
ViKeyMultiFunctionalTime = 0x0B, //多功能时钟加密狗
ViKeyInvalid //无效类型
};
public enum : uint
{
/// <summary>
/// 成功
/// </summary>
VIKEY_SUCCESS = 0x00000000,
/// <summary>
/// 没有找到ViKey加密锁
/// </summary>
VIKEY_ERROR_NO_VIKEY = 0x80000001,
/// <summary>
/// 密码错误
/// </summary>
VIKEY_ERROR_INVALID_PASSWORD = 0x80000002,
/// <summary>
/// 请先查找加密锁
/// </summary>
VIKEY_ERROR_NEED_FIND = 0x80000003,
/// <summary>
/// 无效的句柄
/// </summary>
VIKEY_ERROR_INVALID_INDEX = 0x80000004,
/// <summary>
/// 数值错误
/// </summary>
VIKEY_ERROR_INVALID_VALUE = 0x80000005,
/// <summary>
/// 秘钥无效
/// </summary>
VIKEY_ERROR_INVALID_KEY = 0x80000006,
/// <summary>
/// 读取信息错误
/// </summary>
VIKEY_ERROR_GET_VALUE = 0x80000007,
/// <summary>
/// 设置信息错误
/// </summary>
VIKEY_ERROR_SET_VALUE = 0x80000008,
/// <summary>
/// 没有机会
/// </summary>
VIKEY_ERROR_NO_CHANCE = 0x80000009,
/// <summary>
/// 权限不足
/// </summary>
VIKEY_ERROR_NO_TAUTHORITY = 0x8000000A,
/// <summary>
/// 地址或长度错误
/// </summary>
VIKEY_ERROR_INVALID_ADDR_OR_SIZE = 0x8000000B,
/// <summary>
/// 获取随机数错误
/// </summary>
VIKEY_ERROR_RANDOM = 0x8000000C,
/// <summary>
/// 获取种子错误
/// </summary>
VIKEY_ERROR_SEED = 0x8000000D,
/// <summary>
/// 通信错误
/// </summary>
VIKEY_ERROR_CONNECTION = 0x8000000E,
/// <summary>
/// 算法或计算错误
/// </summary>
VIKEY_ERROR_CALCULATE = 0x8000000F,
/// <summary>
/// 计数器错误
/// </summary>
VIKEY_ERROR_MODULE = 0x80000010,
/// <summary>
/// 产生密码错误
/// </summary>
VIKEY_ERROR_GENERATE_NEW_PASSWORD = 0x80000011,
/// <summary>
/// 加密数据错误
/// </summary>
VIKEY_ERROR_ENCRYPT_FAILED = 0x80000012,
/// <summary>
/// 解密数据错误
/// </summary>
VIKEY_ERROR_DECRYPT_FAILED = 0x80000013,
/// <summary>
/// ViKey加密锁已经被锁定
/// </summary>
VIKEY_ERROR_ALREADY_LOCKED = 0x80000014,
/// <summary>
/// 无效的命令
/// </summary>
VIKEY_ERROR_UNKNOWN_COMMAND = 0x80000015,
/// <summary>
/// 当前ViKey加密锁不支持此功能
/// </summary>
VIKEY_ERROR_NO_SUPPORT = 0x80000016,
/// <summary>
/// 发生异常
/// </summary>
VIKEY_ERROR_CATCH = 0x80000017,
/// <summary>
/// 未知错误
/// </summary>
VIKEY_ERROR_UNKNOWN_ERROR = 0xFFFFFFFF,
}
public class API类
{
/// <summary>
/// 查找加密锁。使用其他API前必须先调用次函数。
/// </summary>
/// <param name="pdwCount">[out] 如果查找到系统中存在加密狗,返回查找到加密狗的个数</param>
/// <returns>0表示系统中存在ViKey加密狗。</returns>
[DllImport("ViKey.dll")]
public static extern uint VikeyFind(ref uint pdwCount);
/// <summary>
/// 获取加密狗的硬件ID,加密狗的硬件ID是加密狗的唯一标识每个加密狗的硬件ID都不一样。
/// </summary>
/// <param name="Index">[in]指定要操作加密狗的序号</param>
/// <param name="pdwHID">[out]返回加密狗的硬件ID</param>
/// <returns></returns>
[DllImport("ViKey.dll")]
public static extern uint VikeyGetHID(ushort Index, out uint pdwHID);
/// <summary>
/// 获取加密狗的当前权限
/// </summary>
/// <param name="Index">指定要操作加密狗的序号</param>
/// <param name="pLevel">返回加密狗的当前权限 0表示加密狗尚未登录 1表示加密狗是用户权限 2表示加密狗是管理员权限</param>
/// <returns></returns>
[DllImport("ViKey.dll", CallingConvention = CallingConvention.StdCall)]
public static extern uint VikeyGetLevel(ushort Index, out byte pLevel);
/// <summary>
/// 以用户权限登录加密狗
/// </summary>
/// <param name="Index"></param>
/// <param name="pUserPassushort"></param>
/// <returns></returns>
[DllImport("ViKey.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern uint VikeyUserLogin(ushort Index, Byte[] pUserPassword);
/// <summary>
/// 以管理员权限登录加密狗
/// </summary>
/// <param name="Index"></param>
/// <param name="pAdminPassword"></param>
/// <returns></returns>
[DllImport("ViKey.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern uint VikeyAdminLogin(ushort Index, Byte[] pAdminPassword);
/// <summary>
/// 注销登录加密狗 注销后的加密狗权限为0
/// </summary>
/// <param name="Index">[in]指定要操作加密狗的序号</param>
/// <returns></returns>
[DllImport("ViKey.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern uint VikeyLogoff(ushort Index);
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct SVikeyTime
{
public byte cYear;
public byte cMonth;
public byte cDay;
public byte cHour;
public byte cMinute;
public byte cSecond;
}
/// <summary>
/// 获取时钟型加密狗中的内部独立时间
/// </summary>
/// <param name="Index"></param>
/// <param name="pTime"></param>
/// <returns></returns>
[DllImport("ViKey.dll", CallingConvention = CallingConvention.StdCall)]
public static extern uint VikeyGetTime(ushort Index, out SVikeyTime pTime);
/// <summary>
/// 获取时钟型加密狗中的到期时间
/// </summary>
/// <param name="Index"></param>
/// <param name="pTime"></param>
/// <returns></returns>
[DllImport("ViKey.dll", CallingConvention = CallingConvention.StdCall)]
public static extern uint VikeyGetValidTime(ushort Index, out SVikeyTime pTime);
/// <summary>
/// 检测时钟型加密狗的时钟功能是否到期
/// </summary>
/// <param name="Index">[in]指定要操作加密狗的序号</param>
/// <param name="pIsValid">[out]返回是否到期结果 1表示没有到期 0表示已经到期</param>
/// <returns></returns>
[DllImport("ViKey.dll", CallingConvention = CallingConvention.StdCall)]
public static extern uint VikeyCheckValidTime(ushort Index, out byte pIsValid);
/// <summary>
/// 从加密狗中的获取4个双字的随机数数据
/// </summary>
/// <param name="Index">指定要操作加密狗的序号</param>
/// <param name="pwRandom1"></param>
/// <param name="pwRandom2"></param>
/// <param name="pwRandom3"></param>
/// <param name="pwRandom4"></param>
/// <returns></returns>
[DllImport("ViKey.dll", CallingConvention = CallingConvention.StdCall)]
public static extern uint ViKeyRandom(ushort Index,
out ushort pwRandom1, out ushort pwRandom2, out ushort pwRandom3, out ushort pwRandom4);
[DllImport("ViKey.dll", CallingConvention = CallingConvention.StdCall)]
public static extern uint VikeyReadData(ushort Index, ushort Addr, ushort Length, byte[] buffer);
[DllImport("ViKey.dll", CallingConvention = CallingConvention.StdCall)]
public static extern uint VikeyWriteData(ushort Index, ushort Addr, ushort Length, byte[] buffer);
/// <summary>
/// // 7.4.6版本(含)以后的加密狗必须在查找加密狗前调用VikeySetUserSN并输入正确的UserSN后面才能查找到所属用户的加密狗
// 7.4.6版本以前的加密狗可以忽略此函数 不用调用此函数
// UserSN在ViKey加密管理工具的->帮助中心页面->复制出来
/// </summary>
/// <param name="pUserSN"></param>
/// <returns></returns>
[DllImport("ViKey")]
public static extern uint VikeySetUserSN(Byte[] pUserSN);
/// <summary>
/// //设置加密狗ApiKey 硬件版本为7.4.5(含)以后的加密狗必须要调用此接口才能登录
//ApiKey 是由ViKey加密管理工具的->加密狗管理->密钥设置页面 通过Api密钥种子生成的
/// </summary>
/// <param name="Index"></param>
/// <param name="pApiKey"></param>
/// <returns></returns>
[DllImport("ViKey")]
public static extern uint VikeySetApiKey(ushort Index, Byte[] pApiKey);
[DllImport("ViKey")]
public static extern uint VikeyUninitialization();
}
}

View File

@@ -0,0 +1,145 @@
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);
}
}
}

View File

@@ -0,0 +1,15 @@
using System;
using ;
// ═══════════════════════════════════════════════════════
// 刷入脚本(独立运行,不属于 ADP 主程序流程)
// 使用前请先用 ViKey 管理工具设置好密码和到期时间
// ═══════════════════════════════════════════════════════
public class Program
{
public static void Main(string[] args)
{
//写入加密狗(需管理员密码)
.(0, "00000000", "ADP项目");
}
}