// SendDataDlg.cpp : implementation file // #include "stdafx.h" #include "ylgl.h" #include "SendDataDlg.h" #include "ConnectDlg.h" #include #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 dialog SendDataDlg::SendDataDlg(CWnd* pParent /*=NULL*/) : CDialog(SendDataDlg::IDD, pParent) { //{{AFX_DATA_INIT(SendDataDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT m_pData=NULL; m_trytimes=0; m_pBk=NULL; } void SendDataDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(SendDataDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(SendDataDlg, CDialog) //{{AFX_MSG_MAP(SendDataDlg) ON_WM_TIMER() ON_WM_DESTROY() ON_WM_CLOSE() ON_WM_PAINT() ON_WM_ERASEBKGND() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // SendDataDlg message handlers BOOL SendDataDlg::OnEraseBkgnd(CDC* pDC) { // TODO: Add your message handler code here and/or call default #ifdef NEW_SKIN return 1; #else return CDialog::OnEraseBkgnd (pDC); #endif } void SendDataDlg::OnOK() { // TODO: Add extra validation here } void SendDataDlg::OnCancel() { // TODO: Add extra cleanup here } BOOL SendDataDlg::OnInitDialog() { CDialog::OnInitDialog();// CenterWindow(g_pMainWnd); 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; //以下获得一个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; i2) { // MessageBox("连接失败!!!"); g_pMainWnd->KillTimer (2); g_pMainWnd->SetTimer (2, 10, NULL); return; } CConnectDlg dlg; dlg.DoModal(); if(g_bSendOK) { //printf("Jeff:SendDataDlg::连接成功,g_conntype=%d\n\n",g_conntype);//Jeff Printf; g_bNeedCoon=0; if(g_conntype) { // g_server=g_serverbak; // MessageBox("获取ipxx"+g_server); // LOG4C((LOG_NOTICE,"g_server=%s",g_server)); 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) { // MessageBox("xxxxw"); //LOG4C((LOG_NOTICE,"连接失败")); delete []pData; g_nSendCode=0; g_bNeedCoon=1; goto ll; } /* int aa=100; while(g_pMainWnd->m_dwConnectionID==INVALID_SOCKET) { ::Sleep (50); aa--; if(aa==0)break; }*/ 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 { // MessageBox("连接失败ccc"); // LOG4C((LOG_NOTICE,"连接失败")); //printf("Jeff:SendDataDlg::连接失败,g_conntype=%d\n\n",g_conntype); g_bNeedCoon=1; goto ll;//2014.3.6 return; } } g_nSendCode=nSendCodetemp; if(m_pData) { // LOG4C((LOG_NOTICE,"m_pData != NULL")); if(g_pMainWnd->ProcessChatMessageRequest((void*)m_pData, m_nlength)==0) { g_bNeedCoon=1; goto ll; } } else if(m_strSQL.IsEmpty ()) { // LOG4C((LOG_NOTICE,"m_strSQL==NULL")); if(g_pMainWnd->ProcessChatMessageRequest(&m_bycode, sizeof(BYTE))==0) { g_bNeedCoon=1; goto ll; } } else { // LOG4C((LOG_NOTICE,"m_strSQL != NULL")); 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() { // MessageBox("close message"); CDialog::OnCancel (); g_hSendWnd=NULL; } void SendDataDlg::OnPaint() { CPaintDC dc(this); // device context for painting 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); } }