#include "StdAfx.h" #include "yDES.h" #include ".\snregister.h" char KeyCode[7][9] = { // 密钥1; 'A','T','O','M','8','2','8','K',0, // 密钥2; 'A','K','C','L','8','9','8','T',0, // 密钥3; 'A','B','J','I','8','7','8','P',0, // 密钥4; 'K','U','C','G','8','6','8','E',0, // 密钥5; 'A','A','S','D','7','0','8','S',0, // 密钥6; 'W','B','V','B','9','0','8','X',0, // 密钥7; 'E','B','T','N','1','0','7','Z',0, }; CSNRegister::CSNRegister() { memset(m_sztemp,0,1024); ClearAll(); } CSNRegister::~CSNRegister() { } void CSNRegister::ClearAll() { m_strStart = ""; m_strCode = ""; m_strSn = ""; m_strUseTime = ""; m_strPName = ""; m_strStartTime = ""; m_strEndTime = ""; m_strLeftTime = ""; m_strCheck = ""; m_strEndof = ""; } BOOL CSNRegister::Load(const char *&pStrDirectroy) { CFile file; if (file.Open(pStrDirectroy, CFile::modeRead)) { CArchive ar(&file,CArchive::load); Serialize(ar); ar.Close(); file.Close(); } if ( m_strStart != "127" || m_strEndof != "13") { return FALSE; } return TRUE; } void CSNRegister::Store(const char *&pStrDirectroy) { CFile file; CFileException e; if(file.Open(pStrDirectroy, CFile::modeWrite|CFile::modeCreate, &e)) { CArchive ar(&file,CArchive::store); Serialize(ar); ar.Close(); file.Close(); } else { #ifdef _DEBUG afxDump << "File could not be opened " << e.m_cause << "\n"; #endif } } void CSNRegister::Serialize(CArchive& ar) { CObject::Serialize(ar); if(ar.IsStoring()) { ar << m_strStart; ar << m_strCode; ar << m_strSn; ar << m_strUseTime; ar << m_strPName; ar << m_strStartTime; ar << m_strEndTime; ar << m_strLeftTime; ar << m_strCheck; ar << m_strEndof; } else { ar >> m_strStart; ar >> m_strCode; ar >> m_strSn; ar >> m_strUseTime; ar >> m_strPName; ar >> m_strStartTime; ar >> m_strEndTime; ar >> m_strLeftTime; ar >> m_strCheck; ar >> m_strEndof; } } void CSNRegister::ShowCihper(BYTE *rtext,long len) { len=(len+7)&0xfffffff8 ; int i; int p; BYTE rtext2[1024] = {0}; unsigned char c1,c2,c3; //处理 p = 0; for (i = 0; i < len; i++) { c3 = rtext[i]; c1 = c3 >> 4; c2 = c3 & 15; if (c1 <=9 && c1 >= 0) c1=c1+'0'; if (c1 <=15 && c1 >= 10) c1=c1-10+'A'; if (c2 <=9 && c2 >= 0) c2=c2+'0'; if (c2 <=15 && c2 >= 10) c2=c2-10+'A'; rtext2[p]=c1; rtext2[p+1]=c2; p+=2; } rtext2[p] = 0; memcpy(rtext,rtext2,p+1); } void CSNRegister::HalfByteToByte(BYTE *rin,BYTE *rout, long len) { int i =0; for (i=0; i='0' && rin[i] <= '9') rin[i] = rin[i]-'0'; if (rin[i]>='A' && rin[i] <= 'F') rin[i] = rin[i]-'A'+10; } for (i=0;i= 'a' && szCheck[i] <= 'z' ) { szCheck[i] = szCheck[i] - 'a' + 'A'; } } if (btype == CREATE) { m_strCheck = szCheck; } else if ( btype == OPEN) { if ( m_strCheck.Compare(szCheck) != 0) return -1; } return 0; } void CSNRegister::GetByteSum(DWORD &dwSum, CString &strSrc) { //int len = strSrc.GetLength(); for (int i = 0; i < strSrc.GetLength(); i++) { dwSum += strSrc.GetAt(i); } } BOOL CSNRegister::itf_create_snfile(const char *szfile, char *byCode, char *bySn, char *byUseTime, char *byPName, char *byStartTime, char *byEndTime, char *byLeftTime) { m_strStart = "127"; m_strEndof = "13"; encrypt(byCode,bySn,byUseTime,byPName,byStartTime,byEndTime,byLeftTime); GetCheck(CREATE); Store(szfile); return 0; } BOOL CSNRegister::itf_modify_snfile(const char *szfile, char *byCode, char *bySn, char *byUseTime, char *byPName, char *byStartTime, char *byEndTime, char *byLeftTime) { return 0; } void CSNRegister::itf_SavebyLTime(const DWORD &dwLTime,const char *szPath) { char szLTime[30] = {0}; itoa(dwLTime, szLTime ,10); yDES ydes; BYTE sztmp[250] = {0}; // 剩余时间; m_strLeftTime = ""; ydes.DES_Act(sztmp, (BYTE*)szLTime, strlen(szLTime)+1, (BYTE*)KeyCode[6], 8); ShowCihper(sztmp,strlen(szLTime)+1); m_strLeftTime = sztmp; GetCheck(CREATE); Store(szPath); } DWORD CSNRegister::itf_returnLeftTime() { // 剩余时间 解码; yDES ydes; char sztmp[1024] = {0}; BYTE szOut[1024] = {0}; memcpy(sztmp,m_strLeftTime.GetBuffer(),m_strLeftTime.GetLength()); HalfByteToByte((BYTE*)sztmp,szOut,m_strLeftTime.GetLength()); memset(sztmp,0,1024); ydes.DES_Act((BYTE*)sztmp,szOut,m_strLeftTime.GetLength(),(BYTE*)KeyCode[6],8,DECRYPT);m_strLeftTime.ReleaseBuffer(); return atol(sztmp); } char *CSNRegister::itf_returnSnCode() { // SN序列号 解码; yDES ydes; BYTE szOut[1024] = {0}; memset(m_sztemp,0,1024); memcpy(m_sztemp,m_strSn.GetBuffer(),m_strSn.GetLength()); HalfByteToByte((BYTE *)m_sztemp,szOut,m_strSn.GetLength()); memset(m_sztemp,0,1024); ydes.DES_Act((BYTE *)m_sztemp,szOut,m_strSn.GetLength(),(BYTE*)KeyCode[1],8,DECRYPT);m_strSn.ReleaseBuffer(); return m_sztemp; } char *CSNRegister::itf_returnCheckCode() { // SN序列号 解码; yDES ydes; BYTE szOut[1024] = {0}; memset(m_sztemp,0,1024); memcpy(m_sztemp,m_strSn.GetBuffer(),m_strSn.GetLength()); HalfByteToByte((BYTE *)m_sztemp,szOut,m_strSn.GetLength()); memset(m_sztemp,0,1024); ydes.DES_Act((BYTE *)m_sztemp,szOut,m_strSn.GetLength(),(BYTE*)KeyCode[1],8,DECRYPT);m_strSn.ReleaseBuffer(); return m_sztemp; }