265 lines
9.7 KiB
C#
265 lines
9.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Security;
|
|
using SuperMap.RealEstate.SecurityDevice.UsbKey; //SecurityDevice.Core
|
|
|
|
namespace SuperMap.RealEstate.SecurityDevice
|
|
{
|
|
|
|
public class UsbKeyLicenseDevice : ClientSecurityDevice
|
|
{
|
|
UsbKey2000 _UsbKey2000 = new UsbKey2000();
|
|
string _KeyPath = string.Empty;
|
|
string _Message = string.Empty;
|
|
|
|
const string PassWord_Default = "FFFFFFFF";
|
|
const string PassWord_Read = "QPWOEIRU";
|
|
const string PassWord_Write = "UTMSJDEF";
|
|
|
|
#region Open
|
|
public override Boolean Open(int index = 0)
|
|
{
|
|
//Message = "正在查找数字证书...";
|
|
//判断系统中是否存在着加密锁。
|
|
if (_UsbKey2000.FindPort(index, ref _KeyPath) != 0)
|
|
{
|
|
Message = "请插入数字证书。";
|
|
return false;
|
|
}
|
|
Message = "成功找到数字证书。";
|
|
return true;
|
|
}
|
|
#endregion
|
|
|
|
public override bool Close(int index = 0)
|
|
{
|
|
//为带容量的版本预留
|
|
return base.Close(index);
|
|
}
|
|
|
|
#region Version
|
|
public override string Version
|
|
{
|
|
get
|
|
{
|
|
short _Version = 0;
|
|
if (_UsbKey2000.NT_GetIDVersion(ref _Version, _KeyPath) != 0)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
return _Version.ToString();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Reset
|
|
public override Boolean Reset(int length = 500)
|
|
{
|
|
Message = "正在初始化数字证书...";
|
|
if (!ResetPassWord())
|
|
return false;
|
|
|
|
if (!ResetDevice(length))
|
|
return false;
|
|
Message = "成功初始化数字证书。";
|
|
return true;
|
|
}
|
|
#endregion
|
|
|
|
#region ReadDeviceLength
|
|
private int ReadDeviceLength(int start, int length)
|
|
{
|
|
string _value = ReadDevice(start, length);
|
|
int _TempLength = 0;
|
|
if (!int.TryParse(_value, out _TempLength))
|
|
_TempLength = 0;
|
|
return _TempLength;
|
|
}
|
|
#endregion
|
|
|
|
#region ReadDevice
|
|
private string ReadDevice(int start, int length)
|
|
{
|
|
string _value = string.Empty;
|
|
if (_UsbKey2000.YReadString(ref _value, start, length, PassWord_Read, PassWord_Read, _KeyPath) != 0)
|
|
return string.Empty;
|
|
_value = _value.Trim();
|
|
return _value;
|
|
}
|
|
#endregion
|
|
|
|
#region WriteDevice
|
|
private bool WriteDevice(string value, int start, ref int lastIndex)
|
|
{
|
|
int _Length = UsbKey2000.lstrlenA(value);
|
|
//写入位置
|
|
if (_UsbKey2000.YWriteString(value, start, PassWord_Write, PassWord_Write, _KeyPath) != 0)
|
|
{
|
|
Message = "写入数字证书失败。";
|
|
return false;
|
|
}
|
|
lastIndex = start + _Length;
|
|
return true;
|
|
}
|
|
#endregion
|
|
|
|
#region ResetInfo
|
|
private bool ResetInfo(int start, int length, ref int lastIndex)
|
|
{
|
|
int _Length = ReadDeviceLength(start, length);
|
|
string _Str = "0";
|
|
_Str = _Str.PadRight(length, ' ');
|
|
WriteDevice(_Str, start, ref _Length);
|
|
_Str = "";
|
|
_Str = _Str.PadRight(_Length, ' ');
|
|
return WriteDevice(_Str, lastIndex, ref lastIndex);
|
|
}
|
|
#endregion
|
|
|
|
#region ResetDevice
|
|
private bool ResetDevice(int length )
|
|
{
|
|
//Message = "正在重置数据...";
|
|
int _Length = ClientSecurityDeviceObject.StartIndex;
|
|
if (!ResetInfo(ClientSecurityDeviceObject.StartIndex_Name, ClientSecurityDeviceObject.Length_Name, ref _Length) ||
|
|
!ResetInfo(ClientSecurityDeviceObject.StartIndex_User, ClientSecurityDeviceObject.Length_User, ref _Length) ||
|
|
!ResetInfo(ClientSecurityDeviceObject.StartIndex_Key, ClientSecurityDeviceObject.Length_Key, ref _Length) ||
|
|
!ResetInfo(ClientSecurityDeviceObject.StartIndex_Date, ClientSecurityDeviceObject.Length_Date, ref _Length) ||
|
|
!ResetInfo(ClientSecurityDeviceObject.StartIndex_Url, ClientSecurityDeviceObject.Length_Url, ref _Length))
|
|
{
|
|
Message = "重置数字证书「数据区」失败。";
|
|
return false;
|
|
}
|
|
|
|
#region 【冗余代码】用于清除前length位置为空白。
|
|
string _Str = "";
|
|
_Str = _Str.PadRight(length, ' ');
|
|
if (WriteDevice(_Str, ClientSecurityDeviceObject.StartIndex, ref _Length) == false)
|
|
{
|
|
Message = "重置数字证书「数据区」失败。";
|
|
return false;
|
|
}
|
|
#endregion
|
|
|
|
//Message = "成功重置数据。";
|
|
return true;
|
|
}
|
|
#endregion
|
|
|
|
#region ResetPassWord
|
|
private bool ResetPassWord()
|
|
{
|
|
//重置写入密码
|
|
//Message = "正在重置密码...";
|
|
if ((_UsbKey2000.SetWritePassword(PassWord_Write, PassWord_Write,
|
|
PassWord_Default, PassWord_Default, _KeyPath) != 0) &&
|
|
(_UsbKey2000.SetWritePassword(PassWord_Default, PassWord_Default,
|
|
PassWord_Default, PassWord_Default, _KeyPath) != 0))
|
|
{
|
|
Message = "重置写入密码失败。";
|
|
return false;
|
|
}
|
|
//设置写入密码
|
|
//Message = "正在设置新密码...";
|
|
if (_UsbKey2000.SetWritePassword(PassWord_Default, PassWord_Default,
|
|
PassWord_Write, PassWord_Write, _KeyPath) != 0)
|
|
{
|
|
Message = "设置写入密码失败。";
|
|
return false;
|
|
}
|
|
//设置读取密码
|
|
if (_UsbKey2000.SetReadPassword(PassWord_Write, PassWord_Write,
|
|
PassWord_Read, PassWord_Read, _KeyPath) != 0)
|
|
{
|
|
Message = "设置读取密码失败。";
|
|
return false;
|
|
}
|
|
//Message = "成功重置密码。";
|
|
return true;
|
|
}
|
|
#endregion
|
|
|
|
#region Read
|
|
public override bool Read(out ClientSecurityDeviceObject keyInfo)
|
|
{
|
|
keyInfo = new ClientSecurityDeviceObject();
|
|
try
|
|
{
|
|
Message = "正在读取数字证书...";
|
|
int _Length = ClientSecurityDeviceObject.StartIndex;
|
|
keyInfo.Name = ReadInfo(ClientSecurityDeviceObject.StartIndex_Name, ClientSecurityDeviceObject.Length_Name, ref _Length);
|
|
keyInfo.User = ReadInfo(ClientSecurityDeviceObject.StartIndex_User, ClientSecurityDeviceObject.Length_User, ref _Length);
|
|
keyInfo.Key = ReadInfo(ClientSecurityDeviceObject.StartIndex_Key, ClientSecurityDeviceObject.Length_Key, ref _Length);
|
|
keyInfo.Date = ReadInfo(ClientSecurityDeviceObject.StartIndex_Date, ClientSecurityDeviceObject.Length_Date, ref _Length);
|
|
keyInfo.Url = ReadInfo(ClientSecurityDeviceObject.StartIndex_Url, ClientSecurityDeviceObject.Length_Url, ref _Length);
|
|
//Message = ReadDevice(0, 500);
|
|
Message = "成功读取数字证书。";
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Message = "读取数字证书失败," + ex.Message;
|
|
return true;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
|
|
|
|
#region Write
|
|
|
|
public override bool Write(ClientSecurityDeviceObject keyInfo,string password)
|
|
{
|
|
Message = "正在写入数字证书...";
|
|
if (password != "SuperMap.RealEstate.SecurityDevices")
|
|
{
|
|
Message = "写入数字证书失败「密钥错误」";
|
|
}
|
|
int _Length = ClientSecurityDeviceObject.StartIndex;
|
|
if (!WriteInfo(keyInfo.Name, ClientSecurityDeviceObject.StartIndex_Name, ref _Length) ||
|
|
!WriteInfo(keyInfo.User, ClientSecurityDeviceObject.StartIndex_User, ref _Length) ||
|
|
!WriteInfo(keyInfo.Key, ClientSecurityDeviceObject.StartIndex_Key, ref _Length) ||
|
|
!WriteInfo(keyInfo.Date, ClientSecurityDeviceObject.StartIndex_Date, ref _Length) ||
|
|
!WriteInfo(keyInfo.Url, ClientSecurityDeviceObject.StartIndex_Url, ref _Length))
|
|
{
|
|
Message = "写入数字证书失败。";
|
|
return false;
|
|
}
|
|
Message = "成功写入数字证书。";
|
|
return true;
|
|
}
|
|
#endregion
|
|
|
|
#region WriteInfo
|
|
private bool WriteInfo(string value, int keyStart, ref int valueStart)
|
|
{
|
|
int _Length = UsbKey2000.lstrlenA(value);
|
|
if (WriteDevice(_Length.ToString(), keyStart, ref _Length) == false)
|
|
return false;
|
|
|
|
if (WriteDevice(value, valueStart, ref valueStart) == false)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
#endregion
|
|
|
|
#region ReadInfo
|
|
private string ReadInfo(int keyStart, int keyLength, ref int valueStart)
|
|
{
|
|
string _Str = ReadDevice(keyStart, keyLength);
|
|
int _Length = 0;
|
|
if (!int.TryParse(_Str, out _Length))
|
|
{
|
|
throw new Exception("位置控制标识应该为数字。");
|
|
}
|
|
_Str = ReadDevice(valueStart, _Length);
|
|
valueStart += UsbKey2000.lstrlenA(_Str);
|
|
return _Str;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|