| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- #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<len; i++)
- {
- if (rin[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<len;i+=2)
- {
- rout[i/2] = rin[i]*16 + rin[i+1];
- }
- rout[i] = 0;
- }
- void CSNRegister::encrypt(char *byCode, char *bySn, char *byUseTime, char *byPName, char *byStartTime, char *byEndTime, char *byLeftTime)
- {
- yDES ydes;
- BYTE sztmp[1024] = {0};
- // 特征码 1;
- //nlen = strlen(byCode);
- ydes.DES_Act(sztmp, (BYTE*)byCode, strlen(byCode)+1, (BYTE*)KeyCode[0], 8);
- ShowCihper(sztmp,strlen(byCode)+1);
- m_strCode = sztmp;
- // SN码 2;
- //nlen = strlen(bySn);
- memset(sztmp,0,1024);
- ydes.DES_Act(sztmp, (BYTE*)bySn, strlen(bySn)+1, (BYTE*)KeyCode[1], 8);
- ShowCihper(sztmp,strlen(bySn)+1);
- m_strSn = sztmp;
- // 试用时间 3;
- //nlen = strlen(byUseTime);
- memset(sztmp,0,1024);
- ydes.DES_Act(sztmp, (BYTE*)byUseTime, strlen(byUseTime)+1, (BYTE*)KeyCode[2], 8);
- ShowCihper(sztmp,strlen(byUseTime)+1);
- m_strUseTime = sztmp;
- // 项目名称 4;
- //nlen = strlen(byPName);
- memset(sztmp,0,1024);
- ydes.DES_Act(sztmp, (BYTE*)byPName, strlen(byPName)+1, (BYTE*)KeyCode[3], 8);
- ShowCihper(sztmp,strlen(byPName)+1);
- m_strPName = sztmp;
- // 建成时间 5;
- //nlen = strlen(byStartTime);
- memset(sztmp,0,1024);
- ydes.DES_Act(sztmp, (BYTE*)byStartTime, strlen(byStartTime)+1, (BYTE*)KeyCode[4], 8);
- ShowCihper(sztmp,strlen(byStartTime)+1);
- m_strStartTime = sztmp;
- // 完成时间 6;
- //nlen = strlen(byEndTime);
- memset(sztmp,0,1024);
- ydes.DES_Act(sztmp, (BYTE*)byEndTime, strlen(byEndTime)+1, (BYTE*)KeyCode[5], 8);
- ShowCihper(sztmp,strlen(byEndTime)+1);
- m_strEndTime = sztmp;
- // 剩余时间 7;
- //nlen = strlen(byLeftTime);
- memset(sztmp,0,1024);
- ydes.DES_Act(sztmp, (BYTE*)byLeftTime, strlen(byLeftTime)+1, (BYTE*)KeyCode[6], 8);
- ShowCihper(sztmp,strlen(byLeftTime)+1);
- m_strLeftTime = sztmp;
- }
- void CSNRegister::decrypt(char *byCode, char *bySn, char *byUseTime, char *byPName, char *byStartTime, char *byEndTime, char *byLeftTime)
- {
- }
- BOOL CSNRegister::itf_IsCorrectSN(const char *szfile)
- {
- Load(szfile);
- if ( m_strStart != "127" || m_strEndof != "13" || (GetCheck(OPEN) == -1))
- return FALSE;
- return TRUE;
- }
- BOOL CSNRegister::itf_read_snfile(const char *szfile)
- {
- if ( !itf_IsCorrectSN(szfile))
- return FALSE;
- yDES ydes;
- BYTE sztmp[1024] = {0};
- BYTE szOut[1024] = {0};
- // 1.特征码;
- memcpy(sztmp,m_strCode.GetBuffer(),m_strCode.GetLength());
- HalfByteToByte(sztmp,szOut,m_strCode.GetLength());
- memset(sztmp,0,1024);
- ydes.DES_Act(sztmp,szOut,m_strCode.GetLength(),(BYTE*)KeyCode[0],8,DECRYPT);m_strCode.ReleaseBuffer();
- m_strCode = "";
- m_strCode = sztmp;
- // 2.SN码;
- //nlen = m_strSn.GetLength();
- memset(sztmp,0,1024);
- memset(szOut,0,1024);
- memcpy(sztmp,m_strSn.GetBuffer(),m_strSn.GetLength());
- HalfByteToByte(sztmp,szOut,m_strSn.GetLength());
- memset(sztmp,0,1024);
- ydes.DES_Act(sztmp,szOut,m_strSn.GetLength(),(BYTE*)KeyCode[1],8,DECRYPT);m_strSn.ReleaseBuffer();
- m_strSn = "";
- m_strSn = sztmp;
- // 3.试用时间;
- memset(sztmp,0,1024);
- memset(szOut,0,1024);
- memcpy(sztmp,m_strUseTime.GetBuffer(),m_strUseTime.GetLength());
- HalfByteToByte(sztmp,szOut,m_strUseTime.GetLength());
- memset(sztmp,0,1024);
- ydes.DES_Act(sztmp,szOut,m_strUseTime.GetLength(),(BYTE*)KeyCode[2],8,DECRYPT);m_strUseTime.ReleaseBuffer();
- m_strUseTime = "";
- m_strUseTime = sztmp;
- // 4.项目名称;
- memset(sztmp,0,1024);
- memset(szOut,0,1024);
- memcpy(sztmp,m_strPName.GetBuffer(),m_strPName.GetLength());
- HalfByteToByte(sztmp,szOut,m_strPName.GetLength());
- memset(sztmp,0,1024);
- ydes.DES_Act(sztmp,szOut,m_strPName.GetLength(),(BYTE*)KeyCode[3],8,DECRYPT);m_strPName.ReleaseBuffer();
- m_strPName = "";
- m_strPName = sztmp;
- // 5.建成时间;
- memset(sztmp,0,1024);
- memset(szOut,0,1024);
- memcpy(sztmp,m_strStartTime.GetBuffer(),m_strStartTime.GetLength());
- HalfByteToByte(sztmp,szOut,m_strStartTime.GetLength());
- memset(sztmp,0,1024);
- ydes.DES_Act(sztmp,szOut,m_strStartTime.GetLength(),(BYTE*)KeyCode[4],8,DECRYPT);m_strStartTime.ReleaseBuffer();
- m_strStartTime = "";
- m_strStartTime = sztmp;
- // 6.完成时间;
- memset(sztmp,0,1024);
- memset(szOut,0,1024);
- memcpy(sztmp,m_strEndTime.GetBuffer(),m_strEndTime.GetLength());
- HalfByteToByte(sztmp,szOut,m_strEndTime.GetLength());
- memset(sztmp,0,1024);
- ydes.DES_Act(sztmp,szOut,m_strEndTime.GetLength(),(BYTE*)KeyCode[5],8,DECRYPT);m_strEndTime.ReleaseBuffer();
- m_strEndTime = "";
- m_strEndTime = sztmp;
- // 7.剩余时间;
- memset(sztmp,0,1024);
- memset(szOut,0,1024);
- memcpy(sztmp,m_strLeftTime.GetBuffer(),m_strLeftTime.GetLength());
- HalfByteToByte(sztmp,szOut,m_strLeftTime.GetLength());
- memset(sztmp,0,1024);
- ydes.DES_Act(sztmp,szOut,m_strLeftTime.GetLength(),(BYTE*)KeyCode[6],8,DECRYPT);m_strLeftTime.ReleaseBuffer();
- m_strLeftTime = "";
- m_strLeftTime = sztmp;
- return TRUE;
- }
- int CSNRegister::GetCheck(BOOL btype)
- {
- char szCheck[1024] = {0};
- DWORD dwSum = 0;
- GetByteSum(dwSum ,m_strCode);
- GetByteSum(dwSum ,m_strSn);
- GetByteSum(dwSum ,m_strUseTime);
- GetByteSum(dwSum ,m_strPName);
- GetByteSum(dwSum ,m_strStartTime);
- GetByteSum(dwSum ,m_strEndTime);
- GetByteSum(dwSum ,m_strLeftTime);
- dwSum = ~dwSum;
- dwSum++;
- itoa(dwSum, szCheck, 16);
- for (int i(0); i < 5 ; i++)
- {
- if( szCheck[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;
- }
|