AutoRun3.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // AutoRun3.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "AutoRun3.h"
  5. #include "AutoRun3Dlg.h"
  6. #include <initguid.h>
  7. #include <afxsock.h>//<Winsock2.h>
  8. #include <wincrypt.h>
  9. #include "DBConfig.h"
  10. #include "Lzari.h"
  11. #define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
  12. #define KEYLENGTH 0x00800000
  13. #define ENCRYPT_ALGORITHM CALG_RC4
  14. #define ENCRYPT_BLOCK_SIZE 8
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. CDatabase g_db;
  21. extern CDatabase *g_curdb;
  22. CString g_server;
  23. CString g_server2;
  24. CString g_localip;
  25. CAutoRun3Dlg *g_pMainWnd=NULL;
  26. BOOL g_bSendOK=0;
  27. SENDHEAD g_sendhead;
  28. #pragma comment(lib, "Shlwapi.lib")
  29. void MyGetIPByName(CString &name)
  30. {
  31. CString strIP=name;
  32. name.Empty ();
  33. DWORD dwServerIP=0;
  34. HOSTENT *host = gethostbyname(strIP);
  35. struct in_addr addr;
  36. if (host != NULL)
  37. {
  38. for(int i = 0; host-> h_addr_list[i] != NULL; i ++)
  39. {
  40. memset(&addr, 0, sizeof(addr));
  41. memcpy(&addr.S_un.S_addr, host-> h_addr_list[i], host-> h_length);
  42. dwServerIP = ntohl(addr.S_un.S_addr);
  43. }
  44. BYTE* pIP = (BYTE*)&dwServerIP;
  45. name.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]);
  46. }
  47. }
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CAutoRun3App
  50. BEGIN_MESSAGE_MAP(CAutoRun3App, CWinApp)
  51. //{{AFX_MSG_MAP(CAutoRun3App)
  52. // NOTE - the ClassWizard will add and remove mapping macros here.
  53. // DO NOT EDIT what you see in these blocks of generated code!
  54. //}}AFX_MSG
  55. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  56. END_MESSAGE_MAP()
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CAutoRun3App construction
  59. CAutoRun3App::CAutoRun3App()
  60. {
  61. // TODO: add construction code here,
  62. // Place all significant initialization in InitInstance
  63. }
  64. /////////////////////////////////////////////////////////////////////////////
  65. // The one and only CAutoRun3App object
  66. CAutoRun3App theApp;
  67. /////////////////////////////////////////////////////////////////////////////
  68. // CAutoRun3App initialization
  69. BOOL CAutoRun3App::InitInstance()
  70. {
  71. HANDLE hObject = CreateMutex(NULL,FALSE,_T("CAutoRun3AppXiao"));
  72. if(GetLastError() == ERROR_ALREADY_EXISTS)
  73. {
  74. return false;
  75. }
  76. int nResult = Transport_Init();
  77. if( TRANSPORT_OK != nResult)
  78. {
  79. return false;
  80. }
  81. g_server="lijiafz.vicp.net" ;
  82. MyGetIPByName(g_server);
  83. // Standard initialization
  84. // If you are not using these features and wish to reduce the size
  85. // of your final executable, you should remove from the following
  86. // the specific initialization routines you do not need.
  87. SetRegistryKey(_T("LYFZ-AutoRun3"));
  88. CAutoRun3Dlg dlg;
  89. m_pMainWnd = &dlg;
  90. int nResponse = dlg.DoModal();
  91. if (nResponse == IDOK)
  92. {
  93. // TODO: Place code here to handle when the dialog is
  94. // dismissed with OK
  95. }
  96. else if (nResponse == IDCANCEL)
  97. {
  98. // TODO: Place code here to handle when the dialog is
  99. // dismissed with Cancel
  100. }
  101. // Since the dialog has been closed, return FALSE so that we exit the
  102. // application, rather than start the application's message pump.
  103. return FALSE;
  104. }
  105. int CAutoRun3App::ExitInstance()
  106. {
  107. return CWinApp::ExitInstance();
  108. }
  109. BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword)
  110. {
  111. try
  112. {
  113. HCRYPTPROV hCryptProv;
  114. HCRYPTKEY hKey;
  115. HCRYPTHASH hHash;
  116. PBYTE pbBuffer;
  117. DWORD dwBlockLen;
  118. DWORD dwBufferLen;
  119. DWORD dwCount;
  120. //以下获得一个CSP句柄
  121. if(CryptAcquireContext(
  122. &hCryptProv,
  123. NULL, //NULL表示使用默认密钥容器,默认密钥容器名为用户登陆名
  124. NULL,
  125. PROV_RSA_FULL,
  126. 0))
  127. {
  128. printf("A cryptographic provider has been acquired. \n");
  129. }
  130. else//密钥容器不存在
  131. {
  132. if(CryptAcquireContext(
  133. &hCryptProv,
  134. NULL,
  135. NULL,
  136. PROV_RSA_FULL,
  137. CRYPT_NEWKEYSET))//创建密钥容器
  138. {
  139. //创建密钥容器成功,并得到CSP句柄
  140. printf("A new key container has been created.\n");
  141. }
  142. else
  143. {
  144. return 0;
  145. }
  146. }
  147. //--------------------------------------------------------------------
  148. // 创建一个会话密钥(session key)
  149. // 会话密钥也叫对称密钥,用于对称加密算法。
  150. // (注: 一个Session是指从调用函数CryptAcquireContext到调用函数
  151. // CryptReleaseContext 期间的阶段。)
  152. //--------------------------------------------------------------------
  153. // Create a hash object.
  154. if(CryptCreateHash(
  155. hCryptProv,
  156. CALG_MD5,
  157. 0,
  158. 0,
  159. &hHash))
  160. {
  161. printf("A hash object has been created. \n");
  162. }
  163. else
  164. {
  165. return 0;
  166. }
  167. //--------------------------------------------------------------------
  168. // 用输入的密码产生一个散列
  169. if(CryptHashData(
  170. hHash,
  171. (BYTE *)szPassword,
  172. strlen(szPassword),
  173. 0))
  174. {
  175. printf("The password has been added to the hash. \n");
  176. }
  177. else
  178. {
  179. return 0;
  180. }
  181. //--------------------------------------------------------------------
  182. // 通过散列生成会话密钥(session key)
  183. if(CryptDeriveKey(
  184. hCryptProv,
  185. ENCRYPT_ALGORITHM,
  186. hHash,
  187. KEYLENGTH,
  188. &hKey))
  189. {
  190. printf("An encryption key is derived from the password hash. \n");
  191. }
  192. else
  193. {
  194. return 0;
  195. }
  196. //--------------------------------------------------------------------
  197. // Destroy the hash object.
  198. CryptDestroyHash(hHash);
  199. hHash = NULL;
  200. //--------------------------------------------------------------------
  201. // The session key is now ready.
  202. //--------------------------------------------------------------------
  203. // 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的,所以被加密的
  204. // 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的
  205. // 数据长度。
  206. dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
  207. //--------------------------------------------------------------------
  208. // Determine the block size. If a block cipher is used,
  209. // it must have room for an extra block.
  210. if(ENCRYPT_BLOCK_SIZE > 1)
  211. dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
  212. else
  213. dwBufferLen = dwBlockLen;
  214. dwCount=dwBufferLen;
  215. //--------------------------------------------------------------------
  216. // In a do loop, encrypt the source file and write to the source file.
  217. int count;
  218. // if(leng%dwBlockLen==0)
  219. count=leng/dwBufferLen;
  220. // else
  221. // count=leng/dwBlockLen+1;
  222. for(int i=0; i<count; i++)
  223. {
  224. pbBuffer=buffer+i*dwBufferLen;
  225. // 加密数据
  226. if(!CryptEncrypt(
  227. hKey, //密钥
  228. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  229. 0, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  230. //这里通过判断是否到文件尾来决定是否为最后一块。
  231. 0, //保留
  232. pbBuffer, //输入被加密数据,输出加密后的数据
  233. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  234. dwBufferLen)) //pbBuffer的大小。
  235. {
  236. return 0;
  237. }
  238. }
  239. if(leng%dwBlockLen)
  240. {
  241. pbBuffer=buffer+i*dwBufferLen;
  242. dwCount=leng-i*dwBufferLen;
  243. if(!CryptEncrypt(
  244. hKey, //密钥
  245. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  246. TRUE, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  247. //这里通过判断是否到文件尾来决定是否为最后一块。
  248. 0, //保留
  249. pbBuffer, //输入被加密数据,输出加密后的数据
  250. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  251. dwBufferLen)) //pbBuffer的大小。
  252. {
  253. return 0;
  254. }
  255. }
  256. //--------------------------------------------------------------------
  257. // Destroy session key.
  258. if(hKey)
  259. CryptDestroyKey(hKey);
  260. //--------------------------------------------------------------------
  261. // Destroy hash object.
  262. if(hHash)
  263. CryptDestroyHash(hHash);
  264. //--------------------------------------------------------------------
  265. // Release provider handle.
  266. if(hCryptProv)
  267. CryptReleaseContext(hCryptProv, 0);
  268. return(TRUE);
  269. }
  270. catch(...)
  271. {
  272. }
  273. }
  274. extern CArray<CStringArray, CStringArray>g_List1array;
  275. extern DWORD g_nLeng;
  276. extern BYTE *g_pData;
  277. extern CString g_str;
  278. void DataToArray(CArray<CStringArray, CStringArray>*List1array)
  279. {
  280. List1array->RemoveAll ();
  281. if(g_nLeng==0)return;
  282. if(g_sendhead.code[0])
  283. {
  284. BYTE *lpszOut = NULL;
  285. int nOutSize = 0;
  286. LZARI Lzari;
  287. Lzari.UnCompress(g_pData,g_nLeng,lpszOut,nOutSize);
  288. CMemFile memfile;
  289. memfile.Attach (lpszOut,nOutSize);
  290. Lzari.Release();
  291. CArchive ar(&memfile, CArchive::load);
  292. List1array->SetSize(g_sendhead.count[0]);
  293. for(int ii=0; ii<List1array->GetSize (); ii++)
  294. {
  295. List1array->ElementAt (ii).Serialize (ar);
  296. }
  297. ar.Close();
  298. memfile.Detach ();
  299. }
  300. else
  301. {
  302. CMemFile memfile;
  303. memfile.Attach (g_pData,g_nLeng);
  304. CArchive ar(&memfile, CArchive::load);
  305. List1array->SetSize(g_sendhead.count[0]);
  306. for(int ii=0; ii<List1array->GetSize (); ii++)
  307. {
  308. List1array->ElementAt (ii).Serialize (ar);
  309. }
  310. ar.Close();
  311. memfile.Detach ();
  312. }
  313. }
  314. void DataToArray(CArray<CStringArray, CStringArray>*List1array, CArray<CStringArray, CStringArray>*List2array)
  315. {
  316. CArray<CStringArray, CStringArray>*parray[2]={List1array,List2array};
  317. DWORD bytereads=0;
  318. for(int i=0; i<2; i++)
  319. {
  320. parray[i]->RemoveAll ();if(g_sendhead.length[i]==0)continue;
  321. if(g_sendhead.code[i])
  322. {
  323. BYTE *lpszOut = NULL;
  324. int nOutSize = 0;
  325. LZARI Lzari;
  326. Lzari.UnCompress(g_pData+bytereads,g_sendhead.length[i],lpszOut,nOutSize);
  327. CMemFile memfile;
  328. memfile.Attach (lpszOut,nOutSize);
  329. Lzari.Release();
  330. bytereads+=g_sendhead.length[i];
  331. CArchive ar(&memfile, CArchive::load);
  332. parray[i]->SetSize(g_sendhead.count[i]);
  333. for(int ii=0; ii<parray[i]->GetSize (); ii++)
  334. {
  335. parray[i]->ElementAt (ii).Serialize (ar);
  336. }
  337. ar.Close();
  338. memfile.Detach ();
  339. }
  340. else
  341. {
  342. CMemFile memfile;
  343. memfile.Attach (g_pData+bytereads,g_sendhead.length[i]);
  344. bytereads+=g_sendhead.length[i];
  345. CArchive ar(&memfile, CArchive::load);
  346. parray[i]->SetSize(g_sendhead.count[i]);
  347. for(int ii=0; ii<parray[i]->GetSize (); ii++)
  348. {
  349. parray[i]->ElementAt (ii).Serialize (ar);
  350. }
  351. ar.Close();
  352. memfile.Detach ();
  353. }
  354. }
  355. }