123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- #include "stdafx.h"
- #include "ylgl.h"
- #include "SendDataDlg.h"
- #include "ConnectDlg.h"
- #include <wincrypt.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
- HWND g_hSendWnd = NULL;
- SendDataDlg::SendDataDlg(CWnd* pParent )
- : CDialog(SendDataDlg::IDD, pParent)
- {
-
-
-
- m_pData = NULL;
- m_trytimes = 0;
- m_pBk = NULL;
- }
- void SendDataDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
-
-
-
- }
- BEGIN_MESSAGE_MAP(SendDataDlg, CDialog)
-
- ON_WM_TIMER()
- ON_WM_DESTROY()
- ON_WM_CLOSE()
- ON_WM_PAINT()
- ON_WM_ERASEBKGND()
-
- END_MESSAGE_MAP()
- BOOL SendDataDlg::OnEraseBkgnd(CDC* pDC)
- {
-
- #ifdef NEW_SKIN
- return 1;
- #else
- return CDialog::OnEraseBkgnd (pDC);
- #endif
- }
- void SendDataDlg::OnOK()
- {
-
- }
- void SendDataDlg::OnCancel()
- {
-
- }
- BOOL SendDataDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- m_nconncot = 0;
- if (g_bNoConnDlg)
- {
- SetWindowPos(&wndNoTopMost, 0, 0, 0, 0, SWP_HIDEWINDOW);
- ModifyStyleEx(WS_EX_APPWINDOW, WS_EX_TOOLWINDOW);
- }
- #ifdef NEW_SKIN
- if (PathFileExists(g_mainpath + "\\图片\\连接.jpg") && g_bNoConnDlg == 0)
- ::LoadImageFromBuf(&m_pBk, g_mainpath + "\\图片\\连接.jpg");
- #endif
- if (m_pBk)
- {
- GetDlgItem(IDC_STATIC1)->ShowWindow(SW_HIDE);
- CRect rc;
- GetWindowRect(rc);
- rc.right = rc.left + m_pBk->GetWidth();
- rc.bottom = rc.top + m_pBk->GetHeight();
- MoveWindow(rc);
- CenterWindow();
- GetClientRect(rc);
- CRgn rgn1, rgn2, rgn3;
- rgn1.CreateRectRgn(0, 0, rc.Width(), rc.Height());
- rgn2.CreateRectRgn(0, 0, 6, 6);
- rgn3.CreateEllipticRgn(0, 0, 12, 12);
- rgn2.CombineRgn(&rgn2, &rgn3, RGN_DIFF);
- rgn1.CombineRgn(&rgn1, &rgn2, RGN_DIFF);
- rgn2.DeleteObject();
- rgn3.DeleteObject();
- rgn2.CreateRectRgn(rc.Width() - 6, 0, rc.Width(), 6);
- rgn3.CreateEllipticRgn(rc.Width() - 12, 0, rc.Width(), 12);
- rgn2.CombineRgn(&rgn2, &rgn3, RGN_DIFF);
- rgn1.CombineRgn(&rgn1, &rgn2, RGN_DIFF);
- rgn2.DeleteObject();
- rgn3.DeleteObject();
- ::SetWindowRgn(m_hWnd, rgn1, 0);
- }
- g_bSendOK = 1;
- g_hSendWnd = m_hWnd;
- SetTimer(1, 100, NULL);
- CenterWindow();
- return TRUE;
- }
- extern HWND g_hConnectWnd;
- BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword)
- {
- try
- {
- HCRYPTPROV hCryptProv;
- HCRYPTKEY hKey;
- HCRYPTHASH hHash;
- PBYTE pbBuffer;
- DWORD dwBlockLen;
- DWORD dwBufferLen;
- DWORD dwCount;
-
- if (CryptAcquireContext(
- &hCryptProv,
- 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))
- {
-
- printf("A new key container has been created.\n");
- }
- else
- {
- return 0;
- }
- }
-
-
-
-
-
-
-
- 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;
- }
-
-
- if (CryptDeriveKey(
- hCryptProv,
- ENCRYPT_ALGORITHM,
- hHash,
- KEYLENGTH,
- &hKey))
- {
- printf("An encryption key is derived from the password hash. \n");
- }
- else
- {
- return 0;
- }
-
-
- CryptDestroyHash(hHash);
- hHash = NULL;
-
-
-
-
-
-
- dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
-
-
-
- if (ENCRYPT_BLOCK_SIZE > 1)
- dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
- else
- dwBufferLen = dwBlockLen;
- dwCount = dwBufferLen;
-
-
- int count;
-
- count = leng / dwBufferLen;
-
-
- for (int i = 0; i < count; i++)
- {
- pbBuffer = buffer + i*dwBufferLen;
-
- if (!CryptEncrypt(
- hKey,
- 0,
- 0,
-
- 0,
- pbBuffer,
- &dwCount,
- dwBufferLen))
- {
- return 0;
- }
- }
- if (leng%dwBlockLen)
- {
- pbBuffer = buffer + i*dwBufferLen;
- dwCount = leng - i*dwBufferLen;
- if (!CryptEncrypt(
- hKey,
- 0,
- TRUE,
-
- 0,
- pbBuffer,
- &dwCount,
- dwBufferLen))
- {
- return 0;
- }
- }
-
-
- if (hKey)
- CryptDestroyKey(hKey);
-
-
- if (hHash)
- CryptDestroyHash(hHash);
-
-
- if (hCryptProv)
- CryptReleaseContext(hCryptProv, 0);
- return(TRUE);
- }
- catch (...)
- {
- }
- }
- void SendDataDlg::OnTimer(UINT nIDEvent)
- {
- if (nIDEvent == 1)
- {
- KillTimer(nIDEvent);
- int nSendCodetemp = g_nSendCode;
- ll: if (g_bNeedCoon)
- {
- m_nconncot++;
- if (m_nconncot > 2)
- {
- g_pMainWnd->KillTimer(2);
- g_pMainWnd->SetTimer(2, 10, NULL);
- return;
- }
- CConnectDlg dlg;
- dlg.DoModal();
- if (g_bSendOK)
- {
-
- g_bNeedCoon = 0;
- if (g_conntype)
- {
-
-
-
- BYTE *pData = new BYTE[g_server.GetLength()];
- memcpy(pData, g_server.GetBuffer(0), g_server.GetLength());
- g_server.ReleaseBuffer();
- EncryptFile2(pData, g_server.GetLength(), "888666333");
- g_nSendCode = 7;
- if (g_pMainWnd->ProcessChatMessageRequest(pData, g_server.GetLength()) == 0)
- {
-
- delete[]pData;
- g_nSendCode = 0;
- g_bNeedCoon = 1;
- goto ll;
- }
-
- delete[]pData;
- g_nSendCode = 0;
- g_bNeedCoon = 1;
- int aa = 200;
- while (g_conntype)
- {
- ::Sleep(50);
- aa--;
- if (aa == 0)break;
- }
- goto ll;
- return;
- }
- int aa = 100;
- while (g_pMainWnd->m_dwConnectionID == INVALID_SOCKET)
- {
- ::Sleep(50);
- aa--;
- if (aa == 0)break;
- }
- }
- else
- {
-
-
-
- g_bNeedCoon = 1;
- goto ll;
- return;
- }
- }
- g_nSendCode = nSendCodetemp;
- if (m_pData)
- {
-
-
- if (g_pMainWnd->ProcessChatMessageRequest((void*)m_pData, m_nlength) == 0)
- {
-
- g_bNeedCoon = 1;
- goto ll;
- }
-
- }
- else if (m_strSQL.IsEmpty())
- {
-
-
- if (g_pMainWnd->ProcessChatMessageRequest(&m_bycode, sizeof(BYTE)) == 0)
- {
- g_bNeedCoon = 1;
- goto ll;
- }
- }
- else
- {
-
-
- const char *szDataBuf = m_strSQL.GetBuffer(0);
- m_strSQL.ReleaseBuffer();
- if (g_pMainWnd->ProcessChatMessageRequest((void*)szDataBuf, strlen(szDataBuf) + 1) == 0)
- {
- g_bNeedCoon = 1;
- goto ll;
- }
- }
- g_bSendOK = 1;
-
- }
-
-
- }
- void SendDataDlg::OnDestroy()
- {
- g_hSendWnd = NULL;
- if (m_pBk)
- delete m_pBk;
- CDialog::OnDestroy();
- }
- void SendDataDlg::OnClose()
- {
-
- CDialog::OnCancel();
- g_hSendWnd = NULL;
- }
- void SendDataDlg::OnPaint()
- {
- CPaintDC dc(this);
- if (m_pBk)
- {
- Graphics graph(dc.GetSafeHdc());
- CRect rc;
- GetClientRect(rc);
- Rect destinationRect(0, 0, rc.Width(), rc.Height());
- graph.DrawImage(m_pBk, destinationRect, rc.left, rc.top, rc.Width(), rc.Height(), UnitPixel);
- }
- }
|