SendDataDlg.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // SendDataDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ylgl.h"
  5. #include "SendDataDlg.h"
  6. #include "ConnectDlg.h"
  7. #include <wincrypt.h>
  8. #define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
  9. #define KEYLENGTH 0x00800000
  10. #define ENCRYPT_ALGORITHM CALG_RC4
  11. #define ENCRYPT_BLOCK_SIZE 8
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17. HWND g_hSendWnd=NULL;
  18. /////////////////////////////////////////////////////////////////////////////
  19. // SendDataDlg dialog
  20. SendDataDlg::SendDataDlg(CWnd* pParent /*=NULL*/)
  21. : CDialog(SendDataDlg::IDD, pParent)
  22. {
  23. //{{AFX_DATA_INIT(SendDataDlg)
  24. // NOTE: the ClassWizard will add member initialization here
  25. //}}AFX_DATA_INIT
  26. m_pData=NULL;
  27. m_trytimes=0;
  28. m_pBk=NULL;
  29. }
  30. void SendDataDlg::DoDataExchange(CDataExchange* pDX)
  31. {
  32. CDialog::DoDataExchange(pDX);
  33. //{{AFX_DATA_MAP(SendDataDlg)
  34. // NOTE: the ClassWizard will add DDX and DDV calls here
  35. //}}AFX_DATA_MAP
  36. }
  37. BEGIN_MESSAGE_MAP(SendDataDlg, CDialog)
  38. //{{AFX_MSG_MAP(SendDataDlg)
  39. ON_WM_TIMER()
  40. ON_WM_DESTROY()
  41. ON_WM_CLOSE()
  42. ON_WM_PAINT()
  43. ON_WM_ERASEBKGND()
  44. //}}AFX_MSG_MAP
  45. END_MESSAGE_MAP()
  46. /////////////////////////////////////////////////////////////////////////////
  47. // SendDataDlg message handlers
  48. BOOL SendDataDlg::OnEraseBkgnd(CDC* pDC)
  49. {
  50. // TODO: Add your message handler code here and/or call default
  51. #ifdef NEW_SKIN
  52. return 1;
  53. #else
  54. return CDialog::OnEraseBkgnd (pDC);
  55. #endif
  56. }
  57. void SendDataDlg::OnOK()
  58. {
  59. // TODO: Add extra validation here
  60. }
  61. void SendDataDlg::OnCancel()
  62. {
  63. // TODO: Add extra cleanup here
  64. }
  65. BOOL SendDataDlg::OnInitDialog()
  66. {
  67. CDialog::OnInitDialog();// CenterWindow(g_pMainWnd);
  68. m_nconncot=0;
  69. if(g_bNoConnDlg)
  70. {
  71. SetWindowPos(&wndNoTopMost,0,0,0,0,SWP_HIDEWINDOW);
  72. ModifyStyleEx(WS_EX_APPWINDOW,WS_EX_TOOLWINDOW);
  73. }
  74. #ifdef NEW_SKIN
  75. if(::PathFileExists (g_mainpath+"\\图片\\连接.jpg") && g_bNoConnDlg==0)
  76. ::LoadImageFromBuf (&m_pBk, g_mainpath+"\\图片\\连接.jpg");
  77. #endif
  78. if(m_pBk)
  79. {
  80. GetDlgItem(IDC_STATIC1)->ShowWindow(SW_HIDE);
  81. CRect rc;
  82. GetWindowRect(rc);
  83. rc.right =rc.left +m_pBk->GetWidth();
  84. rc.bottom =rc.top +m_pBk->GetHeight();
  85. MoveWindow(rc);
  86. CenterWindow();
  87. GetClientRect(rc);
  88. CRgn rgn1, rgn2, rgn3;
  89. rgn1.CreateRectRgn (0,0, rc.Width (), rc.Height ());
  90. rgn2.CreateRectRgn (0,0, 6, 6);
  91. rgn3.CreateEllipticRgn(0,0,12,12);
  92. rgn2.CombineRgn (&rgn2, &rgn3, RGN_DIFF);
  93. rgn1.CombineRgn (&rgn1, &rgn2, RGN_DIFF);
  94. rgn2.DeleteObject ();
  95. rgn3.DeleteObject ();
  96. rgn2.CreateRectRgn (rc.Width ()-6,0, rc.Width (), 6);
  97. rgn3.CreateEllipticRgn(rc.Width ()-12,0,rc.Width (),12);
  98. rgn2.CombineRgn (&rgn2, &rgn3, RGN_DIFF);
  99. rgn1.CombineRgn (&rgn1, &rgn2, RGN_DIFF);
  100. rgn2.DeleteObject ();
  101. rgn3.DeleteObject ();
  102. ::SetWindowRgn (m_hWnd, rgn1, 0);
  103. }
  104. g_bSendOK=1;
  105. g_hSendWnd=m_hWnd;
  106. SetTimer(1, 100, NULL);
  107. CenterWindow();
  108. return TRUE;
  109. }
  110. extern HWND g_hConnectWnd;
  111. BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword)
  112. {
  113. try
  114. {
  115. HCRYPTPROV hCryptProv;
  116. HCRYPTKEY hKey;
  117. HCRYPTHASH hHash;
  118. PBYTE pbBuffer;
  119. DWORD dwBlockLen;
  120. DWORD dwBufferLen;
  121. DWORD dwCount;
  122. //以下获得一个CSP句柄
  123. if(CryptAcquireContext(
  124. &hCryptProv,
  125. NULL, //NULL表示使用默认密钥容器,默认密钥容器名为用户登陆名
  126. NULL,
  127. PROV_RSA_FULL,
  128. 0))
  129. {
  130. printf("A cryptographic provider has been acquired. \n");
  131. }
  132. else//密钥容器不存在
  133. {
  134. if(CryptAcquireContext(
  135. &hCryptProv,
  136. NULL,
  137. NULL,
  138. PROV_RSA_FULL,
  139. CRYPT_NEWKEYSET))//创建密钥容器
  140. {
  141. //创建密钥容器成功,并得到CSP句柄
  142. printf("A new key container has been created.\n");
  143. }
  144. else
  145. {
  146. return 0;
  147. }
  148. }
  149. //--------------------------------------------------------------------
  150. // 创建一个会话密钥(session key)
  151. // 会话密钥也叫对称密钥,用于对称加密算法。
  152. // (注: 一个Session是指从调用函数CryptAcquireContext到调用函数
  153. // CryptReleaseContext 期间的阶段。)
  154. //--------------------------------------------------------------------
  155. // Create a hash object.
  156. if(CryptCreateHash(
  157. hCryptProv,
  158. CALG_MD5,
  159. 0,
  160. 0,
  161. &hHash))
  162. {
  163. printf("A hash object has been created. \n");
  164. }
  165. else
  166. {
  167. return 0;
  168. }
  169. //--------------------------------------------------------------------
  170. // 用输入的密码产生一个散列
  171. if(CryptHashData(
  172. hHash,
  173. (BYTE *)szPassword,
  174. strlen(szPassword),
  175. 0))
  176. {
  177. printf("The password has been added to the hash. \n");
  178. }
  179. else
  180. {
  181. return 0;
  182. }
  183. //--------------------------------------------------------------------
  184. // 通过散列生成会话密钥(session key)
  185. if(CryptDeriveKey(
  186. hCryptProv,
  187. ENCRYPT_ALGORITHM,
  188. hHash,
  189. KEYLENGTH,
  190. &hKey))
  191. {
  192. printf("An encryption key is derived from the password hash. \n");
  193. }
  194. else
  195. {
  196. return 0;
  197. }
  198. //--------------------------------------------------------------------
  199. // Destroy the hash object.
  200. CryptDestroyHash(hHash);
  201. hHash = NULL;
  202. //--------------------------------------------------------------------
  203. // The session key is now ready.
  204. //--------------------------------------------------------------------
  205. // 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的,所以被加密的
  206. // 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的
  207. // 数据长度。
  208. dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
  209. //--------------------------------------------------------------------
  210. // Determine the block size. If a block cipher is used,
  211. // it must have room for an extra block.
  212. if(ENCRYPT_BLOCK_SIZE > 1)
  213. dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
  214. else
  215. dwBufferLen = dwBlockLen;
  216. dwCount=dwBufferLen;
  217. //--------------------------------------------------------------------
  218. // In a do loop, encrypt the source file and write to the source file.
  219. int count;
  220. // if(leng%dwBlockLen==0)
  221. count=leng/dwBufferLen;
  222. // else
  223. // count=leng/dwBlockLen+1;
  224. int i = 0;
  225. for( i=0; i<count; i++)
  226. {
  227. pbBuffer=buffer+i*dwBufferLen;
  228. // 加密数据
  229. if(!CryptEncrypt(
  230. hKey, //密钥
  231. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  232. 0, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  233. //这里通过判断是否到文件尾来决定是否为最后一块。
  234. 0, //保留
  235. pbBuffer, //输入被加密数据,输出加密后的数据
  236. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  237. dwBufferLen)) //pbBuffer的大小。
  238. {
  239. return 0;
  240. }
  241. }
  242. if(leng%dwBlockLen)
  243. {
  244. pbBuffer=buffer+i*dwBufferLen;
  245. dwCount=leng-i*dwBufferLen;
  246. if(!CryptEncrypt(
  247. hKey, //密钥
  248. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  249. TRUE, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  250. //这里通过判断是否到文件尾来决定是否为最后一块。
  251. 0, //保留
  252. pbBuffer, //输入被加密数据,输出加密后的数据
  253. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  254. dwBufferLen)) //pbBuffer的大小。
  255. {
  256. return 0;
  257. }
  258. }
  259. //--------------------------------------------------------------------
  260. // Destroy session key.
  261. if(hKey)
  262. CryptDestroyKey(hKey);
  263. //--------------------------------------------------------------------
  264. // Destroy hash object.
  265. if(hHash)
  266. CryptDestroyHash(hHash);
  267. //--------------------------------------------------------------------
  268. // Release provider handle.
  269. if(hCryptProv)
  270. CryptReleaseContext(hCryptProv, 0);
  271. return(TRUE);
  272. }
  273. catch(...)
  274. {
  275. }
  276. }
  277. void SendDataDlg::OnTimer(UINT nIDEvent)
  278. {
  279. if(nIDEvent==1)
  280. {
  281. KillTimer(nIDEvent);
  282. int nSendCodetemp=g_nSendCode;
  283. ll: if(g_bNeedCoon) // Jeff.g_bNeedCoon全局初始值==1;
  284. {
  285. m_nconncot++; // Jeff.连接的次数;
  286. if(m_nconncot>2)
  287. { // MessageBox("连接失败!!!");
  288. g_pMainWnd->KillTimer (2);
  289. g_pMainWnd->SetTimer (2, 10, NULL);
  290. return;
  291. }
  292. CConnectDlg dlg;
  293. dlg.DoModal();
  294. if(g_bSendOK)
  295. {
  296. //printf("Jeff:SendDataDlg::连接成功,g_conntype=%d\n\n",g_conntype);//Jeff Printf;
  297. g_bNeedCoon=0;
  298. if(g_conntype)
  299. {
  300. // g_server=g_serverbak;
  301. // MessageBox("获取ipxx"+g_server);
  302. // LOG4C((LOG_NOTICE,"g_server=%s",g_server));
  303. BYTE *pData=new BYTE[g_server.GetLength ()];
  304. memcpy(pData, g_server.GetBuffer (0), g_server.GetLength ());
  305. g_server.ReleaseBuffer ();
  306. EncryptFile2(pData, g_server.GetLength (), "888666333");
  307. g_nSendCode=7;
  308. if(g_pMainWnd->ProcessChatMessageRequest(pData, g_server.GetLength ())==0)
  309. { // MessageBox("xxxxw");
  310. //LOG4C((LOG_NOTICE,"连接失败"));
  311. delete []pData;
  312. g_nSendCode=0;
  313. g_bNeedCoon=1;
  314. goto ll;
  315. }
  316. /* int aa=100;
  317. while(g_pMainWnd->m_dwConnectionID==INVALID_SOCKET)
  318. {
  319. ::Sleep (50);
  320. aa--;
  321. if(aa==0)break;
  322. }*/
  323. delete []pData;
  324. g_nSendCode=0;
  325. g_bNeedCoon=1;
  326. int aa=200;
  327. while(g_conntype)
  328. {
  329. ::Sleep (50);
  330. aa--;
  331. if(aa==0)break;
  332. }
  333. goto ll;
  334. return;
  335. }
  336. int aa=100;
  337. while(g_pMainWnd->m_dwConnectionID==INVALID_SOCKET)
  338. {
  339. ::Sleep (50);
  340. aa--;
  341. if(aa==0)break;
  342. }
  343. }
  344. else
  345. {
  346. // MessageBox("连接失败ccc");
  347. // LOG4C((LOG_NOTICE,"连接失败"));
  348. //printf("Jeff:SendDataDlg::连接失败,g_conntype=%d\n\n",g_conntype);
  349. g_bNeedCoon=1;
  350. goto ll;//2014.3.6
  351. return;
  352. }
  353. }
  354. g_nSendCode=nSendCodetemp;
  355. if(m_pData)
  356. {
  357. // LOG4C((LOG_NOTICE,"m_pData != NULL"));
  358. if(g_pMainWnd->ProcessChatMessageRequest((void*)m_pData, m_nlength)==0)
  359. {
  360. g_bNeedCoon=1;
  361. goto ll;
  362. }
  363. }
  364. else if(m_strSQL.IsEmpty ())
  365. {
  366. // LOG4C((LOG_NOTICE,"m_strSQL==NULL"));
  367. if(g_pMainWnd->ProcessChatMessageRequest(&m_bycode, sizeof(BYTE))==0)
  368. {
  369. g_bNeedCoon=1;
  370. goto ll;
  371. }
  372. }
  373. else
  374. {
  375. // LOG4C((LOG_NOTICE,"m_strSQL != NULL"));
  376. const char *szDataBuf = m_strSQL.GetBuffer(0);
  377. m_strSQL.ReleaseBuffer();
  378. if(g_pMainWnd->ProcessChatMessageRequest((void*)szDataBuf, strlen(szDataBuf) + 1)==0)
  379. {
  380. g_bNeedCoon=1;
  381. goto ll;
  382. }
  383. }
  384. g_bSendOK=1;
  385. }
  386. }
  387. void SendDataDlg::OnDestroy()
  388. {
  389. g_hSendWnd=NULL;
  390. if(m_pBk)
  391. delete m_pBk;
  392. CDialog::OnDestroy();
  393. }
  394. void SendDataDlg::OnClose()
  395. {
  396. // MessageBox("close message");
  397. CDialog::OnCancel ();
  398. g_hSendWnd=NULL;
  399. }
  400. void SendDataDlg::OnPaint()
  401. {
  402. CPaintDC dc(this); // device context for painting
  403. if(m_pBk)
  404. {
  405. Graphics graph(dc.GetSafeHdc ());
  406. CRect rc;
  407. GetClientRect (rc);
  408. Rect destinationRect(0,0,rc.Width (), rc.Height ());
  409. graph.DrawImage(m_pBk, destinationRect, rc.left , rc.top ,rc.Width (), rc.Height (),UnitPixel);
  410. }
  411. }