123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- // TestConnIP.cpp : Defines the class behaviors for the application.
- //
- #include "stdafx.h"
- #include "TestConnIP.h"
- #include "TestConnIPDlg.h"
- #include <initguid.h>
- #include <afxsock.h>//<Winsock2.h>
- #include <wincrypt.h>
- #include <tlhelp32.h>
- #define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
- #define KEYLENGTH 0x00800000
- #define ENCRYPT_ALGORITHM CALG_RC4
- #define ENCRYPT_BLOCK_SIZE 8
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- CDatabase g_db;
- extern CDatabase *g_curdb;
- CString g_mainpath;
- CString g_server;
- CString g_server2;
- CString g_localip;
- CString g_sbranchname;
- CString g_sdomain;
- CTestConnIPDlg *g_pMainWnd=NULL;
- BOOL g_bSendOK=0;
- SENDHEAD g_sendhead;
- #pragma comment(lib, "Shlwapi.lib")
- extern void WriteLogin(CString str);
- /////////////////////////////////////////////////////////////////////////////
- // CTestConnIPApp
- BEGIN_MESSAGE_MAP(CTestConnIPApp, CWinApp)
- //{{AFX_MSG_MAP(CTestConnIPApp)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- // DO NOT EDIT what you see in these blocks of generated code!
- //}}AFX_MSG
- ON_COMMAND(ID_HELP, CWinApp::OnHelp)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTestConnIPApp construction
- CTestConnIPApp::CTestConnIPApp()
- {
- // TODO: add construction code here,
- // Place all significant initialization in InitInstance
- }
- /////////////////////////////////////////////////////////////////////////////
- // The one and only CTestConnIPApp object
- CTestConnIPApp theApp;
- /////////////////////////////////////////////////////////////////////////////
- // CTestConnIPApp initialization
- DWORD FindAppProcessID(CString path)
- {
- try
- {
- HANDLE handle=::CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
- PROCESSENTRY32 Info;
- Info.dwSize = sizeof(PROCESSENTRY32);
- path.MakeLower ();
- if(::Process32First(handle,&Info))
- {
- do
- {
- CString ss=Info.szExeFile;
- ss.MakeLower ();
- // if(ss.Find (path)!=-1 || (!ss.CompareNoCase(path)) )
- if(ss==path)
- {
- ::CloseHandle(handle);
- return Info.th32ProcessID;
- }
- }
- while(::Process32Next(handle,&Info));
- ::CloseHandle(handle);
- }
- return -1;
- }
- catch(...)
- {
- }
- }
- BOOL CTestConnIPApp::InitInstance()
- {
- HANDLE hObject = CreateMutex(NULL,FALSE,_T("xdCTestConnIPAppXiao"));
- if(GetLastError() == ERROR_ALREADY_EXISTS)
- {
- #ifndef _DEBUG
- return false;
- #endif
- }
- char path[MAX_PATH];
- ::GetModuleFileName (NULL, path, MAX_PATH);
- g_mainpath=path;
- g_mainpath=g_mainpath.Left (g_mainpath.ReverseFind ('\\'));
- int nResult = Transport_Init();
- if( TRANSPORT_OK != nResult)
- {
- WriteLogin("网络初始化失败!");
- return false;
- }
-
-
- // Standard initialization
- // If you are not using these features and wish to reduce the size
- // of your final executable, you should remove from the following
- // the specific initialization routines you do not need.
- SetRegistryKey(_T("LYFZ-TestConnIP"));
- ::CoInitialize( NULL );
- CTestConnIPDlg dlg;
- m_pMainWnd = &dlg;
- int nResponse = dlg.DoModal();
- if (nResponse == IDOK)
- {
- // TODO: Place code here to handle when the dialog is
- // dismissed with OK
- }
- else if (nResponse == IDCANCEL)
- {
- // TODO: Place code here to handle when the dialog is
- // dismissed with Cancel
- }
- CoUninitialize();
- // Since the dialog has been closed, return FALSE so that we exit the
- // application, rather than start the application's message pump.
- return FALSE;
- }
- int CTestConnIPApp::ExitInstance()
- {
- return CWinApp::ExitInstance();
- }
- BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword)
- {
- try
- {
- HCRYPTPROV hCryptProv;
- HCRYPTKEY hKey;
- HCRYPTHASH hHash;
- PBYTE pbBuffer;
- DWORD dwBlockLen;
- DWORD dwBufferLen;
- DWORD dwCount;
-
- //以下获得一个CSP句柄
- if(CryptAcquireContext(
- &hCryptProv,
- NULL, //NULL表示使用默认密钥容器,默认密钥容器名为用户登陆名
- NULL,
- PROV_RSA_FULL,
- 0))
- {
- printf("A cryptographic provider has been acquired. \n");
- }
- else//密钥容器不存在
- {
- if(CryptAcquireContext(
- &hCryptProv,
- NULL,
- NULL,
- PROV_RSA_FULL,
- CRYPT_NEWKEYSET))//创建密钥容器
- {
- //创建密钥容器成功,并得到CSP句柄
- printf("A new key container has been created.\n");
- }
- else
- {
- return 0;
- }
-
- }
- //--------------------------------------------------------------------
- // 创建一个会话密钥(session key)
- // 会话密钥也叫对称密钥,用于对称加密算法。
- // (注: 一个Session是指从调用函数CryptAcquireContext到调用函数
- // CryptReleaseContext 期间的阶段。)
- //--------------------------------------------------------------------
- // Create a hash object.
- if(CryptCreateHash(
- hCryptProv,
- CALG_MD5,
- 0,
- 0,
- &hHash))
- {
- printf("A hash object has been created. \n");
- }
- else
- {
- return 0;
- }
- //--------------------------------------------------------------------
- // 用输入的密码产生一个散列
- if(CryptHashData(
- hHash,
- (BYTE *)szPassword,
- strlen(szPassword),
- 0))
- {
- printf("The password has been added to the hash. \n");
- }
- else
- {
- return 0;
- }
- //--------------------------------------------------------------------
- // 通过散列生成会话密钥(session key)
- if(CryptDeriveKey(
- hCryptProv,
- ENCRYPT_ALGORITHM,
- hHash,
- KEYLENGTH,
- &hKey))
- {
- printf("An encryption key is derived from the password hash. \n");
- }
- else
- {
- return 0;
- }
- //--------------------------------------------------------------------
- // Destroy the hash object.
-
- CryptDestroyHash(hHash);
- hHash = NULL;
-
- //--------------------------------------------------------------------
- // The session key is now ready.
-
- //--------------------------------------------------------------------
- // 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的,所以被加密的
- // 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的
- // 数据长度。
- dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
-
- //--------------------------------------------------------------------
- // Determine the block size. If a block cipher is used,
- // it must have room for an extra block.
-
- if(ENCRYPT_BLOCK_SIZE > 1)
- dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
- else
- dwBufferLen = dwBlockLen;
- dwCount=dwBufferLen;
- //--------------------------------------------------------------------
- // In a do loop, encrypt the source file and write to the source file.
- int count;
- // if(leng%dwBlockLen==0)
- count=leng/dwBufferLen;
- // else
- // count=leng/dwBlockLen+1;
- for(int i=0; i<count; i++)
- {
- pbBuffer=buffer+i*dwBufferLen;
- // 加密数据
- if(!CryptEncrypt(
- hKey, //密钥
- 0, //如果数据同时进行散列和加密,这里传入一个散列对象
- 0, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
- //这里通过判断是否到文件尾来决定是否为最后一块。
- 0, //保留
- pbBuffer, //输入被加密数据,输出加密后的数据
- &dwCount, //输入被加密数据实际长度,输出加密后数据长度
- dwBufferLen)) //pbBuffer的大小。
- {
- return 0;
- }
- }
- if(leng%dwBlockLen)
- {
- pbBuffer=buffer+i*dwBufferLen;
- dwCount=leng-i*dwBufferLen;
- if(!CryptEncrypt(
- hKey, //密钥
- 0, //如果数据同时进行散列和加密,这里传入一个散列对象
- TRUE, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
- //这里通过判断是否到文件尾来决定是否为最后一块。
- 0, //保留
- pbBuffer, //输入被加密数据,输出加密后的数据
- &dwCount, //输入被加密数据实际长度,输出加密后数据长度
- dwBufferLen)) //pbBuffer的大小。
- {
- return 0;
- }
- }
-
-
- //--------------------------------------------------------------------
- // Destroy session key.
-
- if(hKey)
- CryptDestroyKey(hKey);
-
- //--------------------------------------------------------------------
- // Destroy hash object.
-
- if(hHash)
- CryptDestroyHash(hHash);
-
- //--------------------------------------------------------------------
- // Release provider handle.
-
- if(hCryptProv)
- CryptReleaseContext(hCryptProv, 0);
- return(TRUE);
- }
- catch(...)
- {
- }
- }
|