123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914 |
- // MD5Test.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include "MD5Test.h"
- #include "md5.h"
- #include "Base64.h"
- #include "StringProcess.h"
- #include "AesCipher.h"
- #include "lzari.h"
- #include "des.h"
- #include <stdio.h>
- // ----stat头文件;
- #include <sys/types.h>
- #include <sys/stat.h>
- // ----stat头文件;
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // 唯一的应用程序对象;
- INT Test_Bin2Base64_WChar(IN WCHAR* pWString, IN INT nWStrLen, OUT CHAR** pBase64)
- {
- // 1.计算出Base64的长度,CalcBase64Len的参数必须是字节长度,而非字符数;
- INT nBaseLen = CBase64::CalcBase64Len(nWStrLen*sizeof(WCHAR));
- // 2.创建Base64缓存;
- *pBase64 = new CHAR[nBaseLen + 1];
- memset(*pBase64, 0, nBaseLen + 1);
- // 3.转化出Base64字符;
- CBase64::binToBase64((const unsigned char*)pWString, nWStrLen, *pBase64);
- return nBaseLen;
- }
- INT Test_Bin2Base64_ASCII(IN CHAR* pString, IN INT nStrLen, OUT CHAR** pBase64)
- {
- // 1.计算出Base64的长度,CalcBase64Len的参数必须是字节长度,而非字符数;
- INT nBaseLen = CBase64::CalcBase64Len(nStrLen);
- // 2.创建Base64缓存;
- *pBase64 = new CHAR[nBaseLen + 1];
- memset(*pBase64, 0, nBaseLen + 1);
- // 3.转化出Base64字符;
- CBase64::binToBase64((const unsigned char*)pString, nStrLen, *pBase64);
- return nBaseLen;
- }
- void Test_Base642Bin_WChar(IN CHAR* pBase64, IN INT nBase64Len, OUT WCHAR* pOutBin, IN INT nOutLen )
- {
- // 1.计算出字节长度;
- INT nByteLen = CBase64::CalcBinLen(nBase64Len);
- // 2.创建Byte缓存区;
- BYTE *pBin = new BYTE[nByteLen];
- memset(pBin, 0, nByteLen);
- // 3.转化成字节;
- CBase64::base64ToBin(pBase64, pBin, nByteLen);
- if ( pOutBin == NULL )
- {
- if ( pBin )
- delete []pBin;
- pBin = NULL;
- }
- else
- {
- memcpy(pOutBin, pBin, nByteLen);
- if ( pBin )
- delete []pBin;
- pBin = NULL;
- }
- }
- INT Test_Base642Bin_ASCII(IN CHAR* pBase64, IN INT nBase64Len, OUT CHAR* pOutBin, IN INT nOutLen )
- {
- // 1.计算出字节长度;
- INT nByteLen = CBase64::CalcBinLen(nBase64Len);
- // 2.创建Byte缓存区;
- BYTE *pBin = new BYTE[nByteLen];
- memset(pBin, 0, nByteLen);
- // 3.转化成字节;
- CBase64::base64ToBin(pBase64, pBin, nByteLen);
- if ( pOutBin == NULL )
- {
- if ( pBin )
- delete []pBin;
- pBin = NULL;
- }
- else
- {
- memcpy(pOutBin, pBin, nByteLen);
- if ( pBin )
- delete []pBin;
- pBin = NULL;
- }
- return nByteLen;
- }
- // 从指定的文件读取数据,并生成出Base64对应的文本文件;
- void Test_CreateBase64File(IN LPCTSTR lpBinFile, IN LPCTSTR lpBase64File)
- {
- CFile cf;
- if ( cf.Open(lpBinFile, CFile::modeRead) )
- {
- ULONGLONG uLen = cf.GetLength();
- BYTE *pData = new BYTE[uLen];
- memset(pData, 0, uLen);
- cf.Read(pData, uLen);
- // 1.计算出base 64的长度;
- ULONGLONG uBaseLen = CBase64::CalcBase64Len(uLen);
- // 创建Base64缓存;
- CHAR *pBase64 = new CHAR[uBaseLen + 1];
- memset(pBase64, 0, uBaseLen + 1);
- CBase64::binToBase64(pData, uLen, pBase64);
- CFile cf64;
- if ( cf64.Open(lpBase64File, CFile::modeCreate|CFile::modeWrite) )
- {
- cf64.Write(pBase64, uBaseLen + 1);
- if ( pBase64 )
- delete []pBase64;
- pBase64 = NULL;
- cf64.Close();
- }
- if ( pData )
- delete []pData;
- pData = NULL;
- cf.Close();
- }
- }
- // 将Base64内容的文本文件恢复回正常的文件;
- void Test_CreateBindFile(IN LPCTSTR lpBase64File, IN LPCTSTR lpBinFile )
- {
- CFile cf64;
- if ( cf64.Open(lpBase64File, CFile::modeRead) )
- {
- ULONGLONG uLen = cf64.GetLength();
- BYTE *pBase64Data = new BYTE[uLen];
- memset(pBase64Data, 0, uLen);
- cf64.Read(pBase64Data, uLen);
- // 1.计算出二进制字节的长度;
- ULONGLONG uBinLen = CBase64::CalcBinLen(uLen);
- // 创建binary缓存;
- BYTE *pByte = new BYTE[uBinLen];
- memset(pByte, 0, uBinLen);
- uBinLen = CBase64::base64ToBin((CHAR*)pBase64Data, pByte, uBinLen);
- pByte[uBinLen] = '\0'; // 必须操作这一步,否则数据末尾不会正常截断;
- CFile cfbin;
- if ( cfbin.Open(lpBinFile, CFile::modeCreate|CFile::modeWrite) )
- {
- cfbin.Write(pByte, uBinLen);
- if ( pByte )
- delete []pByte;
- pByte = NULL;
- cfbin.Close();
- }
- if ( pBase64Data )
- delete []pBase64Data;
- pBase64Data = NULL;
- cf64.Close();
- }
- }
- //////////////////////////////////////////////////////////////////////////
- // 将文件使用Aes计算器模式加密,并生成加密文件;
- void Test_Aes_Encryptfile_CTR(IN LPCTSTR lpFile, IN LPCCH lpKey, IN LPCTSTR lpAesFile)
- {
- // 1.定义数据;
- BYTE *pFileBuff = NULL; // 文件缓存指针;
- ULONGLONG uFileLen = 0; // 文件长度;
- BYTE *pOutBuff = NULL; // 加密后的文件数据;
- ULONGLONG uOutLen = 0; // 文件数据加密后的长度;
- CHAR szKey[MAX_PATH] = {0}; // 加密使用的密钥;
- CHAR szCounter[16] = {0}; // 计算器模式下的计算值;
- // 设置计算器计算值;
- szCounter[5] = -113;
- // 获取文件数据;
- CFile cf;
- if ( cf.Open(lpFile, CFile::modeRead) )
- {
- uFileLen = cf.GetLength();
- pFileBuff = new BYTE[uFileLen];
- memset(pFileBuff, 0, uFileLen);
- cf.Read(pFileBuff, uFileLen);
- cf.Close();
- }
- // 3.开始加密;
- AesCipher AesCrypto;
- AesCrypto.makeRoundKey(lpKey, strlen(lpKey));
- AesCrypto.setCounter(szCounter);
- // 计算出文件加密后的长度;
- uOutLen = AesCrypto.calculateCipherLen(uFileLen);
- pOutBuff = new BYTE[uOutLen];
- memset(pOutBuff, 0, uOutLen);
- // 加密文件数据成字符串;
- AesCrypto.encryptString((CHAR*)pFileBuff, (CHAR*)pOutBuff, uFileLen, AesCipher::CTR);
- // 释放文件数据资源;
- if ( pFileBuff )
- delete []pFileBuff;
- pFileBuff = NULL;
- // 将加密后的数据保存到文件中;
- CFile cfAes;
- if ( cfAes.Open(lpAesFile, CFile::modeCreate|CFile::modeWrite) )
- {
- cfAes.Write(pOutBuff, uOutLen);
- cfAes.Close();
- }
- // 释放加密后的文件数据;
- if ( pOutBuff )
- delete []pOutBuff;
- pOutBuff = NULL;
- }
- void Test_Aes_Decryptfile_CTR(IN LPCTSTR lpFile, IN LPCCH lpKey, IN LPCTSTR lpAesFile)
- {
- // 1.定义数据;
- BYTE *pFileBuff = NULL; // 文件缓存指针;
- ULONGLONG uFileLen = 0; // 文件长度;
- BYTE *pOutBuff = NULL; // 解密后的文件数据;
- ULONGLONG uOutLen = 0; // 文件数据解密后的长度;
- CHAR szKey[MAX_PATH] = {0}; // 加密使用的密钥;
- CHAR szCounter[16] = {0}; // 计算器模式下的计算值;
- // 设置计算器计算值;
- szCounter[5] = -113;
- // 获取文件数据;
- CFile cf;
- if ( cf.Open(lpFile, CFile::modeRead) )
- {
- uFileLen = cf.GetLength();
- pFileBuff = new BYTE[uFileLen];
- memset(pFileBuff, 0, uFileLen);
- cf.Read(pFileBuff, uFileLen);
- cf.Close();
- }
- // 3.开始解密;
- AesCipher AesCrypto;
- AesCrypto.makeRoundKey(lpKey, strlen(lpKey));
- AesCrypto.setCounter(szCounter);
- // 初始化输出数据;
- pOutBuff = new BYTE[uFileLen + 1];
- memset(pOutBuff, 0, uFileLen + 1);
- // 解密文件数据成字符串,并返回解密后的数据长度;
- uOutLen = AesCrypto.decryptString((CHAR*)pFileBuff, (CHAR*)pOutBuff, uFileLen, AesCipher::CTR);
- pOutBuff[uOutLen] = '\0'; // 必须操作这一步,否则数据末尾不会正常截断;
- // 释放文件数据资源;
- if ( pFileBuff )
- delete []pFileBuff;
- pFileBuff = NULL;
- // 将加密后的数据保存到文件中;
- CFile cfAes;
- if ( cfAes.Open(lpAesFile, CFile::modeCreate|CFile::modeWrite) )
- {
- cfAes.Write(pOutBuff, uOutLen);
- cfAes.Close();
- }
- // 释放加密后的文件数据;
- if ( pOutBuff )
- delete []pOutBuff;
- pOutBuff = NULL;
- }
- // ECB, CBC, CFB, OFB工作模式(明文比密文长度要短很多);
- void Test_Aes_Decryptfile_Others(IN LPCCH lpPlainText, IN LPCCH lpKey, IN LPCCH lpCounter)
- {
- // 参数有效性判断;
- if ( lpPlainText == NULL || lpKey == NULL )
- return;
- // 密钥是否符合长度要求;
- INT nKeyLen = strlen(lpKey);
- if ( nKeyLen != 16 && nKeyLen != 24 && nKeyLen != 32 )
- return;
- // 明文长度;
- INT nPlainTextLen = strlen(lpPlainText);
- AesCipher AesCrypt;
- AesCrypt.makeRoundKey(lpKey);
- if ( lpCounter )
- AesCrypt.setCounter(lpCounter);
- // 输出的密文;
- CHAR* pOutCipherText = NULL;
- // 计算密文长度;
- INT nCipherTextLen = AesCrypt.calculateCipherLen(strlen(lpPlainText));
- // 创建密文堆;
- pOutCipherText = new CHAR[nCipherTextLen+1];
- memset(pOutCipherText, 0, nCipherTextLen + 1);
- // 解密后的明文;
- CHAR* pPlainText = NULL;
- pPlainText = new CHAR[nCipherTextLen+1];
- memset(pPlainText, 0 ,nCipherTextLen+1);
- // ECB mode;
- printf("\n\nECB*****************************************************\n");
- AesCrypt.encryptString(lpPlainText, pOutCipherText, nPlainTextLen, AesCipher::ECB);
- //pOutCipherText[nCipherTextLen] = '\0';
- printf("ECB Encrypt mode \n %s \n %s \n\n", lpPlainText, pOutCipherText);
- nPlainTextLen = AesCrypt.decryptString(pOutCipherText, pPlainText, nCipherTextLen, AesCipher::ECB);
- pPlainText[nPlainTextLen] = '\0';
- printf("ECB Decrypt mode \n %s \n %s \n\n", pOutCipherText, pPlainText);
- // CBC mode;
- printf("\n\nCBC*****************************************************\n");
- memset(pOutCipherText, 0, nCipherTextLen + 1);
- memset(pPlainText, 0 ,nCipherTextLen+1);
- // 为CBC、CFB、OFB三模式设置初始向量;
- AesCrypt.setIV("Cache.0123456789");
- AesCrypt.encryptString(lpPlainText, pOutCipherText, nPlainTextLen, AesCipher::CBC);
- //pOutCipherText[nCipherTextLen] = '\0';
- printf("CBC Encrypt mode \n %s \n %s \n\n", lpPlainText, pOutCipherText);
- nPlainTextLen = AesCrypt.decryptString(pOutCipherText, pPlainText, nCipherTextLen, AesCipher::CBC);
- pPlainText[nPlainTextLen] = '\0';
- printf("CBC Decrypt mode \n %s \n %s \n\n", pOutCipherText, pPlainText);
- // CFB mode;
- printf("\n\nCFB*****************************************************\n");
- memset(pOutCipherText, 0, nCipherTextLen + 1);
- memset(pPlainText, 0 ,nCipherTextLen+1);
- AesCrypt.encryptString(lpPlainText, pOutCipherText, nPlainTextLen, AesCipher::CFB);
- //pOutCipherText[nCipherTextLen] = '\0';
- printf("CFB Encrypt mode \n %s \n %s \n\n", lpPlainText, pOutCipherText);
- nPlainTextLen = AesCrypt.decryptString(pOutCipherText, pPlainText, nCipherTextLen, AesCipher::CFB);
- pPlainText[nPlainTextLen] = '\0';
- printf("CFB Decrypt mode \n %s \n %s \n\n", pOutCipherText, pPlainText);
- // OFB mode;
- printf("\n\nOFB*****************************************************\n");
- memset(pOutCipherText, 0, nCipherTextLen + 1);
- memset(pPlainText, 0 ,nCipherTextLen+1);
- AesCrypt.encryptString(lpPlainText, pOutCipherText, nPlainTextLen, AesCipher::OFB);
- //pOutCipherText[nCipherTextLen] = '\0';
- printf("OFB Encrypt mode \n %s \n %s \n\n", lpPlainText, pOutCipherText);
- nPlainTextLen = AesCrypt.decryptString(pOutCipherText, pPlainText, nCipherTextLen, AesCipher::OFB);
- pPlainText[nPlainTextLen] = '\0';
- printf("OFB Decrypt mode \n %s \n %s \n\n", pOutCipherText, pPlainText);
- }
- //
- //void DES()
- //{
- // int i, j;
- // unsigned char buf[1024];
- // unsigned long tsc;
- // unsigned char tmp[64];
- //
- // CHAR* pBase64 = NULL;
- // //CHAR* pBinary = NULL;
- // BYTE szVI[9] = "ABCD6789";
- // CHAR szBinary[MAX_PATH] = {0};
- //
- // des_context des;
- // des3_context des3;
- //
- // //密钥是01234567, 明文zth DES
- // memset(tmp, 0, sizeof(tmp));
- // strcpy((char*)tmp, "01234567");
- // memset(buf, 0, sizeof(buf));
- // strcpy((char*)buf, "zth");
- //
- // // 设置加密的des结构体值;
- // des_setkey_enc( &des, tmp);
- //
- // printf("pwd=%s, txt=%s\n", tmp, buf);
- // // 加密;
- // des_crypt_cbc(&des, DES_ENCRYPT, 8, szVI, buf, buf );
- // //Test_Base642Bin_ASCII();
- //
- // Test_Bin2Base64_ASCII((char*)buf, strlen((char*)buf), &pBase64);
- // Test_Base642Bin_ASCII(pBase64, strlen(pBase64), szBinary, MAX_PATH);
- // /*printf("encrypt txt= \n %s\n%s\n%s\n", tmp, buf, buf);
- // for(i=0; i<8; i++) printf("%x ", buf[i]);
- // printf("\n");*/
- //
- // // 解密;
- // memset(szVI, 0, sizeof(szVI));
- // strcpy((char*)szVI, "ABCD6789");
- // // 设置解密des结构体;
- // des_setkey_dec(&des, tmp);
- // // 解密;
- // des_crypt_cbc(&des, DES_DECRYPT, 8, szVI, (BYTE*)szBinary, buf );
- // /*printf("decrypt txt=");
- // for(i=0; i<8; i++) printf("%x ", buf[i]);
- // printf("\n\n");*/
- //
- // //密钥是01234567abcdefgh, 明文zth DES3
- // memset(tmp, 0, sizeof(tmp)); strcpy((char*)tmp, "01234567abcdefgh");
- // memset(buf, 0, sizeof(buf)); strcpy((char*)buf, "zth");
- // des3_set3key_enc( &des3, tmp);
- // printf("DES3 pwd=%s, txt=%s\n", tmp, buf);
- // des3_crypt_cbc(&des3, DES_ENCRYPT, 8, tmp, buf, buf );
- // printf("encrypt txt=", tmp, buf, buf);
- // for(i=0; i<8; i++) printf("%x ", buf[i]);
- // printf("\n");
- //
- // memset(tmp, 0, sizeof(tmp)); strcpy((char*)tmp, "01234567abcdefgh");
- // des3_set3key_dec(&des3, tmp);
- // des3_crypt_cbc(&des3, DES_DECRYPT, 8, tmp, buf, buf );
- // printf("decrypt txt=");
- // for(i=0; i<8; i++) printf("%x ", buf[i]);
- // printf("\n");
- //
- // system("pause");
- //}
- void DES_EncryptFile(IN LPCTSTR lpFile, IN LPBYTE lpKey, IN LPBYTE lpVI, IN LPCTSTR lpEncryFile)
- {
- if ( lpFile == NULL )
- return;
- if ( lpKey == NULL || lpVI == NULL )
- return;
- INT nKeyLen = strlen((char*)lpKey);
- if (nKeyLen != 8 )
- return;
- INT nVILen = strlen((char*)lpVI);
- if ( nVILen != 8 )
- return;
- CFile cf;
- BYTE *pFileData = NULL;
- BYTE *pOutData = NULL;
- INT nPadLen = 0;
- ULONGLONG nFileLen = 0;
- ULONGLONG nOutLen = 0;
- if ( cf.Open(lpFile, CFile::modeRead) )
- {
- nFileLen = cf.GetLength();
- nOutLen = des_enc_len(nFileLen);
- pFileData = new BYTE[nOutLen];
- memset(pFileData, 0, nOutLen);
- cf.Read(pFileData,nFileLen);
- cf.Close();
- }
- pOutData = new BYTE[nOutLen + 1];
- memset(pOutData, 0, nOutLen + 1);
- des_context des;
- if ( des_key_check_key_parity(lpKey) == 1)
- {
- printf("key was not correct\n");
- des_key_set_parity(lpKey);
- if ( des_key_check_key_parity(lpKey) == 0 )
- {
- des_setkey_enc(&des, lpKey);
- }
- }
- else
- des_setkey_enc(&des, lpKey);
- des_crypt_cbc(&des, DES_ENCRYPT, nOutLen, lpVI, pFileData, pOutData);
- CFile cfo;
- if ( cfo.Open(lpEncryFile, CFile::modeCreate|CFile::modeWrite) )
- {
- cfo.Write(pOutData, nOutLen);
- cfo.Close();
- }
- if ( pOutData )
- delete []pOutData;
- }
- void DES_DecryptFile(IN LPCTSTR lpFile, IN LPBYTE lpKey, IN LPBYTE lpVI, IN LPCTSTR lpDecryFile)
- {
- if ( lpFile == NULL )
- return;
- if ( lpKey == NULL || lpVI == NULL )
- return;
- INT nKeyLen = strlen((char*)lpKey);
- if (nKeyLen != 8 )
- return;
- INT nVILen = strlen((char*)lpVI);
- if ( nVILen != 8 )
- return;
- CFile cf;
- BYTE *pFileData = NULL;
- BYTE *pOutData = NULL;
- UINT64 nFileLen = 0;
- if ( cf.Open(lpFile, CFile::modeRead) )
- {
- nFileLen = cf.GetLength();
- pFileData = new BYTE[nFileLen];
- memset(pFileData, 0, nFileLen);
- cf.Read(pFileData,nFileLen);
- cf.Close();
- }
- // nFileLen 是否是8的整数倍,不是则加满;
- UINT64 nOutLen = nFileLen;
- pOutData = new BYTE[nOutLen + 1];
- memset(pOutData, 0, nOutLen + 1);
- des_context des;
- if ( des_key_check_key_parity(lpKey) == 1)
- {
- printf("key was not correct\n");
- des_key_set_parity(lpKey);
- if ( des_key_check_key_parity(lpKey) == 0 )
- {
- des_setkey_dec(&des, lpKey);
- }
- }
- else
- des_setkey_dec(&des, lpKey);
- des_crypt_cbc(&des, DES_DECRYPT, nOutLen, lpVI, pFileData, pOutData);
- // 解密后,要去除填充的字符;
- /*while ( pOutData[nOutLen-1] == 0x00 )
- {
- nOutLen--;
- }*/
- nOutLen = des_dec_len(pOutData,nOutLen);
- CFile cfo;
- if ( cfo.Open(lpDecryFile, CFile::modeCreate|CFile::modeWrite) )
- {
- cfo.Write(pOutData, nOutLen);
- cfo.Close();
- }
- if ( pOutData )
- delete []pOutData;
- }
- CWinApp theApp;
- using namespace std;
- //#include "md5和sha1.h"
- #include "sha1.h"
- #include <list>
- using namespace std;
- int cmp(const void* a, const void* b){
- return *(int*)a - *(int*)b;
- }
- inline void println(int arr[], int len){
- for(int i = 0; i < len; i++){
- cout << arr[i] << " ";
- }
- //cout << endl;
- }
- inline void reverse(int arr[], int left, int right){
- while(right >= left){
- swap(arr[left++], arr[right--]);
- }
- }
- void full_permutation(int arr[], int len){
- qsort(arr, len, sizeof(int), cmp);
- println(arr, len);
- while(true){
- int j = len-1;
- int t = len;
- while(j >= 0 && arr[--j] >= arr[j+1]) ;
- if(j >= 0){
- while(arr[--t] <= arr[j]) ;
- swap(arr[t], arr[j]);
- reverse(arr, j+1, len-1);
- println(arr, len);
- }
- else{
- break;
- }
- }
- }
- // 冒泡排序;
- void BubbleSort(string *s, int count)
- {
- string temp;
- for (int i = 1; i < count; i++)
- {
- for (int j = count - 1; j >= i; j--)
- {
- if (s[j].compare(s[j - 1]) < 0)
- {
- temp = s[j - 1];
- s[j - 1] = s[j];
- s[j] = temp;
- }
- }
- }
- }
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- int nRetCode = 0;
- // 初始化 MFC 并在失败时显示错误
- if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
- {
- // TODO: 更改错误代码以符合您的需要
- _tprintf(_T("错误: MFC 初始化失败\n"));
- nRetCode = 1;
- }
- else
- {
- #if 0
- char sz[3] = {0};
- BYTE by = 0xff;
- ByteTurn2HexChar(by, sz);
- string ss[3] = {"J8UcPVTTxPqZYRw2ZmVEznX6j","201607111759111","lyfz.net"};
- BubbleSort(ss,3);
- string sAll;
- for ( int i = 0; i < 3; i++ )
- {
- sAll.append(ss[i]);
- }
- BYTE szSource[560] = "lyfz.net";
- BYTE byResult[21] = {0};
- BYTE byHexResult[250] = {0};
- memset(szSource, 0, 560);
- memcpy(szSource, sAll.c_str(), sAll.size());
- sha1(szSource, sAll.size(), byResult);
- ByteToTwoByte(byResult, 20, byHexResult);
- sha1Hex(szSource, sAll.size(), byHexResult);
-
- sha1(szSource, strlen((char*)szSource), byResult);
- sha1Hex(szSource, strlen((char*)szSource), byHexResult);
- printf("原始加密:%s\n 16进制加密:%s\n", byResult, byHexResult);
- system("pause");
- #endif
- #if 1
- CONST TCHAR* pMD5 = NULL;
- // TODO: 在此处为应用程序的行为编写代码。
- printf("示例:MD5加密'订单1234', 结果如下:\n");
- // 读取文件大小;
- CStringA strpath = "D:\\0.dat";
- struct stat statbuf;
- stat(strpath.GetString(), &statbuf);
- BYTE *pFileData = new BYTE[statbuf.st_size + 1];
- memset(pFileData, 0, statbuf.st_size);
- // 读取文件内容;
- #if 0
- FILE *f = NULL;
- fopen_s(&f,strpath, "r+");
- if ( f != NULL )
- {
- fread_s(pFileData, statbuf.st_size, sizeof(BYTE), statbuf.st_size, f);
- fclose(f);
- }
- else
- {
- return -1;
- }
- #else
- CFile cf;
- if ( cf.Open(strpath, CFile::modeRead) )
- {
- //DWORD dwlen = cf.GetLength();
- //if ( dwlen == statbuf.st_size )
- //{
- // TRACE("dfdfdfd");
- //}
- cf.Read(pFileData, statbuf.st_size);
- }
- #endif
- CHAR *pUTF8 = new CHAR[statbuf.st_size+1];
- StringProcess::ascii_to_utf8((const char*)pFileData, pUTF8);
- CMD5 mdd5(pFileData, statbuf.st_size);
- pMD5 = mdd5.GetMD5Digest();
- mdd5.SetBYTEText(reinterpret_cast<BYTE*>(pUTF8), strlen(pUTF8));
- pMD5 = mdd5.GetMD5Digest();
-
-
- // 默认GB2321的md5值;
- CMD5 md5((const BYTE*)("AT中国123"), strlen("AT中国123"));
- pMD5 = md5.GetMD5Digest();
- _tprintf(_T("GB2321\t\t:%s\n"), pMD5);
- // 获取utf8的md5值;
- CHAR szUtf8[MAX_PATH] = {0};
- CHAR szbuffer[MAX_PATH] = "AT中国123";
- StringProcess::ascii_to_utf8(szbuffer,szUtf8);
- md5.SetBYTEText(reinterpret_cast<BYTE*>(szUtf8), strlen(szUtf8));
- pMD5 = md5.GetMD5Digest();
- _tprintf(_T("utf8\t\t:%s\n"), pMD5);
- // 获取unicode的md5值;
- WCHAR szUnicode[MAX_PATH] = L"AT中国123";
- BYTE szUN[MAX_PATH] = {0};
- INT nBytes = sizeof(WCHAR)*wcslen(szUnicode);
- memcpy(szUN, szUnicode, nBytes);
- //INT nChars = strlen((char*)szUN);
- md5.SetBYTEText(reinterpret_cast<BYTE*>(szUN), nBytes);
- pMD5 = md5.GetMD5Digest();
- _tprintf(_T("unicode\t\t:%s\n"), pMD5);
- // 获取ascii的md5值;
- CHAR szAscii[MAX_PATH] = {0};
- StringProcess::utf8_to_ascii(szUtf8,szAscii);
- md5.SetBYTEText(reinterpret_cast<BYTE*>(szAscii), strlen(szAscii));
- pMD5 = md5.GetMD5Digest();
- _tprintf(_T("ascii\t\t:%s\n"), pMD5);
- #endif
- #if 0 // 测试Base64类的有效性;
- Test_CreateBase64File(_T("D:\\dindansp2.txt"), _T("D:\\base64.txt"));
- Test_CreateBindFile(_T("D:\\base64.txt"), _T("D:\\binary.txt"));
-
- WCHAR szString[MAX_PATH] = L"11寸水晶";
- CHAR *pBase64 = NULL;
- INT nBase64Len = Test_Bin2Base64_WChar(szString, _tcslen(szString)*sizeof(WCHAR), &pBase64);
-
- WCHAR szWBase64[MAX_PATH] = {0};
- Test_Base642Bin_WChar(pBase64, nBase64Len, szWBase64, MAX_PATH);
- if ( pBase64 )
- delete []pBase64;
- pBase64 = NULL;
- #endif
- #if 0
- Test_Aes_Encryptfile_CTR(_T("F:\\刻录驱动.rar"), "A012369DFEROE.LKDJFOI*&^%$!@_+~!", _T("D:\\Aes_ke.txt"));
- Test_CreateBase64File(_T("D:\\Aes_ke.txt"), _T("D:\\base64_ke.txt"));
- Test_CreateBindFile(_T("D:\\base64_ke.txt"), _T("D:\\binary_ke.txt"));
- Test_Aes_Decryptfile_CTR(_T("D:\\binary_ke.txt"), "A012369DFEROE.LKDJFOI*&^%$!@_+~!", _T("D:\\刻录驱动.rar"));
- #endif
- #if 0
- // 方八水立方册;ecnrptyIyFsVC3Y65L3qv+jvdrH
- CHAR szExample[MAX_PATH] = "方10公主梦幻册";
- CHAR szUTF8[MAX_PATH] = {0};
- StringProcess::ascii_to_utf8(szExample,szUTF8);
-
- CHAR szBase64[MAX_PATH] = "pagFVaDD8KfR24+mq4k=";
- CHAR szBinary[MAX_PATH] = {0};
- Test_Base642Bin_ASCII(szBase64, strlen(szBase64), szBinary, MAX_PATH);
- StringProcess::utf8_to_ascii(szUTF8, szExample);
- StringProcess::utf8_to_ascii(szBinary, szExample);
- system("pause");
- #endif
- #if 0
- CHAR szCounter[16] = {0};
- szCounter[7] = 113;
- szCounter[15] = -25;
- Test_Aes_Decryptfile_Others("加拿大记者借“人权”问题发难 王毅怒斥:傲慢与偏见", "ABCD0123456789EF", NULL);
- printf("\n\n\n使用计数器");// 证实计数器只对CTR模式有用;
- Test_Aes_Decryptfile_Others("加拿大记者借“人权”问题发难 王毅怒斥:傲慢与偏见", "ABCD0123456789EF", szCounter);
- #endif
- #if 0
- //DES();
- BYTE szKey[MAX_PATH] = "lyfz.net";
- BYTE szVI[MAX_PATH] = "WorkbyIT";
- DES_EncryptFile(_T("E:\\dindansp.txt"), szKey, szVI, _T("E:\\out.txt"));
- sprintf((char*)szVI, "%s", "WorkbyIT");
- DES_DecryptFile(_T("E:\\out.txt"), szKey, szVI, _T("E:\\Decout.txt"));
- #endif
- #if 1
- // N/jZ0RbjHYc=
- BYTE szKey[MAX_PATH] = "mygz/ndu";
- BYTE szVI[MAX_PATH] = "WorkbyIT";
- CHAR szBinary[MAX_PATH] = {0};
- CHAR szBase64[MAX_PATH] = "tO/cYVBbZ3vwZAJ8WQAgdQWLGyycqETbJDc7xB0xZKgK0Ps2noivrY/jhKTdevxI";//ALRJrTZYzN0
- // 解密回二进制;
- INT nReal = Test_Base642Bin_ASCII(szBase64, strlen(szBase64), szBinary, MAX_PATH);
- // 解密;
- des_context des;
- if ( des_key_check_key_parity(szKey) == 1)
- {
- printf("key was not correct\n");
- des_key_set_parity(szKey);
- if ( des_key_check_key_parity(szKey) == 0 )
- {
- des_setkey_dec(&des, szKey);
- }
- }
- else
- des_setkey_dec(&des, szKey);
- CHAR szDesDec[MAX_PATH] = {0};
- INT nInputlen = strlen(szBinary);
- des_crypt_cbc(&des, DES_DECRYPT, nReal, szVI, (const unsigned char*)szBinary, (unsigned char*)szDesDec);
- // 解密后,要去除填充的字符;
- while ( szDesDec[nInputlen-1] == 0x00 )
- {
- nInputlen--;
- }
-
- #endif
- #if 0
- // N/jZ0RbjHYc=
- BYTE szKey[MAX_PATH] = "mygz/ndu";
- BYTE szVI[MAX_PATH] = "WorkbyIT";
-
- CFile cf;
- if ( cf.Open(_T("input.txt"), CFile::modeRead) )
- {
- DWORD dwlength = cf.GetLength();
- BYTE *pData = new BYTE[dwlength +1];
- memset(pData, 0, dwlength + 1);
- cf.Read(pData, dwlength);
- // 解密回二进制;
- INT nByteLen = CBase64::CalcBinLen(dwlength);
- BYTE *pBinary = new BYTE[nByteLen+1];
- memset(pBinary, 0, nByteLen+1);
- Test_Base642Bin_ASCII((char*)pData, dwlength, (char*)pBinary, MAX_PATH);
- cf.Close();
- CFile ccf;
- if ( ccf.Open(_T("output.txt"), CFile::modeCreate|CFile::modeReadWrite) )
- {
- ccf.Write(pBinary, nByteLen);
- ccf.Close();
- }
- }
- //DES_DecryptFile(_T("output.txt"), szKey, szVI, _T("decout.txt"));
- sprintf((char*)szVI, "%s", "WorkbyIT");
- DES_DecryptFile(_T("output.txt"), szKey, szVI, _T("decout.txt"));
- #endif
- system("pause");
- }
- return nRetCode;
- }
|