#include "StdAfx.h" #include "LicenseMgr.h" #include #pragma comment(lib, "Iphlpapi.lib") SINGLETON_IMPLEMENT(CLicenseMgr) CLicenseMgr::CLicenseMgr(void) { } CLicenseMgr::~CLicenseMgr( void ) { } /***************************************************************** **【函数名称】 __vaildTimer **【函数功能】 检测定时器 **【参数】 **【返回值】 ****************************************************************/ void CALLBACK CLicenseMgr::__vaildTimer( void ) { if (!CLicenseMgr::GetInstance().__checkTime(CLicenseMgr::GetInstance().m_License)) { PostQuitMessage(0); } } /***************************************************************** **【函数名称】 __readlicense **【函数功能】 读授权 **【参数】 **【返回值】 ****************************************************************/ bool CLicenseMgr::__readlicense( CString& License ) { TCHAR path[MAX_PATH] = { 0 }; //获得当前应用程序路径 if(GetCurrentDirectory (MAX_PATH, path) == 0) return false; CString LicFileName; #ifdef _DEBUG LicFileName.Format(_T("D:\\DATA\\project\\HY\\Middleware\\Debug\\lic.txt")); #else LicFileName.Format(_T("%s\\lic.txt"), path); #endif CFile File; if(!File.Open(LicFileName, CFile::modeRead)) return false; TCHAR LicCode[1024] = { 0 }; File.Read(LicCode, 1024); License = LicCode; File.Close(); return true; } /***************************************************************** **【函数名称】 __decode **【函数功能】 解密授权码 **【参数】 **【返回值】 ****************************************************************/ CString CLicenseMgr::__decode( CString LicCode ) { CString strValue; char szAr1[1024]; char szAr2[1024]; char szAr3[1024]; memset(szAr1, 0, 1024); memset(szAr2, 0, 1024); memset(szAr3, 0, 1024); // 获得字符串长度 int nCodeLen = LicCode.GetLength(); // 拷贝源字符串到szAr3 lstrcpy(szAr3, LicCode); // 加密字符串恢复顺序 for (int nIndex = 0; nIndex < (nCodeLen-(nCodeLen % 2))/2; nIndex++) { if ((nIndex % 2) == 0 && nIndex < (nCodeLen - nIndex -1)) { char cTmp = szAr3[nIndex]; szAr3[nIndex] = szAr3[nCodeLen - nIndex -1]; szAr3[nCodeLen - nIndex -1] = cTmp; } } //int t1[] = { 6, 12, 5, 9, 10, 0, 13, 8, 15, 3, 14, 4, 2, 11, 1, 7 }; int t2[] = { 5, 14, 12, 9, 11, 2, 0, 15, 7, 3, 4, 13, 1, 6, 10, 8 }; char t3[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; int i=0, j=0; int p, q, temp; while ((i < nCodeLen) && (szAr3[i] != '\0 ')) { szAr1[j] = szAr3[i]; p=0; while((p <16) && (szAr1[j] != t3[p++])); szAr2[j] = szAr3[i+1]; q=0; while ((q <16) && (szAr2[j] != t3[q++])); szAr1[j] = (char)(t2[p-1]*16 + t2[q-1]); temp = (int)szAr1[j]; temp = temp ^ 0xFF; szAr1[j]=(char)temp; i=i+2; j++; } szAr1[j] = 0; strValue.Format("%s", szAr1); return strValue; } /***************************************************************** **【函数名称】 __getValue **【函数功能】 获取授权码指定值 **【参数】 **【返回值】 ****************************************************************/ CString CLicenseMgr::__getValue( CString License, License_Key_Type Key ) { CString szString[12]; int i = 0; while(1) { int pos = License.Find('|'); if(pos>=0){ szString[i] = License.Left(pos); License = License.Mid(pos+1); } else { szString[i] = License; break; } i++; } CString returnVlue; switch (Key) { case License_Key_CpuId: returnVlue = szString[License_Key_CpuId]; break; case License_Key_PhysicalValue: returnVlue = szString[License_Key_PhysicalValue]; break; case License_Key_HardDiskId: returnVlue = szString[License_Key_HardDiskId]; break; case License_Key_TrunkNum: returnVlue = szString[License_Key_TrunkNum]; break; case License_Key_AgentNum: returnVlue = szString[License_Key_AgentNum]; break; case License_Key_FaxNum: returnVlue = szString[License_Key_FaxNum]; break; case License_Key_IvrFlowNum: returnVlue = szString[License_Key_IvrFlowNum]; break; case License_Key_VoipNum: returnVlue = szString[License_Key_VoipNum]; break; case License_Key_EndTime: returnVlue = szString[License_Key_EndTime]; break; case License_Key_BeginTime: returnVlue = szString[License_Key_BeginTime]; break; } return returnVlue.Mid(returnVlue.Find('=') + 1); } /***************************************************************** **【函数名称】 __getCpuId **【函数功能】 读取CPU ID **【参数】 **【返回值】 ****************************************************************/ bool CLicenseMgr::__getCpuId( CString& CpuId ) { char szOEMString[13]; // CPU名称 int iEAXValue, iEBXValue, iECXValue, iEDXValue; _asm { mov eax, 0 cpuid mov DWORD PTR szOEMString, ebx mov DWORD PTR szOEMString+4, edx mov DWORD PTR szOEMString+8, ecx mov BYTE PTR szOEMString+12, 0 } _asm { mov eax,1 cpuid mov iEAXValue, eax mov iEBXValue, ebx mov iECXValue, ecx mov iEDXValue, edx } int iCPUFamily = (0xf00 & iEAXValue) >> 8; // CPU系列 char szFamily[256] = { 0 }; _itoa_s(iCPUFamily, szFamily, 256, 10); _asm { mov eax, 2 CPUID } char szCPUID[256] = { NULL }; // CPUID号 unsigned long s1 = 0, s2 = 0; _asm { mov eax, 01h xor edx, edx cpuid mov s1, edx mov s2, eax } sprintf_s(szCPUID, "%08X%08X", s1, s2); // CPU ID信息整合 CpuId.Format("%s%s%s", szOEMString, szFamily, szCPUID); return true; } /***************************************************************** **【函数名称】 __getMac **【函数功能】 读取MAC **【参数】 **【返回值】 ****************************************************************/ bool CLicenseMgr::__getMac( CString& Mac ) { bool Res = false; // 获取MAC地址相关参数定义 ULONG uFamily = AF_UNSPEC; ULONG uFlag = GAA_FLAG_INCLUDE_PREFIX; PIP_ADAPTER_ADDRESSES pIaa = NULL; DWORD uBufLen = 15000; // 获取网卡MAC地址 pIaa = (IP_ADAPTER_ADDRESSES*)malloc(uBufLen); DWORD dwResult = GetAdaptersAddresses(uFamily, uFlag, NULL, pIaa, &uBufLen); if ( dwResult == NO_ERROR ) { for (int i = 0; i < 6; i++) { CString strTmp; strTmp.Format("%02X", *(pIaa->PhysicalAddress + i)); Mac += strTmp; } // end for Res = true; } free(pIaa); return Res; } /***************************************************************** **【函数名称】 __getTimeFromString **【函数功能】 由时间字符串生成时间对象 **【参数】 **【返回值】 ****************************************************************/ CTime CLicenseMgr::__getTimeFromString( LPCTSTR TimeString ) { int nYear, nMonth, nDate, nHour, nMin, nSec; sscanf_s(TimeString, "%d-%d-%d %d:%d:%d", &nYear, &nMonth, &nDate, &nHour, &nMin, &nSec); CTime t(nYear, nMonth, nDate, nHour, nMin, nSec); return t; } /***************************************************************** **【函数名称】 __checkTimeInReg **【函数功能】 检测注册表授权时间,防止出现修改系统时间来延长授权 **【参数】 **【返回值】 ****************************************************************/ bool CLicenseMgr::__checkTimeInReg( void ) { CRegKey key; if(key.Open(HKEY_CURRENT_USER, "Software\\Midware\\CurTime", KEY_READ|KEY_WRITE) != ERROR_SUCCESS) { key.Create(HKEY_CURRENT_USER, "Software\\Midware\\CurTime"); } ULONG nSize = OTL_REG_KEY_SIZE; CHAR szTmp[OTL_REG_KEY_SIZE] = { 0 }; key.QueryStringValue("TIME", szTmp, &nSize); CString strTime; //注册表时间记录 strTime.Format("%s",szTmp); CTime Curtime = CTime::GetCurrentTime(); // 系统当前时间 time_t Systime = Curtime.GetTime(); if (strTime.IsEmpty()) // 如果为空(第一次打开),则写入当前时间 { CString strBuff; strBuff.Format("%d",Systime); key.SetStringValue("TIME",strBuff); return true; } time_t Regtime = atol(strTime.GetBuffer(0)); if (Systime >= Regtime) // 如果大于注册表时间,则在授权范围内 { CString strBuff; strBuff.Format("%d",Systime); key.SetStringValue("TIME",strBuff); return true; } else // 如果小于注册表时间,则被认为是超出授权范围 { return false; } } /***************************************************************** **【函数名称】 __checkCpuId **【函数功能】 验证CPUID **【参数】 **【返回值】 ****************************************************************/ bool CLicenseMgr::__checkCpuId( CString License ) { CString CpuId; if(!__getCpuId(CpuId)) return false; CString CpuIdInLic = __getValue(License, License_Key_CpuId); return CpuId == CpuIdInLic; } /***************************************************************** **【函数名称】 __checkMac **【函数功能】 验证MAC **【参数】 **【返回值】 ****************************************************************/ bool CLicenseMgr::__checkMac( CString License ) { CString Mac; if(!__getMac(Mac)) return false; CString MacInLic = __getValue(License, License_Key_PhysicalValue); return Mac == MacInLic; } /***************************************************************** **【函数名称】 __checkTime **【函数功能】 验证时间 **【参数】 **【返回值】 ****************************************************************/ bool CLicenseMgr::__checkTime( CString License ) { if(!__checkTimeInReg()) { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{License}: 许可证注册时间到期")); return false; } CTime tCurrentTime = CTime::GetCurrentTime(); CTime tEndTime; CString strEndTime = __getValue(License, License_Key_EndTime); if (!strEndTime.IsEmpty()) { tEndTime = __getTimeFromString(strEndTime); } CTime tBeginTime; CString strBeginTime = __getValue(License, License_Key_BeginTime); if (!strBeginTime.IsEmpty()) { tBeginTime = __getTimeFromString(strBeginTime); } if ((tCurrentTime - tEndTime) > 0 || (tBeginTime - tCurrentTime > 0) || strBeginTime.IsEmpty() || strEndTime.IsEmpty()) { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{License}: 许可证时间到期: %s"), strEndTime); return false; } ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_NORMAL, _T("{License}: 许可证结束时间: %s"), strEndTime); return true; } /***************************************************************** **【函数名称】 checkLicense **【函数功能】 检查授权 **【参数】 **【返回值】 ****************************************************************/ bool CLicenseMgr::checkLicense( void ) { if(!__readlicense(m_License)) { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{License}: 读取许可信息失败")); return false; } m_License = __decode(m_License); if(m_License == "") { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{License}: 解析许可信息失败")); return false; } if(!__checkCpuId(m_License)) { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{License}: 核对CPU信息失败")); return false; } if(!__checkMac(m_License)) { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{License}: 核对MAC信息失败")); return false; } if(!__checkTime(m_License)) { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{License}: 核对时间信息失败")); return false; } // 定时器 SetTimer(NULL, NULL, 1000*60*60, (TIMERPROC)__vaildTimer); return true; }