MD5Test.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. // MD5Test.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "MD5Test.h"
  5. #include "md5.h"
  6. #include "Base64.h"
  7. #include "StringProcess.h"
  8. #include "AesCipher.h"
  9. #include "lzari.h"
  10. #include "des.h"
  11. #include <stdio.h>
  12. // ----stat头文件;
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. // ----stat头文件;
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #endif
  19. // 唯一的应用程序对象;
  20. INT Test_Bin2Base64_WChar(IN WCHAR* pWString, IN INT nWStrLen, OUT CHAR** pBase64)
  21. {
  22. // 1.计算出Base64的长度,CalcBase64Len的参数必须是字节长度,而非字符数;
  23. INT nBaseLen = CBase64::CalcBase64Len(nWStrLen*sizeof(WCHAR));
  24. // 2.创建Base64缓存;
  25. *pBase64 = new CHAR[nBaseLen + 1];
  26. memset(*pBase64, 0, nBaseLen + 1);
  27. // 3.转化出Base64字符;
  28. CBase64::binToBase64((const unsigned char*)pWString, nWStrLen, *pBase64);
  29. return nBaseLen;
  30. }
  31. INT Test_Bin2Base64_ASCII(IN CHAR* pString, IN INT nStrLen, OUT CHAR** pBase64)
  32. {
  33. // 1.计算出Base64的长度,CalcBase64Len的参数必须是字节长度,而非字符数;
  34. INT nBaseLen = CBase64::CalcBase64Len(nStrLen);
  35. // 2.创建Base64缓存;
  36. *pBase64 = new CHAR[nBaseLen + 1];
  37. memset(*pBase64, 0, nBaseLen + 1);
  38. // 3.转化出Base64字符;
  39. CBase64::binToBase64((const unsigned char*)pString, nStrLen, *pBase64);
  40. return nBaseLen;
  41. }
  42. void Test_Base642Bin_WChar(IN CHAR* pBase64, IN INT nBase64Len, OUT WCHAR* pOutBin, IN INT nOutLen )
  43. {
  44. // 1.计算出字节长度;
  45. INT nByteLen = CBase64::CalcBinLen(nBase64Len);
  46. // 2.创建Byte缓存区;
  47. BYTE *pBin = new BYTE[nByteLen];
  48. memset(pBin, 0, nByteLen);
  49. // 3.转化成字节;
  50. CBase64::base64ToBin(pBase64, pBin, nByteLen);
  51. if ( pOutBin == NULL )
  52. {
  53. if ( pBin )
  54. delete []pBin;
  55. pBin = NULL;
  56. }
  57. else
  58. {
  59. memcpy(pOutBin, pBin, nByteLen);
  60. if ( pBin )
  61. delete []pBin;
  62. pBin = NULL;
  63. }
  64. }
  65. INT Test_Base642Bin_ASCII(IN CHAR* pBase64, IN INT nBase64Len, OUT CHAR* pOutBin, IN INT nOutLen )
  66. {
  67. // 1.计算出字节长度;
  68. INT nByteLen = CBase64::CalcBinLen(nBase64Len);
  69. // 2.创建Byte缓存区;
  70. BYTE *pBin = new BYTE[nByteLen];
  71. memset(pBin, 0, nByteLen);
  72. // 3.转化成字节;
  73. CBase64::base64ToBin(pBase64, pBin, nByteLen);
  74. if ( pOutBin == NULL )
  75. {
  76. if ( pBin )
  77. delete []pBin;
  78. pBin = NULL;
  79. }
  80. else
  81. {
  82. memcpy(pOutBin, pBin, nByteLen);
  83. if ( pBin )
  84. delete []pBin;
  85. pBin = NULL;
  86. }
  87. return nByteLen;
  88. }
  89. // 从指定的文件读取数据,并生成出Base64对应的文本文件;
  90. void Test_CreateBase64File(IN LPCTSTR lpBinFile, IN LPCTSTR lpBase64File)
  91. {
  92. CFile cf;
  93. if ( cf.Open(lpBinFile, CFile::modeRead) )
  94. {
  95. ULONGLONG uLen = cf.GetLength();
  96. BYTE *pData = new BYTE[uLen];
  97. memset(pData, 0, uLen);
  98. cf.Read(pData, uLen);
  99. // 1.计算出base 64的长度;
  100. ULONGLONG uBaseLen = CBase64::CalcBase64Len(uLen);
  101. // 创建Base64缓存;
  102. CHAR *pBase64 = new CHAR[uBaseLen + 1];
  103. memset(pBase64, 0, uBaseLen + 1);
  104. CBase64::binToBase64(pData, uLen, pBase64);
  105. CFile cf64;
  106. if ( cf64.Open(lpBase64File, CFile::modeCreate|CFile::modeWrite) )
  107. {
  108. cf64.Write(pBase64, uBaseLen + 1);
  109. if ( pBase64 )
  110. delete []pBase64;
  111. pBase64 = NULL;
  112. cf64.Close();
  113. }
  114. if ( pData )
  115. delete []pData;
  116. pData = NULL;
  117. cf.Close();
  118. }
  119. }
  120. // 将Base64内容的文本文件恢复回正常的文件;
  121. void Test_CreateBindFile(IN LPCTSTR lpBase64File, IN LPCTSTR lpBinFile )
  122. {
  123. CFile cf64;
  124. if ( cf64.Open(lpBase64File, CFile::modeRead) )
  125. {
  126. ULONGLONG uLen = cf64.GetLength();
  127. BYTE *pBase64Data = new BYTE[uLen];
  128. memset(pBase64Data, 0, uLen);
  129. cf64.Read(pBase64Data, uLen);
  130. // 1.计算出二进制字节的长度;
  131. ULONGLONG uBinLen = CBase64::CalcBinLen(uLen);
  132. // 创建binary缓存;
  133. BYTE *pByte = new BYTE[uBinLen];
  134. memset(pByte, 0, uBinLen);
  135. uBinLen = CBase64::base64ToBin((CHAR*)pBase64Data, pByte, uBinLen);
  136. pByte[uBinLen] = '\0'; // 必须操作这一步,否则数据末尾不会正常截断;
  137. CFile cfbin;
  138. if ( cfbin.Open(lpBinFile, CFile::modeCreate|CFile::modeWrite) )
  139. {
  140. cfbin.Write(pByte, uBinLen);
  141. if ( pByte )
  142. delete []pByte;
  143. pByte = NULL;
  144. cfbin.Close();
  145. }
  146. if ( pBase64Data )
  147. delete []pBase64Data;
  148. pBase64Data = NULL;
  149. cf64.Close();
  150. }
  151. }
  152. //////////////////////////////////////////////////////////////////////////
  153. // 将文件使用Aes计算器模式加密,并生成加密文件;
  154. void Test_Aes_Encryptfile_CTR(IN LPCTSTR lpFile, IN LPCCH lpKey, IN LPCTSTR lpAesFile)
  155. {
  156. // 1.定义数据;
  157. BYTE *pFileBuff = NULL; // 文件缓存指针;
  158. ULONGLONG uFileLen = 0; // 文件长度;
  159. BYTE *pOutBuff = NULL; // 加密后的文件数据;
  160. ULONGLONG uOutLen = 0; // 文件数据加密后的长度;
  161. CHAR szKey[MAX_PATH] = {0}; // 加密使用的密钥;
  162. CHAR szCounter[16] = {0}; // 计算器模式下的计算值;
  163. // 设置计算器计算值;
  164. szCounter[5] = -113;
  165. // 获取文件数据;
  166. CFile cf;
  167. if ( cf.Open(lpFile, CFile::modeRead) )
  168. {
  169. uFileLen = cf.GetLength();
  170. pFileBuff = new BYTE[uFileLen];
  171. memset(pFileBuff, 0, uFileLen);
  172. cf.Read(pFileBuff, uFileLen);
  173. cf.Close();
  174. }
  175. // 3.开始加密;
  176. AesCipher AesCrypto;
  177. AesCrypto.makeRoundKey(lpKey, strlen(lpKey));
  178. AesCrypto.setCounter(szCounter);
  179. // 计算出文件加密后的长度;
  180. uOutLen = AesCrypto.calculateCipherLen(uFileLen);
  181. pOutBuff = new BYTE[uOutLen];
  182. memset(pOutBuff, 0, uOutLen);
  183. // 加密文件数据成字符串;
  184. AesCrypto.encryptString((CHAR*)pFileBuff, (CHAR*)pOutBuff, uFileLen, AesCipher::CTR);
  185. // 释放文件数据资源;
  186. if ( pFileBuff )
  187. delete []pFileBuff;
  188. pFileBuff = NULL;
  189. // 将加密后的数据保存到文件中;
  190. CFile cfAes;
  191. if ( cfAes.Open(lpAesFile, CFile::modeCreate|CFile::modeWrite) )
  192. {
  193. cfAes.Write(pOutBuff, uOutLen);
  194. cfAes.Close();
  195. }
  196. // 释放加密后的文件数据;
  197. if ( pOutBuff )
  198. delete []pOutBuff;
  199. pOutBuff = NULL;
  200. }
  201. void Test_Aes_Decryptfile_CTR(IN LPCTSTR lpFile, IN LPCCH lpKey, IN LPCTSTR lpAesFile)
  202. {
  203. // 1.定义数据;
  204. BYTE *pFileBuff = NULL; // 文件缓存指针;
  205. ULONGLONG uFileLen = 0; // 文件长度;
  206. BYTE *pOutBuff = NULL; // 解密后的文件数据;
  207. ULONGLONG uOutLen = 0; // 文件数据解密后的长度;
  208. CHAR szKey[MAX_PATH] = {0}; // 加密使用的密钥;
  209. CHAR szCounter[16] = {0}; // 计算器模式下的计算值;
  210. // 设置计算器计算值;
  211. szCounter[5] = -113;
  212. // 获取文件数据;
  213. CFile cf;
  214. if ( cf.Open(lpFile, CFile::modeRead) )
  215. {
  216. uFileLen = cf.GetLength();
  217. pFileBuff = new BYTE[uFileLen];
  218. memset(pFileBuff, 0, uFileLen);
  219. cf.Read(pFileBuff, uFileLen);
  220. cf.Close();
  221. }
  222. // 3.开始解密;
  223. AesCipher AesCrypto;
  224. AesCrypto.makeRoundKey(lpKey, strlen(lpKey));
  225. AesCrypto.setCounter(szCounter);
  226. // 初始化输出数据;
  227. pOutBuff = new BYTE[uFileLen + 1];
  228. memset(pOutBuff, 0, uFileLen + 1);
  229. // 解密文件数据成字符串,并返回解密后的数据长度;
  230. uOutLen = AesCrypto.decryptString((CHAR*)pFileBuff, (CHAR*)pOutBuff, uFileLen, AesCipher::CTR);
  231. pOutBuff[uOutLen] = '\0'; // 必须操作这一步,否则数据末尾不会正常截断;
  232. // 释放文件数据资源;
  233. if ( pFileBuff )
  234. delete []pFileBuff;
  235. pFileBuff = NULL;
  236. // 将加密后的数据保存到文件中;
  237. CFile cfAes;
  238. if ( cfAes.Open(lpAesFile, CFile::modeCreate|CFile::modeWrite) )
  239. {
  240. cfAes.Write(pOutBuff, uOutLen);
  241. cfAes.Close();
  242. }
  243. // 释放加密后的文件数据;
  244. if ( pOutBuff )
  245. delete []pOutBuff;
  246. pOutBuff = NULL;
  247. }
  248. // ECB, CBC, CFB, OFB工作模式(明文比密文长度要短很多);
  249. void Test_Aes_Decryptfile_Others(IN LPCCH lpPlainText, IN LPCCH lpKey, IN LPCCH lpCounter)
  250. {
  251. // 参数有效性判断;
  252. if ( lpPlainText == NULL || lpKey == NULL )
  253. return;
  254. // 密钥是否符合长度要求;
  255. INT nKeyLen = strlen(lpKey);
  256. if ( nKeyLen != 16 && nKeyLen != 24 && nKeyLen != 32 )
  257. return;
  258. // 明文长度;
  259. INT nPlainTextLen = strlen(lpPlainText);
  260. AesCipher AesCrypt;
  261. AesCrypt.makeRoundKey(lpKey);
  262. if ( lpCounter )
  263. AesCrypt.setCounter(lpCounter);
  264. // 输出的密文;
  265. CHAR* pOutCipherText = NULL;
  266. // 计算密文长度;
  267. INT nCipherTextLen = AesCrypt.calculateCipherLen(strlen(lpPlainText));
  268. // 创建密文堆;
  269. pOutCipherText = new CHAR[nCipherTextLen+1];
  270. memset(pOutCipherText, 0, nCipherTextLen + 1);
  271. // 解密后的明文;
  272. CHAR* pPlainText = NULL;
  273. pPlainText = new CHAR[nCipherTextLen+1];
  274. memset(pPlainText, 0 ,nCipherTextLen+1);
  275. // ECB mode;
  276. printf("\n\nECB*****************************************************\n");
  277. AesCrypt.encryptString(lpPlainText, pOutCipherText, nPlainTextLen, AesCipher::ECB);
  278. //pOutCipherText[nCipherTextLen] = '\0';
  279. printf("ECB Encrypt mode \n %s \n %s \n\n", lpPlainText, pOutCipherText);
  280. nPlainTextLen = AesCrypt.decryptString(pOutCipherText, pPlainText, nCipherTextLen, AesCipher::ECB);
  281. pPlainText[nPlainTextLen] = '\0';
  282. printf("ECB Decrypt mode \n %s \n %s \n\n", pOutCipherText, pPlainText);
  283. // CBC mode;
  284. printf("\n\nCBC*****************************************************\n");
  285. memset(pOutCipherText, 0, nCipherTextLen + 1);
  286. memset(pPlainText, 0 ,nCipherTextLen+1);
  287. // 为CBC、CFB、OFB三模式设置初始向量;
  288. AesCrypt.setIV("Cache.0123456789");
  289. AesCrypt.encryptString(lpPlainText, pOutCipherText, nPlainTextLen, AesCipher::CBC);
  290. //pOutCipherText[nCipherTextLen] = '\0';
  291. printf("CBC Encrypt mode \n %s \n %s \n\n", lpPlainText, pOutCipherText);
  292. nPlainTextLen = AesCrypt.decryptString(pOutCipherText, pPlainText, nCipherTextLen, AesCipher::CBC);
  293. pPlainText[nPlainTextLen] = '\0';
  294. printf("CBC Decrypt mode \n %s \n %s \n\n", pOutCipherText, pPlainText);
  295. // CFB mode;
  296. printf("\n\nCFB*****************************************************\n");
  297. memset(pOutCipherText, 0, nCipherTextLen + 1);
  298. memset(pPlainText, 0 ,nCipherTextLen+1);
  299. AesCrypt.encryptString(lpPlainText, pOutCipherText, nPlainTextLen, AesCipher::CFB);
  300. //pOutCipherText[nCipherTextLen] = '\0';
  301. printf("CFB Encrypt mode \n %s \n %s \n\n", lpPlainText, pOutCipherText);
  302. nPlainTextLen = AesCrypt.decryptString(pOutCipherText, pPlainText, nCipherTextLen, AesCipher::CFB);
  303. pPlainText[nPlainTextLen] = '\0';
  304. printf("CFB Decrypt mode \n %s \n %s \n\n", pOutCipherText, pPlainText);
  305. // OFB mode;
  306. printf("\n\nOFB*****************************************************\n");
  307. memset(pOutCipherText, 0, nCipherTextLen + 1);
  308. memset(pPlainText, 0 ,nCipherTextLen+1);
  309. AesCrypt.encryptString(lpPlainText, pOutCipherText, nPlainTextLen, AesCipher::OFB);
  310. //pOutCipherText[nCipherTextLen] = '\0';
  311. printf("OFB Encrypt mode \n %s \n %s \n\n", lpPlainText, pOutCipherText);
  312. nPlainTextLen = AesCrypt.decryptString(pOutCipherText, pPlainText, nCipherTextLen, AesCipher::OFB);
  313. pPlainText[nPlainTextLen] = '\0';
  314. printf("OFB Decrypt mode \n %s \n %s \n\n", pOutCipherText, pPlainText);
  315. }
  316. //
  317. //void DES()
  318. //{
  319. // int i, j;
  320. // unsigned char buf[1024];
  321. // unsigned long tsc;
  322. // unsigned char tmp[64];
  323. //
  324. // CHAR* pBase64 = NULL;
  325. // //CHAR* pBinary = NULL;
  326. // BYTE szVI[9] = "ABCD6789";
  327. // CHAR szBinary[MAX_PATH] = {0};
  328. //
  329. // des_context des;
  330. // des3_context des3;
  331. //
  332. // //密钥是01234567, 明文zth DES
  333. // memset(tmp, 0, sizeof(tmp));
  334. // strcpy((char*)tmp, "01234567");
  335. // memset(buf, 0, sizeof(buf));
  336. // strcpy((char*)buf, "zth");
  337. //
  338. // // 设置加密的des结构体值;
  339. // des_setkey_enc( &des, tmp);
  340. //
  341. // printf("pwd=%s, txt=%s\n", tmp, buf);
  342. // // 加密;
  343. // des_crypt_cbc(&des, DES_ENCRYPT, 8, szVI, buf, buf );
  344. // //Test_Base642Bin_ASCII();
  345. //
  346. // Test_Bin2Base64_ASCII((char*)buf, strlen((char*)buf), &pBase64);
  347. // Test_Base642Bin_ASCII(pBase64, strlen(pBase64), szBinary, MAX_PATH);
  348. // /*printf("encrypt txt= \n %s\n%s\n%s\n", tmp, buf, buf);
  349. // for(i=0; i<8; i++) printf("%x ", buf[i]);
  350. // printf("\n");*/
  351. //
  352. // // 解密;
  353. // memset(szVI, 0, sizeof(szVI));
  354. // strcpy((char*)szVI, "ABCD6789");
  355. // // 设置解密des结构体;
  356. // des_setkey_dec(&des, tmp);
  357. // // 解密;
  358. // des_crypt_cbc(&des, DES_DECRYPT, 8, szVI, (BYTE*)szBinary, buf );
  359. // /*printf("decrypt txt=");
  360. // for(i=0; i<8; i++) printf("%x ", buf[i]);
  361. // printf("\n\n");*/
  362. //
  363. // //密钥是01234567abcdefgh, 明文zth DES3
  364. // memset(tmp, 0, sizeof(tmp)); strcpy((char*)tmp, "01234567abcdefgh");
  365. // memset(buf, 0, sizeof(buf)); strcpy((char*)buf, "zth");
  366. // des3_set3key_enc( &des3, tmp);
  367. // printf("DES3 pwd=%s, txt=%s\n", tmp, buf);
  368. // des3_crypt_cbc(&des3, DES_ENCRYPT, 8, tmp, buf, buf );
  369. // printf("encrypt txt=", tmp, buf, buf);
  370. // for(i=0; i<8; i++) printf("%x ", buf[i]);
  371. // printf("\n");
  372. //
  373. // memset(tmp, 0, sizeof(tmp)); strcpy((char*)tmp, "01234567abcdefgh");
  374. // des3_set3key_dec(&des3, tmp);
  375. // des3_crypt_cbc(&des3, DES_DECRYPT, 8, tmp, buf, buf );
  376. // printf("decrypt txt=");
  377. // for(i=0; i<8; i++) printf("%x ", buf[i]);
  378. // printf("\n");
  379. //
  380. // system("pause");
  381. //}
  382. void DES_EncryptFile(IN LPCTSTR lpFile, IN LPBYTE lpKey, IN LPBYTE lpVI, IN LPCTSTR lpEncryFile)
  383. {
  384. if ( lpFile == NULL )
  385. return;
  386. if ( lpKey == NULL || lpVI == NULL )
  387. return;
  388. INT nKeyLen = strlen((char*)lpKey);
  389. if (nKeyLen != 8 )
  390. return;
  391. INT nVILen = strlen((char*)lpVI);
  392. if ( nVILen != 8 )
  393. return;
  394. CFile cf;
  395. BYTE *pFileData = NULL;
  396. BYTE *pOutData = NULL;
  397. INT nPadLen = 0;
  398. ULONGLONG nFileLen = 0;
  399. ULONGLONG nOutLen = 0;
  400. if ( cf.Open(lpFile, CFile::modeRead) )
  401. {
  402. nFileLen = cf.GetLength();
  403. nOutLen = des_enc_len(nFileLen);
  404. pFileData = new BYTE[nOutLen];
  405. memset(pFileData, 0, nOutLen);
  406. cf.Read(pFileData,nFileLen);
  407. cf.Close();
  408. }
  409. pOutData = new BYTE[nOutLen + 1];
  410. memset(pOutData, 0, nOutLen + 1);
  411. des_context des;
  412. if ( des_key_check_key_parity(lpKey) == 1)
  413. {
  414. printf("key was not correct\n");
  415. des_key_set_parity(lpKey);
  416. if ( des_key_check_key_parity(lpKey) == 0 )
  417. {
  418. des_setkey_enc(&des, lpKey);
  419. }
  420. }
  421. else
  422. des_setkey_enc(&des, lpKey);
  423. des_crypt_cbc(&des, DES_ENCRYPT, nOutLen, lpVI, pFileData, pOutData);
  424. CFile cfo;
  425. if ( cfo.Open(lpEncryFile, CFile::modeCreate|CFile::modeWrite) )
  426. {
  427. cfo.Write(pOutData, nOutLen);
  428. cfo.Close();
  429. }
  430. if ( pOutData )
  431. delete []pOutData;
  432. }
  433. void DES_DecryptFile(IN LPCTSTR lpFile, IN LPBYTE lpKey, IN LPBYTE lpVI, IN LPCTSTR lpDecryFile)
  434. {
  435. if ( lpFile == NULL )
  436. return;
  437. if ( lpKey == NULL || lpVI == NULL )
  438. return;
  439. INT nKeyLen = strlen((char*)lpKey);
  440. if (nKeyLen != 8 )
  441. return;
  442. INT nVILen = strlen((char*)lpVI);
  443. if ( nVILen != 8 )
  444. return;
  445. CFile cf;
  446. BYTE *pFileData = NULL;
  447. BYTE *pOutData = NULL;
  448. UINT64 nFileLen = 0;
  449. if ( cf.Open(lpFile, CFile::modeRead) )
  450. {
  451. nFileLen = cf.GetLength();
  452. pFileData = new BYTE[nFileLen];
  453. memset(pFileData, 0, nFileLen);
  454. cf.Read(pFileData,nFileLen);
  455. cf.Close();
  456. }
  457. // nFileLen 是否是8的整数倍,不是则加满;
  458. UINT64 nOutLen = nFileLen;
  459. pOutData = new BYTE[nOutLen + 1];
  460. memset(pOutData, 0, nOutLen + 1);
  461. des_context des;
  462. if ( des_key_check_key_parity(lpKey) == 1)
  463. {
  464. printf("key was not correct\n");
  465. des_key_set_parity(lpKey);
  466. if ( des_key_check_key_parity(lpKey) == 0 )
  467. {
  468. des_setkey_dec(&des, lpKey);
  469. }
  470. }
  471. else
  472. des_setkey_dec(&des, lpKey);
  473. des_crypt_cbc(&des, DES_DECRYPT, nOutLen, lpVI, pFileData, pOutData);
  474. // 解密后,要去除填充的字符;
  475. /*while ( pOutData[nOutLen-1] == 0x00 )
  476. {
  477. nOutLen--;
  478. }*/
  479. nOutLen = des_dec_len(pOutData,nOutLen);
  480. CFile cfo;
  481. if ( cfo.Open(lpDecryFile, CFile::modeCreate|CFile::modeWrite) )
  482. {
  483. cfo.Write(pOutData, nOutLen);
  484. cfo.Close();
  485. }
  486. if ( pOutData )
  487. delete []pOutData;
  488. }
  489. CWinApp theApp;
  490. using namespace std;
  491. //#include "md5和sha1.h"
  492. #include "sha1.h"
  493. #include <list>
  494. using namespace std;
  495. int cmp(const void* a, const void* b){
  496. return *(int*)a - *(int*)b;
  497. }
  498. inline void println(int arr[], int len){
  499. for(int i = 0; i < len; i++){
  500. cout << arr[i] << " ";
  501. }
  502. //cout << endl;
  503. }
  504. inline void reverse(int arr[], int left, int right){
  505. while(right >= left){
  506. swap(arr[left++], arr[right--]);
  507. }
  508. }
  509. void full_permutation(int arr[], int len){
  510. qsort(arr, len, sizeof(int), cmp);
  511. println(arr, len);
  512. while(true){
  513. int j = len-1;
  514. int t = len;
  515. while(j >= 0 && arr[--j] >= arr[j+1]) ;
  516. if(j >= 0){
  517. while(arr[--t] <= arr[j]) ;
  518. swap(arr[t], arr[j]);
  519. reverse(arr, j+1, len-1);
  520. println(arr, len);
  521. }
  522. else{
  523. break;
  524. }
  525. }
  526. }
  527. // 冒泡排序;
  528. void BubbleSort(string *s, int count)
  529. {
  530. string temp;
  531. for (int i = 1; i < count; i++)
  532. {
  533. for (int j = count - 1; j >= i; j--)
  534. {
  535. if (s[j].compare(s[j - 1]) < 0)
  536. {
  537. temp = s[j - 1];
  538. s[j - 1] = s[j];
  539. s[j] = temp;
  540. }
  541. }
  542. }
  543. }
  544. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  545. {
  546. int nRetCode = 0;
  547. // 初始化 MFC 并在失败时显示错误
  548. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  549. {
  550. // TODO: 更改错误代码以符合您的需要
  551. _tprintf(_T("错误: MFC 初始化失败\n"));
  552. nRetCode = 1;
  553. }
  554. else
  555. {
  556. #if 0
  557. char sz[3] = {0};
  558. BYTE by = 0xff;
  559. ByteTurn2HexChar(by, sz);
  560. string ss[3] = {"J8UcPVTTxPqZYRw2ZmVEznX6j","201607111759111","lyfz.net"};
  561. BubbleSort(ss,3);
  562. string sAll;
  563. for ( int i = 0; i < 3; i++ )
  564. {
  565. sAll.append(ss[i]);
  566. }
  567. BYTE szSource[560] = "lyfz.net";
  568. BYTE byResult[21] = {0};
  569. BYTE byHexResult[250] = {0};
  570. memset(szSource, 0, 560);
  571. memcpy(szSource, sAll.c_str(), sAll.size());
  572. sha1(szSource, sAll.size(), byResult);
  573. ByteToTwoByte(byResult, 20, byHexResult);
  574. sha1Hex(szSource, sAll.size(), byHexResult);
  575. sha1(szSource, strlen((char*)szSource), byResult);
  576. sha1Hex(szSource, strlen((char*)szSource), byHexResult);
  577. printf("原始加密:%s\n 16进制加密:%s\n", byResult, byHexResult);
  578. system("pause");
  579. #endif
  580. #if 1
  581. CONST TCHAR* pMD5 = NULL;
  582. // TODO: 在此处为应用程序的行为编写代码。
  583. printf("示例:MD5加密'订单1234', 结果如下:\n");
  584. // 读取文件大小;
  585. CStringA strpath = "D:\\0.dat";
  586. struct stat statbuf;
  587. stat(strpath.GetString(), &statbuf);
  588. BYTE *pFileData = new BYTE[statbuf.st_size + 1];
  589. memset(pFileData, 0, statbuf.st_size);
  590. // 读取文件内容;
  591. #if 0
  592. FILE *f = NULL;
  593. fopen_s(&f,strpath, "r+");
  594. if ( f != NULL )
  595. {
  596. fread_s(pFileData, statbuf.st_size, sizeof(BYTE), statbuf.st_size, f);
  597. fclose(f);
  598. }
  599. else
  600. {
  601. return -1;
  602. }
  603. #else
  604. CFile cf;
  605. if ( cf.Open(strpath, CFile::modeRead) )
  606. {
  607. //DWORD dwlen = cf.GetLength();
  608. //if ( dwlen == statbuf.st_size )
  609. //{
  610. // TRACE("dfdfdfd");
  611. //}
  612. cf.Read(pFileData, statbuf.st_size);
  613. }
  614. #endif
  615. CHAR *pUTF8 = new CHAR[statbuf.st_size+1];
  616. StringProcess::ascii_to_utf8((const char*)pFileData, pUTF8);
  617. CMD5 mdd5(pFileData, statbuf.st_size);
  618. pMD5 = mdd5.GetMD5Digest();
  619. mdd5.SetBYTEText(reinterpret_cast<BYTE*>(pUTF8), strlen(pUTF8));
  620. pMD5 = mdd5.GetMD5Digest();
  621. // 默认GB2321的md5值;
  622. CMD5 md5((const BYTE*)("AT中国123"), strlen("AT中国123"));
  623. pMD5 = md5.GetMD5Digest();
  624. _tprintf(_T("GB2321\t\t:%s\n"), pMD5);
  625. // 获取utf8的md5值;
  626. CHAR szUtf8[MAX_PATH] = {0};
  627. CHAR szbuffer[MAX_PATH] = "AT中国123";
  628. StringProcess::ascii_to_utf8(szbuffer,szUtf8);
  629. md5.SetBYTEText(reinterpret_cast<BYTE*>(szUtf8), strlen(szUtf8));
  630. pMD5 = md5.GetMD5Digest();
  631. _tprintf(_T("utf8\t\t:%s\n"), pMD5);
  632. // 获取unicode的md5值;
  633. WCHAR szUnicode[MAX_PATH] = L"AT中国123";
  634. BYTE szUN[MAX_PATH] = {0};
  635. INT nBytes = sizeof(WCHAR)*wcslen(szUnicode);
  636. memcpy(szUN, szUnicode, nBytes);
  637. //INT nChars = strlen((char*)szUN);
  638. md5.SetBYTEText(reinterpret_cast<BYTE*>(szUN), nBytes);
  639. pMD5 = md5.GetMD5Digest();
  640. _tprintf(_T("unicode\t\t:%s\n"), pMD5);
  641. // 获取ascii的md5值;
  642. CHAR szAscii[MAX_PATH] = {0};
  643. StringProcess::utf8_to_ascii(szUtf8,szAscii);
  644. md5.SetBYTEText(reinterpret_cast<BYTE*>(szAscii), strlen(szAscii));
  645. pMD5 = md5.GetMD5Digest();
  646. _tprintf(_T("ascii\t\t:%s\n"), pMD5);
  647. #endif
  648. #if 0 // 测试Base64类的有效性;
  649. Test_CreateBase64File(_T("D:\\dindansp2.txt"), _T("D:\\base64.txt"));
  650. Test_CreateBindFile(_T("D:\\base64.txt"), _T("D:\\binary.txt"));
  651. WCHAR szString[MAX_PATH] = L"11寸水晶";
  652. CHAR *pBase64 = NULL;
  653. INT nBase64Len = Test_Bin2Base64_WChar(szString, _tcslen(szString)*sizeof(WCHAR), &pBase64);
  654. WCHAR szWBase64[MAX_PATH] = {0};
  655. Test_Base642Bin_WChar(pBase64, nBase64Len, szWBase64, MAX_PATH);
  656. if ( pBase64 )
  657. delete []pBase64;
  658. pBase64 = NULL;
  659. #endif
  660. #if 0
  661. Test_Aes_Encryptfile_CTR(_T("F:\\刻录驱动.rar"), "A012369DFEROE.LKDJFOI*&^%$!@_+~!", _T("D:\\Aes_ke.txt"));
  662. Test_CreateBase64File(_T("D:\\Aes_ke.txt"), _T("D:\\base64_ke.txt"));
  663. Test_CreateBindFile(_T("D:\\base64_ke.txt"), _T("D:\\binary_ke.txt"));
  664. Test_Aes_Decryptfile_CTR(_T("D:\\binary_ke.txt"), "A012369DFEROE.LKDJFOI*&^%$!@_+~!", _T("D:\\刻录驱动.rar"));
  665. #endif
  666. #if 0
  667. // 方八水立方册;ecnrptyIyFsVC3Y65L3qv+jvdrH
  668. CHAR szExample[MAX_PATH] = "方10公主梦幻册";
  669. CHAR szUTF8[MAX_PATH] = {0};
  670. StringProcess::ascii_to_utf8(szExample,szUTF8);
  671. CHAR szBase64[MAX_PATH] = "pagFVaDD8KfR24+mq4k=";
  672. CHAR szBinary[MAX_PATH] = {0};
  673. Test_Base642Bin_ASCII(szBase64, strlen(szBase64), szBinary, MAX_PATH);
  674. StringProcess::utf8_to_ascii(szUTF8, szExample);
  675. StringProcess::utf8_to_ascii(szBinary, szExample);
  676. system("pause");
  677. #endif
  678. #if 0
  679. CHAR szCounter[16] = {0};
  680. szCounter[7] = 113;
  681. szCounter[15] = -25;
  682. Test_Aes_Decryptfile_Others("加拿大记者借“人权”问题发难 王毅怒斥:傲慢与偏见", "ABCD0123456789EF", NULL);
  683. printf("\n\n\n使用计数器");// 证实计数器只对CTR模式有用;
  684. Test_Aes_Decryptfile_Others("加拿大记者借“人权”问题发难 王毅怒斥:傲慢与偏见", "ABCD0123456789EF", szCounter);
  685. #endif
  686. #if 0
  687. //DES();
  688. BYTE szKey[MAX_PATH] = "lyfz.net";
  689. BYTE szVI[MAX_PATH] = "WorkbyIT";
  690. DES_EncryptFile(_T("E:\\dindansp.txt"), szKey, szVI, _T("E:\\out.txt"));
  691. sprintf((char*)szVI, "%s", "WorkbyIT");
  692. DES_DecryptFile(_T("E:\\out.txt"), szKey, szVI, _T("E:\\Decout.txt"));
  693. #endif
  694. #if 1
  695. // N/jZ0RbjHYc=
  696. BYTE szKey[MAX_PATH] = "mygz/ndu";
  697. BYTE szVI[MAX_PATH] = "WorkbyIT";
  698. CHAR szBinary[MAX_PATH] = {0};
  699. CHAR szBase64[MAX_PATH] = "tO/cYVBbZ3vwZAJ8WQAgdQWLGyycqETbJDc7xB0xZKgK0Ps2noivrY/jhKTdevxI";//ALRJrTZYzN0
  700. // 解密回二进制;
  701. INT nReal = Test_Base642Bin_ASCII(szBase64, strlen(szBase64), szBinary, MAX_PATH);
  702. // 解密;
  703. des_context des;
  704. if ( des_key_check_key_parity(szKey) == 1)
  705. {
  706. printf("key was not correct\n");
  707. des_key_set_parity(szKey);
  708. if ( des_key_check_key_parity(szKey) == 0 )
  709. {
  710. des_setkey_dec(&des, szKey);
  711. }
  712. }
  713. else
  714. des_setkey_dec(&des, szKey);
  715. CHAR szDesDec[MAX_PATH] = {0};
  716. INT nInputlen = strlen(szBinary);
  717. des_crypt_cbc(&des, DES_DECRYPT, nReal, szVI, (const unsigned char*)szBinary, (unsigned char*)szDesDec);
  718. // 解密后,要去除填充的字符;
  719. while ( szDesDec[nInputlen-1] == 0x00 )
  720. {
  721. nInputlen--;
  722. }
  723. #endif
  724. #if 0
  725. // N/jZ0RbjHYc=
  726. BYTE szKey[MAX_PATH] = "mygz/ndu";
  727. BYTE szVI[MAX_PATH] = "WorkbyIT";
  728. CFile cf;
  729. if ( cf.Open(_T("input.txt"), CFile::modeRead) )
  730. {
  731. DWORD dwlength = cf.GetLength();
  732. BYTE *pData = new BYTE[dwlength +1];
  733. memset(pData, 0, dwlength + 1);
  734. cf.Read(pData, dwlength);
  735. // 解密回二进制;
  736. INT nByteLen = CBase64::CalcBinLen(dwlength);
  737. BYTE *pBinary = new BYTE[nByteLen+1];
  738. memset(pBinary, 0, nByteLen+1);
  739. Test_Base642Bin_ASCII((char*)pData, dwlength, (char*)pBinary, MAX_PATH);
  740. cf.Close();
  741. CFile ccf;
  742. if ( ccf.Open(_T("output.txt"), CFile::modeCreate|CFile::modeReadWrite) )
  743. {
  744. ccf.Write(pBinary, nByteLen);
  745. ccf.Close();
  746. }
  747. }
  748. //DES_DecryptFile(_T("output.txt"), szKey, szVI, _T("decout.txt"));
  749. sprintf((char*)szVI, "%s", "WorkbyIT");
  750. DES_DecryptFile(_T("output.txt"), szKey, szVI, _T("decout.txt"));
  751. #endif
  752. system("pause");
  753. }
  754. return nRetCode;
  755. }