DataSyncSend.cpp 11 KB

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