SendDataDlg.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. for(int i=0; i<count; i++)
  225. {
  226. pbBuffer=buffer+i*dwBufferLen;
  227. // 加密数据
  228. if(!CryptEncrypt(
  229. hKey, //密钥
  230. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  231. 0, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  232. //这里通过判断是否到文件尾来决定是否为最后一块。
  233. 0, //保留
  234. pbBuffer, //输入被加密数据,输出加密后的数据
  235. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  236. dwBufferLen)) //pbBuffer的大小。
  237. {
  238. return 0;
  239. }
  240. }
  241. if(leng%dwBlockLen)
  242. {
  243. pbBuffer=buffer+i*dwBufferLen;
  244. dwCount=leng-i*dwBufferLen;
  245. if(!CryptEncrypt(
  246. hKey, //密钥
  247. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  248. TRUE, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  249. //这里通过判断是否到文件尾来决定是否为最后一块。
  250. 0, //保留
  251. pbBuffer, //输入被加密数据,输出加密后的数据
  252. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  253. dwBufferLen)) //pbBuffer的大小。
  254. {
  255. return 0;
  256. }
  257. }
  258. //--------------------------------------------------------------------
  259. // Destroy session key.
  260. if(hKey)
  261. CryptDestroyKey(hKey);
  262. //--------------------------------------------------------------------
  263. // Destroy hash object.
  264. if(hHash)
  265. CryptDestroyHash(hHash);
  266. //--------------------------------------------------------------------
  267. // Release provider handle.
  268. if(hCryptProv)
  269. CryptReleaseContext(hCryptProv, 0);
  270. return(TRUE);
  271. }
  272. catch(...)
  273. {
  274. }
  275. }
  276. void SendDataDlg::OnTimer(UINT nIDEvent)
  277. {
  278. if(nIDEvent==1)
  279. {
  280. KillTimer(nIDEvent);
  281. int nSendCodetemp=g_nSendCode;
  282. ll: if(g_bNeedCoon) // Jeff.g_bNeedCoon全局初始值==1;
  283. {
  284. m_nconncot++; // Jeff.连接的次数;
  285. if(m_nconncot>2)
  286. { // MessageBox("连接失败!!!");
  287. g_pMainWnd->KillTimer (2);
  288. g_pMainWnd->SetTimer (2, 10, NULL);
  289. return;
  290. }
  291. CConnectDlg dlg;
  292. dlg.DoModal();
  293. if(g_bSendOK)
  294. {
  295. //printf("Jeff:SendDataDlg::连接成功,g_conntype=%d\n\n",g_conntype);//Jeff Printf;
  296. g_bNeedCoon=0;
  297. if(g_conntype)
  298. {
  299. // g_server=g_serverbak;
  300. // MessageBox("获取ipxx"+g_server);
  301. // LOG4C((LOG_NOTICE,"g_server=%s",g_server));
  302. BYTE *pData=new BYTE[g_server.GetLength ()];
  303. memcpy(pData, g_server.GetBuffer (0), g_server.GetLength ());
  304. g_server.ReleaseBuffer ();
  305. EncryptFile2(pData, g_server.GetLength (), "888666333");
  306. g_nSendCode=7;
  307. if(g_pMainWnd->ProcessChatMessageRequest(pData, g_server.GetLength ())==0)
  308. { // MessageBox("xxxxw");
  309. //LOG4C((LOG_NOTICE,"连接失败"));
  310. delete []pData;
  311. g_nSendCode=0;
  312. g_bNeedCoon=1;
  313. goto ll;
  314. }
  315. /* int aa=100;
  316. while(g_pMainWnd->m_dwConnectionID==INVALID_SOCKET)
  317. {
  318. ::Sleep (50);
  319. aa--;
  320. if(aa==0)break;
  321. }*/
  322. delete []pData;
  323. g_nSendCode=0;
  324. g_bNeedCoon=1;
  325. int aa=200;
  326. while(g_conntype)
  327. {
  328. ::Sleep (50);
  329. aa--;
  330. if(aa==0)break;
  331. }
  332. goto ll;
  333. return;
  334. }
  335. int aa=100;
  336. while(g_pMainWnd->m_dwConnectionID==INVALID_SOCKET)
  337. {
  338. ::Sleep (50);
  339. aa--;
  340. if(aa==0)break;
  341. }
  342. }
  343. else
  344. {
  345. // MessageBox("连接失败ccc");
  346. // LOG4C((LOG_NOTICE,"连接失败"));
  347. //printf("Jeff:SendDataDlg::连接失败,g_conntype=%d\n\n",g_conntype);
  348. g_bNeedCoon=1;
  349. goto ll;//2014.3.6
  350. return;
  351. }
  352. }
  353. g_nSendCode=nSendCodetemp;
  354. if(m_pData)
  355. {
  356. // LOG4C((LOG_NOTICE,"m_pData != NULL"));
  357. if(g_pMainWnd->ProcessChatMessageRequest((void*)m_pData, m_nlength)==0)
  358. {
  359. g_bNeedCoon=1;
  360. goto ll;
  361. }
  362. }
  363. else if(m_strSQL.IsEmpty ())
  364. {
  365. // LOG4C((LOG_NOTICE,"m_strSQL==NULL"));
  366. if(g_pMainWnd->ProcessChatMessageRequest(&m_bycode, sizeof(BYTE))==0)
  367. {
  368. g_bNeedCoon=1;
  369. goto ll;
  370. }
  371. }
  372. else
  373. {
  374. // LOG4C((LOG_NOTICE,"m_strSQL != NULL"));
  375. const char *szDataBuf = m_strSQL.GetBuffer(0);
  376. m_strSQL.ReleaseBuffer();
  377. if(g_pMainWnd->ProcessChatMessageRequest((void*)szDataBuf, strlen(szDataBuf) + 1)==0)
  378. {
  379. g_bNeedCoon=1;
  380. goto ll;
  381. }
  382. }
  383. g_bSendOK=1;
  384. }
  385. }
  386. void SendDataDlg::OnDestroy()
  387. {
  388. g_hSendWnd=NULL;
  389. if(m_pBk)
  390. delete m_pBk;
  391. CDialog::OnDestroy();
  392. }
  393. void SendDataDlg::OnClose()
  394. {
  395. // MessageBox("close message");
  396. CDialog::OnCancel ();
  397. g_hSendWnd=NULL;
  398. }
  399. void SendDataDlg::OnPaint()
  400. {
  401. CPaintDC dc(this); // device context for painting
  402. if(m_pBk)
  403. {
  404. Graphics graph(dc.GetSafeHdc ());
  405. CRect rc;
  406. GetClientRect (rc);
  407. Rect destinationRect(0,0,rc.Width (), rc.Height ());
  408. graph.DrawImage(m_pBk, destinationRect, rc.left , rc.top ,rc.Width (), rc.Height (),UnitPixel);
  409. }
  410. }