TestConnIP.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // TestConnIP.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "TestConnIP.h"
  5. #include "TestConnIPDlg.h"
  6. #include <initguid.h>
  7. #include <afxsock.h>//<Winsock2.h>
  8. #include <wincrypt.h>
  9. #include <tlhelp32.h>
  10. #define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
  11. #define KEYLENGTH 0x00800000
  12. #define ENCRYPT_ALGORITHM CALG_RC4
  13. #define ENCRYPT_BLOCK_SIZE 8
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19. CDatabase g_db;
  20. extern CDatabase *g_curdb;
  21. CString g_mainpath;
  22. CString g_server;
  23. CString g_server2;
  24. CString g_localip;
  25. CString g_sbranchname;
  26. CString g_sdomain;
  27. CTestConnIPDlg *g_pMainWnd=NULL;
  28. BOOL g_bSendOK=0;
  29. SENDHEAD g_sendhead;
  30. #pragma comment(lib, "Shlwapi.lib")
  31. extern void WriteLogin(CString str);
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CTestConnIPApp
  34. BEGIN_MESSAGE_MAP(CTestConnIPApp, CWinApp)
  35. //{{AFX_MSG_MAP(CTestConnIPApp)
  36. // NOTE - the ClassWizard will add and remove mapping macros here.
  37. // DO NOT EDIT what you see in these blocks of generated code!
  38. //}}AFX_MSG
  39. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  40. END_MESSAGE_MAP()
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CTestConnIPApp construction
  43. CTestConnIPApp::CTestConnIPApp()
  44. {
  45. // TODO: add construction code here,
  46. // Place all significant initialization in InitInstance
  47. }
  48. /////////////////////////////////////////////////////////////////////////////
  49. // The one and only CTestConnIPApp object
  50. CTestConnIPApp theApp;
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CTestConnIPApp initialization
  53. DWORD FindAppProcessID(CString path)
  54. {
  55. try
  56. {
  57. HANDLE handle=::CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
  58. PROCESSENTRY32 Info;
  59. Info.dwSize = sizeof(PROCESSENTRY32);
  60. path.MakeLower ();
  61. if(::Process32First(handle,&Info))
  62. {
  63. do
  64. {
  65. CString ss=Info.szExeFile;
  66. ss.MakeLower ();
  67. // if(ss.Find (path)!=-1 || (!ss.CompareNoCase(path)) )
  68. if(ss==path)
  69. {
  70. ::CloseHandle(handle);
  71. return Info.th32ProcessID;
  72. }
  73. }
  74. while(::Process32Next(handle,&Info));
  75. ::CloseHandle(handle);
  76. }
  77. return -1;
  78. }
  79. catch(...)
  80. {
  81. }
  82. }
  83. BOOL CTestConnIPApp::InitInstance()
  84. {
  85. HANDLE hObject = CreateMutex(NULL,FALSE,_T("xdCTestConnIPAppXiao"));
  86. if(GetLastError() == ERROR_ALREADY_EXISTS)
  87. {
  88. #ifndef _DEBUG
  89. return false;
  90. #endif
  91. }
  92. char path[MAX_PATH];
  93. ::GetModuleFileName (NULL, path, MAX_PATH);
  94. g_mainpath=path;
  95. g_mainpath=g_mainpath.Left (g_mainpath.ReverseFind ('\\'));
  96. int nResult = Transport_Init();
  97. if( TRANSPORT_OK != nResult)
  98. {
  99. WriteLogin("网络初始化失败!");
  100. return false;
  101. }
  102. // Standard initialization
  103. // If you are not using these features and wish to reduce the size
  104. // of your final executable, you should remove from the following
  105. // the specific initialization routines you do not need.
  106. SetRegistryKey(_T("LYFZ-TestConnIP"));
  107. ::CoInitialize( NULL );
  108. CTestConnIPDlg dlg;
  109. m_pMainWnd = &dlg;
  110. int nResponse = dlg.DoModal();
  111. if (nResponse == IDOK)
  112. {
  113. // TODO: Place code here to handle when the dialog is
  114. // dismissed with OK
  115. }
  116. else if (nResponse == IDCANCEL)
  117. {
  118. // TODO: Place code here to handle when the dialog is
  119. // dismissed with Cancel
  120. }
  121. CoUninitialize();
  122. // Since the dialog has been closed, return FALSE so that we exit the
  123. // application, rather than start the application's message pump.
  124. return FALSE;
  125. }
  126. int CTestConnIPApp::ExitInstance()
  127. {
  128. return CWinApp::ExitInstance();
  129. }
  130. BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword)
  131. {
  132. try
  133. {
  134. HCRYPTPROV hCryptProv;
  135. HCRYPTKEY hKey;
  136. HCRYPTHASH hHash;
  137. PBYTE pbBuffer;
  138. DWORD dwBlockLen;
  139. DWORD dwBufferLen;
  140. DWORD dwCount;
  141. //以下获得一个CSP句柄
  142. if(CryptAcquireContext(
  143. &hCryptProv,
  144. NULL, //NULL表示使用默认密钥容器,默认密钥容器名为用户登陆名
  145. NULL,
  146. PROV_RSA_FULL,
  147. 0))
  148. {
  149. printf("A cryptographic provider has been acquired. \n");
  150. }
  151. else//密钥容器不存在
  152. {
  153. if(CryptAcquireContext(
  154. &hCryptProv,
  155. NULL,
  156. NULL,
  157. PROV_RSA_FULL,
  158. CRYPT_NEWKEYSET))//创建密钥容器
  159. {
  160. //创建密钥容器成功,并得到CSP句柄
  161. printf("A new key container has been created.\n");
  162. }
  163. else
  164. {
  165. return 0;
  166. }
  167. }
  168. //--------------------------------------------------------------------
  169. // 创建一个会话密钥(session key)
  170. // 会话密钥也叫对称密钥,用于对称加密算法。
  171. // (注: 一个Session是指从调用函数CryptAcquireContext到调用函数
  172. // CryptReleaseContext 期间的阶段。)
  173. //--------------------------------------------------------------------
  174. // Create a hash object.
  175. if(CryptCreateHash(
  176. hCryptProv,
  177. CALG_MD5,
  178. 0,
  179. 0,
  180. &hHash))
  181. {
  182. printf("A hash object has been created. \n");
  183. }
  184. else
  185. {
  186. return 0;
  187. }
  188. //--------------------------------------------------------------------
  189. // 用输入的密码产生一个散列
  190. if(CryptHashData(
  191. hHash,
  192. (BYTE *)szPassword,
  193. strlen(szPassword),
  194. 0))
  195. {
  196. printf("The password has been added to the hash. \n");
  197. }
  198. else
  199. {
  200. return 0;
  201. }
  202. //--------------------------------------------------------------------
  203. // 通过散列生成会话密钥(session key)
  204. if(CryptDeriveKey(
  205. hCryptProv,
  206. ENCRYPT_ALGORITHM,
  207. hHash,
  208. KEYLENGTH,
  209. &hKey))
  210. {
  211. printf("An encryption key is derived from the password hash. \n");
  212. }
  213. else
  214. {
  215. return 0;
  216. }
  217. //--------------------------------------------------------------------
  218. // Destroy the hash object.
  219. CryptDestroyHash(hHash);
  220. hHash = NULL;
  221. //--------------------------------------------------------------------
  222. // The session key is now ready.
  223. //--------------------------------------------------------------------
  224. // 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的,所以被加密的
  225. // 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的
  226. // 数据长度。
  227. dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
  228. //--------------------------------------------------------------------
  229. // Determine the block size. If a block cipher is used,
  230. // it must have room for an extra block.
  231. if(ENCRYPT_BLOCK_SIZE > 1)
  232. dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
  233. else
  234. dwBufferLen = dwBlockLen;
  235. dwCount=dwBufferLen;
  236. //--------------------------------------------------------------------
  237. // In a do loop, encrypt the source file and write to the source file.
  238. int count;
  239. // if(leng%dwBlockLen==0)
  240. count=leng/dwBufferLen;
  241. // else
  242. // count=leng/dwBlockLen+1;
  243. for(int i=0; i<count; i++)
  244. {
  245. pbBuffer=buffer+i*dwBufferLen;
  246. // 加密数据
  247. if(!CryptEncrypt(
  248. hKey, //密钥
  249. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  250. 0, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  251. //这里通过判断是否到文件尾来决定是否为最后一块。
  252. 0, //保留
  253. pbBuffer, //输入被加密数据,输出加密后的数据
  254. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  255. dwBufferLen)) //pbBuffer的大小。
  256. {
  257. return 0;
  258. }
  259. }
  260. if(leng%dwBlockLen)
  261. {
  262. pbBuffer=buffer+i*dwBufferLen;
  263. dwCount=leng-i*dwBufferLen;
  264. if(!CryptEncrypt(
  265. hKey, //密钥
  266. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  267. TRUE, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  268. //这里通过判断是否到文件尾来决定是否为最后一块。
  269. 0, //保留
  270. pbBuffer, //输入被加密数据,输出加密后的数据
  271. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  272. dwBufferLen)) //pbBuffer的大小。
  273. {
  274. return 0;
  275. }
  276. }
  277. //--------------------------------------------------------------------
  278. // Destroy session key.
  279. if(hKey)
  280. CryptDestroyKey(hKey);
  281. //--------------------------------------------------------------------
  282. // Destroy hash object.
  283. if(hHash)
  284. CryptDestroyHash(hHash);
  285. //--------------------------------------------------------------------
  286. // Release provider handle.
  287. if(hCryptProv)
  288. CryptReleaseContext(hCryptProv, 0);
  289. return(TRUE);
  290. }
  291. catch(...)
  292. {
  293. }
  294. }