// AutoRun3.cpp : Defines the class behaviors for the application. // #include "stdafx.h" #include "AutoRun3.h" #include "AutoRun3Dlg.h" #include #include // #include #include "DBConfig.h" #include "Lzari.h" #define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING) #define KEYLENGTH 0x00800000 #define ENCRYPT_ALGORITHM CALG_RC4 #define ENCRYPT_BLOCK_SIZE 8 #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif CDatabase g_db; extern CDatabase *g_curdb; CString g_server; CString g_server2; CString g_localip; CAutoRun3Dlg *g_pMainWnd=NULL; BOOL g_bSendOK=0; SENDHEAD g_sendhead; #pragma comment(lib, "Shlwapi.lib") void MyGetIPByName(CString &name) { CString strIP=name; name.Empty (); DWORD dwServerIP=0; HOSTENT *host = gethostbyname(strIP); struct in_addr addr; if (host != NULL) { for(int i = 0; host-> h_addr_list[i] != NULL; i ++) { memset(&addr, 0, sizeof(addr)); memcpy(&addr.S_un.S_addr, host-> h_addr_list[i], host-> h_length); dwServerIP = ntohl(addr.S_un.S_addr); } BYTE* pIP = (BYTE*)&dwServerIP; name.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]); } } ///////////////////////////////////////////////////////////////////////////// // CAutoRun3App BEGIN_MESSAGE_MAP(CAutoRun3App, CWinApp) //{{AFX_MSG_MAP(CAutoRun3App) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG ON_COMMAND(ID_HELP, CWinApp::OnHelp) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CAutoRun3App construction CAutoRun3App::CAutoRun3App() { // TODO: add construction code here, // Place all significant initialization in InitInstance } ///////////////////////////////////////////////////////////////////////////// // The one and only CAutoRun3App object CAutoRun3App theApp; ///////////////////////////////////////////////////////////////////////////// // CAutoRun3App initialization BOOL CAutoRun3App::InitInstance() { HANDLE hObject = CreateMutex(NULL,FALSE,_T("CAutoRun3AppXiao")); if(GetLastError() == ERROR_ALREADY_EXISTS) { return false; } int nResult = Transport_Init(); if( TRANSPORT_OK != nResult) { return false; } g_server="lijiafz.vicp.net" ; MyGetIPByName(g_server); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. SetRegistryKey(_T("LYFZ-AutoRun3")); CAutoRun3Dlg dlg; m_pMainWnd = &dlg; int nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel } // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE; } int CAutoRun3App::ExitInstance() { return CWinApp::ExitInstance(); } BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword) { try { HCRYPTPROV hCryptProv; HCRYPTKEY hKey; HCRYPTHASH hHash; PBYTE pbBuffer; DWORD dwBlockLen; DWORD dwBufferLen; DWORD dwCount; //以下获得一个CSP句柄 if(CryptAcquireContext( &hCryptProv, NULL, //NULL表示使用默认密钥容器,默认密钥容器名为用户登陆名 NULL, PROV_RSA_FULL, 0)) { printf("A cryptographic provider has been acquired. \n"); } else//密钥容器不存在 { if(CryptAcquireContext( &hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET))//创建密钥容器 { //创建密钥容器成功,并得到CSP句柄 printf("A new key container has been created.\n"); } else { return 0; } } //-------------------------------------------------------------------- // 创建一个会话密钥(session key) // 会话密钥也叫对称密钥,用于对称加密算法。 // (注: 一个Session是指从调用函数CryptAcquireContext到调用函数 // CryptReleaseContext 期间的阶段。) //-------------------------------------------------------------------- // Create a hash object. if(CryptCreateHash( hCryptProv, CALG_MD5, 0, 0, &hHash)) { printf("A hash object has been created. \n"); } else { return 0; } //-------------------------------------------------------------------- // 用输入的密码产生一个散列 if(CryptHashData( hHash, (BYTE *)szPassword, strlen(szPassword), 0)) { printf("The password has been added to the hash. \n"); } else { return 0; } //-------------------------------------------------------------------- // 通过散列生成会话密钥(session key) if(CryptDeriveKey( hCryptProv, ENCRYPT_ALGORITHM, hHash, KEYLENGTH, &hKey)) { printf("An encryption key is derived from the password hash. \n"); } else { return 0; } //-------------------------------------------------------------------- // Destroy the hash object. CryptDestroyHash(hHash); hHash = NULL; //-------------------------------------------------------------------- // The session key is now ready. //-------------------------------------------------------------------- // 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的,所以被加密的 // 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的 // 数据长度。 dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE; //-------------------------------------------------------------------- // Determine the block size. If a block cipher is used, // it must have room for an extra block. if(ENCRYPT_BLOCK_SIZE > 1) dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE; else dwBufferLen = dwBlockLen; dwCount=dwBufferLen; //-------------------------------------------------------------------- // In a do loop, encrypt the source file and write to the source file. int count; // if(leng%dwBlockLen==0) count=leng/dwBufferLen; // else // count=leng/dwBlockLen+1; for(int i=0; ig_List1array; extern DWORD g_nLeng; extern BYTE *g_pData; extern CString g_str; void DataToArray(CArray*List1array) { List1array->RemoveAll (); if(g_nLeng==0)return; if(g_sendhead.code[0]) { BYTE *lpszOut = NULL; int nOutSize = 0; LZARI Lzari; Lzari.UnCompress(g_pData,g_nLeng,lpszOut,nOutSize); CMemFile memfile; memfile.Attach (lpszOut,nOutSize); Lzari.Release(); CArchive ar(&memfile, CArchive::load); List1array->SetSize(g_sendhead.count[0]); for(int ii=0; iiGetSize (); ii++) { List1array->ElementAt (ii).Serialize (ar); } ar.Close(); memfile.Detach (); } else { CMemFile memfile; memfile.Attach (g_pData,g_nLeng); CArchive ar(&memfile, CArchive::load); List1array->SetSize(g_sendhead.count[0]); for(int ii=0; iiGetSize (); ii++) { List1array->ElementAt (ii).Serialize (ar); } ar.Close(); memfile.Detach (); } } void DataToArray(CArray*List1array, CArray*List2array) { CArray*parray[2]={List1array,List2array}; DWORD bytereads=0; for(int i=0; i<2; i++) { parray[i]->RemoveAll ();if(g_sendhead.length[i]==0)continue; if(g_sendhead.code[i]) { BYTE *lpszOut = NULL; int nOutSize = 0; LZARI Lzari; Lzari.UnCompress(g_pData+bytereads,g_sendhead.length[i],lpszOut,nOutSize); CMemFile memfile; memfile.Attach (lpszOut,nOutSize); Lzari.Release(); bytereads+=g_sendhead.length[i]; CArchive ar(&memfile, CArchive::load); parray[i]->SetSize(g_sendhead.count[i]); for(int ii=0; iiGetSize (); ii++) { parray[i]->ElementAt (ii).Serialize (ar); } ar.Close(); memfile.Detach (); } else { CMemFile memfile; memfile.Attach (g_pData+bytereads,g_sendhead.length[i]); bytereads+=g_sendhead.length[i]; CArchive ar(&memfile, CArchive::load); parray[i]->SetSize(g_sendhead.count[i]); for(int ii=0; iiGetSize (); ii++) { parray[i]->ElementAt (ii).Serialize (ar); } ar.Close(); memfile.Detach (); } } }