SNRegister.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #include "StdAfx.h"
  2. #include "yDES.h"
  3. #include ".\snregister.h"
  4. char KeyCode[7][9] =
  5. {
  6. // 密钥1;
  7. 'A','T','O','M','8','2','8','K',0,
  8. // 密钥2;
  9. 'A','K','C','L','8','9','8','T',0,
  10. // 密钥3;
  11. 'A','B','J','I','8','7','8','P',0,
  12. // 密钥4;
  13. 'K','U','C','G','8','6','8','E',0,
  14. // 密钥5;
  15. 'A','A','S','D','7','0','8','S',0,
  16. // 密钥6;
  17. 'W','B','V','B','9','0','8','X',0,
  18. // 密钥7;
  19. 'E','B','T','N','1','0','7','Z',0,
  20. };
  21. CSNRegister::CSNRegister()
  22. {
  23. memset(m_sztemp,0,1024);
  24. ClearAll();
  25. }
  26. CSNRegister::~CSNRegister()
  27. {
  28. }
  29. void CSNRegister::ClearAll()
  30. {
  31. m_strStart = "";
  32. m_strCode = "";
  33. m_strSn = "";
  34. m_strUseTime = "";
  35. m_strPName = "";
  36. m_strStartTime = "";
  37. m_strEndTime = "";
  38. m_strLeftTime = "";
  39. m_strCheck = "";
  40. m_strEndof = "";
  41. }
  42. BOOL CSNRegister::Load(const char *&pStrDirectroy)
  43. {
  44. CFile file;
  45. if (file.Open(pStrDirectroy, CFile::modeRead))
  46. {
  47. CArchive ar(&file,CArchive::load);
  48. Serialize(ar);
  49. ar.Close();
  50. file.Close();
  51. }
  52. if ( m_strStart != "127" || m_strEndof != "13")
  53. {
  54. return FALSE;
  55. }
  56. return TRUE;
  57. }
  58. void CSNRegister::Store(const char *&pStrDirectroy)
  59. {
  60. CFile file;
  61. CFileException e;
  62. if(file.Open(pStrDirectroy, CFile::modeWrite|CFile::modeCreate, &e))
  63. {
  64. CArchive ar(&file,CArchive::store);
  65. Serialize(ar);
  66. ar.Close();
  67. file.Close();
  68. }
  69. else
  70. {
  71. #ifdef _DEBUG
  72. afxDump << "File could not be opened " << e.m_cause << "\n";
  73. #endif
  74. }
  75. }
  76. void CSNRegister::Serialize(CArchive& ar)
  77. {
  78. CObject::Serialize(ar);
  79. if(ar.IsStoring())
  80. {
  81. ar << m_strStart;
  82. ar << m_strCode;
  83. ar << m_strSn;
  84. ar << m_strUseTime;
  85. ar << m_strPName;
  86. ar << m_strStartTime;
  87. ar << m_strEndTime;
  88. ar << m_strLeftTime;
  89. ar << m_strCheck;
  90. ar << m_strEndof;
  91. }
  92. else
  93. {
  94. ar >> m_strStart;
  95. ar >> m_strCode;
  96. ar >> m_strSn;
  97. ar >> m_strUseTime;
  98. ar >> m_strPName;
  99. ar >> m_strStartTime;
  100. ar >> m_strEndTime;
  101. ar >> m_strLeftTime;
  102. ar >> m_strCheck;
  103. ar >> m_strEndof;
  104. }
  105. }
  106. void CSNRegister::ShowCihper(BYTE *rtext,long len)
  107. {
  108. len=(len+7)&0xfffffff8 ;
  109. int i;
  110. int p;
  111. BYTE rtext2[1024] = {0};
  112. unsigned char c1,c2,c3;
  113. //处理
  114. p = 0;
  115. for (i = 0; i < len; i++)
  116. {
  117. c3 = rtext[i];
  118. c1 = c3 >> 4;
  119. c2 = c3 & 15;
  120. if (c1 <=9 && c1 >= 0) c1=c1+'0';
  121. if (c1 <=15 && c1 >= 10) c1=c1-10+'A';
  122. if (c2 <=9 && c2 >= 0) c2=c2+'0';
  123. if (c2 <=15 && c2 >= 10) c2=c2-10+'A';
  124. rtext2[p]=c1;
  125. rtext2[p+1]=c2;
  126. p+=2;
  127. }
  128. rtext2[p] = 0;
  129. memcpy(rtext,rtext2,p+1);
  130. }
  131. void CSNRegister::HalfByteToByte(BYTE *rin,BYTE *rout, long len)
  132. {
  133. int i =0;
  134. for (i=0; i<len; i++)
  135. {
  136. if (rin[i]>='0' && rin[i] <= '9') rin[i] = rin[i]-'0';
  137. if (rin[i]>='A' && rin[i] <= 'F') rin[i] = rin[i]-'A'+10;
  138. }
  139. for (i=0;i<len;i+=2)
  140. {
  141. rout[i/2] = rin[i]*16 + rin[i+1];
  142. }
  143. rout[i] = 0;
  144. }
  145. void CSNRegister::encrypt(char *byCode, char *bySn, char *byUseTime, char *byPName, char *byStartTime, char *byEndTime, char *byLeftTime)
  146. {
  147. yDES ydes;
  148. BYTE sztmp[1024] = {0};
  149. // 特征码 1;
  150. //nlen = strlen(byCode);
  151. ydes.DES_Act(sztmp, (BYTE*)byCode, strlen(byCode)+1, (BYTE*)KeyCode[0], 8);
  152. ShowCihper(sztmp,strlen(byCode)+1);
  153. m_strCode = sztmp;
  154. // SN码 2;
  155. //nlen = strlen(bySn);
  156. memset(sztmp,0,1024);
  157. ydes.DES_Act(sztmp, (BYTE*)bySn, strlen(bySn)+1, (BYTE*)KeyCode[1], 8);
  158. ShowCihper(sztmp,strlen(bySn)+1);
  159. m_strSn = sztmp;
  160. // 试用时间 3;
  161. //nlen = strlen(byUseTime);
  162. memset(sztmp,0,1024);
  163. ydes.DES_Act(sztmp, (BYTE*)byUseTime, strlen(byUseTime)+1, (BYTE*)KeyCode[2], 8);
  164. ShowCihper(sztmp,strlen(byUseTime)+1);
  165. m_strUseTime = sztmp;
  166. // 项目名称 4;
  167. //nlen = strlen(byPName);
  168. memset(sztmp,0,1024);
  169. ydes.DES_Act(sztmp, (BYTE*)byPName, strlen(byPName)+1, (BYTE*)KeyCode[3], 8);
  170. ShowCihper(sztmp,strlen(byPName)+1);
  171. m_strPName = sztmp;
  172. // 建成时间 5;
  173. //nlen = strlen(byStartTime);
  174. memset(sztmp,0,1024);
  175. ydes.DES_Act(sztmp, (BYTE*)byStartTime, strlen(byStartTime)+1, (BYTE*)KeyCode[4], 8);
  176. ShowCihper(sztmp,strlen(byStartTime)+1);
  177. m_strStartTime = sztmp;
  178. // 完成时间 6;
  179. //nlen = strlen(byEndTime);
  180. memset(sztmp,0,1024);
  181. ydes.DES_Act(sztmp, (BYTE*)byEndTime, strlen(byEndTime)+1, (BYTE*)KeyCode[5], 8);
  182. ShowCihper(sztmp,strlen(byEndTime)+1);
  183. m_strEndTime = sztmp;
  184. // 剩余时间 7;
  185. //nlen = strlen(byLeftTime);
  186. memset(sztmp,0,1024);
  187. ydes.DES_Act(sztmp, (BYTE*)byLeftTime, strlen(byLeftTime)+1, (BYTE*)KeyCode[6], 8);
  188. ShowCihper(sztmp,strlen(byLeftTime)+1);
  189. m_strLeftTime = sztmp;
  190. }
  191. void CSNRegister::decrypt(char *byCode, char *bySn, char *byUseTime, char *byPName, char *byStartTime, char *byEndTime, char *byLeftTime)
  192. {
  193. }
  194. BOOL CSNRegister::itf_IsCorrectSN(const char *szfile)
  195. {
  196. Load(szfile);
  197. if ( m_strStart != "127" || m_strEndof != "13" || (GetCheck(OPEN) == -1))
  198. return FALSE;
  199. return TRUE;
  200. }
  201. BOOL CSNRegister::itf_read_snfile(const char *szfile)
  202. {
  203. if ( !itf_IsCorrectSN(szfile))
  204. return FALSE;
  205. yDES ydes;
  206. BYTE sztmp[1024] = {0};
  207. BYTE szOut[1024] = {0};
  208. // 1.特征码;
  209. memcpy(sztmp,m_strCode.GetBuffer(),m_strCode.GetLength());
  210. HalfByteToByte(sztmp,szOut,m_strCode.GetLength());
  211. memset(sztmp,0,1024);
  212. ydes.DES_Act(sztmp,szOut,m_strCode.GetLength(),(BYTE*)KeyCode[0],8,DECRYPT);m_strCode.ReleaseBuffer();
  213. m_strCode = "";
  214. m_strCode = sztmp;
  215. // 2.SN码;
  216. //nlen = m_strSn.GetLength();
  217. memset(sztmp,0,1024);
  218. memset(szOut,0,1024);
  219. memcpy(sztmp,m_strSn.GetBuffer(),m_strSn.GetLength());
  220. HalfByteToByte(sztmp,szOut,m_strSn.GetLength());
  221. memset(sztmp,0,1024);
  222. ydes.DES_Act(sztmp,szOut,m_strSn.GetLength(),(BYTE*)KeyCode[1],8,DECRYPT);m_strSn.ReleaseBuffer();
  223. m_strSn = "";
  224. m_strSn = sztmp;
  225. // 3.试用时间;
  226. memset(sztmp,0,1024);
  227. memset(szOut,0,1024);
  228. memcpy(sztmp,m_strUseTime.GetBuffer(),m_strUseTime.GetLength());
  229. HalfByteToByte(sztmp,szOut,m_strUseTime.GetLength());
  230. memset(sztmp,0,1024);
  231. ydes.DES_Act(sztmp,szOut,m_strUseTime.GetLength(),(BYTE*)KeyCode[2],8,DECRYPT);m_strUseTime.ReleaseBuffer();
  232. m_strUseTime = "";
  233. m_strUseTime = sztmp;
  234. // 4.项目名称;
  235. memset(sztmp,0,1024);
  236. memset(szOut,0,1024);
  237. memcpy(sztmp,m_strPName.GetBuffer(),m_strPName.GetLength());
  238. HalfByteToByte(sztmp,szOut,m_strPName.GetLength());
  239. memset(sztmp,0,1024);
  240. ydes.DES_Act(sztmp,szOut,m_strPName.GetLength(),(BYTE*)KeyCode[3],8,DECRYPT);m_strPName.ReleaseBuffer();
  241. m_strPName = "";
  242. m_strPName = sztmp;
  243. // 5.建成时间;
  244. memset(sztmp,0,1024);
  245. memset(szOut,0,1024);
  246. memcpy(sztmp,m_strStartTime.GetBuffer(),m_strStartTime.GetLength());
  247. HalfByteToByte(sztmp,szOut,m_strStartTime.GetLength());
  248. memset(sztmp,0,1024);
  249. ydes.DES_Act(sztmp,szOut,m_strStartTime.GetLength(),(BYTE*)KeyCode[4],8,DECRYPT);m_strStartTime.ReleaseBuffer();
  250. m_strStartTime = "";
  251. m_strStartTime = sztmp;
  252. // 6.完成时间;
  253. memset(sztmp,0,1024);
  254. memset(szOut,0,1024);
  255. memcpy(sztmp,m_strEndTime.GetBuffer(),m_strEndTime.GetLength());
  256. HalfByteToByte(sztmp,szOut,m_strEndTime.GetLength());
  257. memset(sztmp,0,1024);
  258. ydes.DES_Act(sztmp,szOut,m_strEndTime.GetLength(),(BYTE*)KeyCode[5],8,DECRYPT);m_strEndTime.ReleaseBuffer();
  259. m_strEndTime = "";
  260. m_strEndTime = sztmp;
  261. // 7.剩余时间;
  262. memset(sztmp,0,1024);
  263. memset(szOut,0,1024);
  264. memcpy(sztmp,m_strLeftTime.GetBuffer(),m_strLeftTime.GetLength());
  265. HalfByteToByte(sztmp,szOut,m_strLeftTime.GetLength());
  266. memset(sztmp,0,1024);
  267. ydes.DES_Act(sztmp,szOut,m_strLeftTime.GetLength(),(BYTE*)KeyCode[6],8,DECRYPT);m_strLeftTime.ReleaseBuffer();
  268. m_strLeftTime = "";
  269. m_strLeftTime = sztmp;
  270. return TRUE;
  271. }
  272. int CSNRegister::GetCheck(BOOL btype)
  273. {
  274. char szCheck[1024] = {0};
  275. DWORD dwSum = 0;
  276. GetByteSum(dwSum ,m_strCode);
  277. GetByteSum(dwSum ,m_strSn);
  278. GetByteSum(dwSum ,m_strUseTime);
  279. GetByteSum(dwSum ,m_strPName);
  280. GetByteSum(dwSum ,m_strStartTime);
  281. GetByteSum(dwSum ,m_strEndTime);
  282. GetByteSum(dwSum ,m_strLeftTime);
  283. dwSum = ~dwSum;
  284. dwSum++;
  285. itoa(dwSum, szCheck, 16);
  286. for (int i(0); i < 5 ; i++)
  287. {
  288. if( szCheck[i] >= 'a' && szCheck[i] <= 'z' )
  289. {
  290. szCheck[i] = szCheck[i] - 'a' + 'A';
  291. }
  292. }
  293. if (btype == CREATE)
  294. {
  295. m_strCheck = szCheck;
  296. }
  297. else if ( btype == OPEN)
  298. {
  299. if ( m_strCheck.Compare(szCheck) != 0)
  300. return -1;
  301. }
  302. return 0;
  303. }
  304. void CSNRegister::GetByteSum(DWORD &dwSum, CString &strSrc)
  305. {
  306. //int len = strSrc.GetLength();
  307. for (int i = 0; i < strSrc.GetLength(); i++)
  308. {
  309. dwSum += strSrc.GetAt(i);
  310. }
  311. }
  312. BOOL CSNRegister::itf_create_snfile(const char *szfile, char *byCode, char *bySn, char *byUseTime, char *byPName, char *byStartTime, char *byEndTime, char *byLeftTime)
  313. {
  314. m_strStart = "127";
  315. m_strEndof = "13";
  316. encrypt(byCode,bySn,byUseTime,byPName,byStartTime,byEndTime,byLeftTime);
  317. GetCheck(CREATE);
  318. Store(szfile);
  319. return 0;
  320. }
  321. BOOL CSNRegister::itf_modify_snfile(const char *szfile, char *byCode, char *bySn, char *byUseTime, char *byPName, char *byStartTime, char *byEndTime, char *byLeftTime)
  322. {
  323. return 0;
  324. }
  325. void CSNRegister::itf_SavebyLTime(const DWORD &dwLTime,const char *szPath)
  326. {
  327. char szLTime[30] = {0};
  328. itoa(dwLTime, szLTime ,10);
  329. yDES ydes;
  330. BYTE sztmp[250] = {0};
  331. // 剩余时间;
  332. m_strLeftTime = "";
  333. ydes.DES_Act(sztmp, (BYTE*)szLTime, strlen(szLTime)+1, (BYTE*)KeyCode[6], 8);
  334. ShowCihper(sztmp,strlen(szLTime)+1);
  335. m_strLeftTime = sztmp;
  336. GetCheck(CREATE);
  337. Store(szPath);
  338. }
  339. DWORD CSNRegister::itf_returnLeftTime()
  340. {
  341. // 剩余时间 解码;
  342. yDES ydes;
  343. char sztmp[1024] = {0};
  344. BYTE szOut[1024] = {0};
  345. memcpy(sztmp,m_strLeftTime.GetBuffer(),m_strLeftTime.GetLength());
  346. HalfByteToByte((BYTE*)sztmp,szOut,m_strLeftTime.GetLength());
  347. memset(sztmp,0,1024);
  348. ydes.DES_Act((BYTE*)sztmp,szOut,m_strLeftTime.GetLength(),(BYTE*)KeyCode[6],8,DECRYPT);m_strLeftTime.ReleaseBuffer();
  349. return atol(sztmp);
  350. }
  351. char *CSNRegister::itf_returnSnCode()
  352. {
  353. // SN序列号 解码;
  354. yDES ydes;
  355. BYTE szOut[1024] = {0};
  356. memset(m_sztemp,0,1024);
  357. memcpy(m_sztemp,m_strSn.GetBuffer(),m_strSn.GetLength());
  358. HalfByteToByte((BYTE *)m_sztemp,szOut,m_strSn.GetLength());
  359. memset(m_sztemp,0,1024);
  360. ydes.DES_Act((BYTE *)m_sztemp,szOut,m_strSn.GetLength(),(BYTE*)KeyCode[1],8,DECRYPT);m_strSn.ReleaseBuffer();
  361. return m_sztemp;
  362. }
  363. char *CSNRegister::itf_returnCheckCode()
  364. {
  365. // SN序列号 解码;
  366. yDES ydes;
  367. BYTE szOut[1024] = {0};
  368. memset(m_sztemp,0,1024);
  369. memcpy(m_sztemp,m_strSn.GetBuffer(),m_strSn.GetLength());
  370. HalfByteToByte((BYTE *)m_sztemp,szOut,m_strSn.GetLength());
  371. memset(m_sztemp,0,1024);
  372. ydes.DES_Act((BYTE *)m_sztemp,szOut,m_strSn.GetLength(),(BYTE*)KeyCode[1],8,DECRYPT);m_strSn.ReleaseBuffer();
  373. return m_sztemp;
  374. }