/*******************************************************************************
* Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
* 类的信息:
* 1.程序名称:LockLicenseHandle.cs
* 2.功能描述:License文件管理。
* 编辑履历:
* 作者 日期 版本 修改内容
* 张国印 2014/09/16 1.00 新建
*******************************************************************************/
using System;
using System.Data;
using System.IO;
using System.Management;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;
using Dongke.IBOSS.PRD.Basics.Library;
namespace Dongke.IBOSS.PRD.Service.DKIBOSSPRDLogic
{
///
/// License文件管理
///
public class LockLicenseHandle
{
//private const uint ELEP_SUCCESS = 0x80000000; // 成功
//private const uint ELEP_NO_MORE_DEVICE = 0x80000009; // 没有符合条件的设备
//private const uint ELEP_ERR_INVALID_PASSWORD = 0x80010002; // 有锁,但PIN密码错
//private const uint ELEP_INVALID_DEVICE_HANDLE = 0x80000004; // 传入的设备句柄无效
//private const uint ELEP_ERROR_HW_ERROR = 0x80010001; // 硬件错误,可能由硬件损坏引起。
//private const uint ELEP_ERR_INVALID_STATE = 0x80010009; // 无效状态,升级失败将会将设备置为无 效状态,升级成功可以解除无效状态。
//private const uint ELEP_ERR_NO_PRIVILEGE = 0x80010006; // 无开发商权限。
//private const uint ELEP_NOT_USER_DEVICE = 0x80000026; //设备不是用户设备。
//private const uint ELEP_INVALID_PARAMETER = 0x80000001; //参数错误,可能因为传入了空指针。
//private const uint ELEP_ERR_OPT = 0x80010003; //用户信息区已经写过一次。
//private const uint ELEP_ERR_INVALID_ADDR_OR_SIZE = 0x80010004; //读写偏移量或长度错误。
//private const ushort CUSTOMER_CODE_LENGTH = 100; // 写入锁内最长的客户编码字节数
//private const ushort CUSTOMER_NAME_LENGTH = 600; // 写入锁内最长的客户名称字节数
//private const ushort CUSTOMER_START_POINT = 1152; // 开始写锁区域
//private const uint OLD_PIN_1 = 0x30303030;
//private const uint OLD_PIN_2 = 0x30303030;
//private const uint OLD_PIN_3 = 0x30303030;
//private const uint OLD_PIN_4 = 0x30303030;
private static string _uuid = null;
///
/// 读取License文件内容
///
/// license文件路径
/// 读取的license内容
///
/// 0:读取成功
/// -1:license文件不存在
/// -2:license文件错误
/// -3:license过期
///
public static int ReadLisenceFile(string listCode, ref DataSet customer, out string licString)
{
licString = null;
try
{
string sid = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("DBSetting", "SID").ToUpper();
string path = AppDomain.CurrentDomain.BaseDirectory + "LIC\\LIC_" + listCode + "_" + sid + ".lic";
if (!File.Exists(path))
{
return -1;
}
string[] lics = File.ReadAllLines(path, Encoding.UTF32);
string l1 = Encryption.DecryptDES(lics[0], "DK#LIC#CODE#SID");
string[] keys = l1.Split('#');
if (listCode != keys[1] || sid != keys[2].ToUpper())
{
return -2;
}
//licString = lics[1];
string key = Encryption.DecryptDES(lics[1], "DK#CODE#" + keys[1].ToLower());
string iv = Encryption.DecryptDES(lics[2], "DK#SID#" + keys[2].ToLower());
string lic = Encryption.DecryptDES(lics[3], "DK#" + l1.ToUpper());
MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(lic));
memoryStream.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
Rijndael age = Rijndael.Create();
CryptoStream cStream = new CryptoStream(memoryStream,
age.CreateDecryptor(Convert.FromBase64String(key), Convert.FromBase64String(iv)),
CryptoStreamMode.Read);
DataSet data = (DataSet)formatter.Deserialize(cStream);
if (listCode != data.Tables["Info"].Rows[0]["ListCode"]+"" ||
sid != data.Tables["Info"].Rows[0]["SID"].ToString().ToUpper())
{
return -2;
}
if (data.Tables["UUID"].Rows.Count > 0)
{
string uuid = Encryption.EncryptDES(UUID + "_" + sid, sid);
DataRow[] drs = data.Tables["UUID"].Select("UUID = '" + uuid + "'");
if (drs == null || drs.Length == 0)
{
return -2;
}
}
DateTime vBegin = Convert.ToDateTime(data.Tables["Info"].Rows[0]["ValidityBegin"]);
DateTime vEnd = Convert.ToDateTime(data.Tables["Info"].Rows[0]["ValidityEnd"]);
DateTime n = DateTime.Now.Date;
if (n < vBegin || n > vEnd)
{
return -3;
}
licString = Convert.ToDateTime(data.Tables["Info"].Rows[0]["LicCreateTime"]).Ticks.ToString();
customer = data;
return 0;
}
catch (Exception)
{
return -2;
}
}
public static string UUID
{
get
{
if (_uuid != null)
{
return _uuid;
}
try
{
string code = null;
SelectQuery query = new SelectQuery("select * from Win32_ComputerSystemProduct");
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
foreach (var item in searcher.Get())
{
using (item)
{
code = item["UUID"].ToString();
}
}
}
_uuid = code;
}
catch
{
_uuid = "";
}
return _uuid;
}
}
}
}