ShuTuView.cpp 6.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

// ShuTuView.cpp : CShuTuView 类的实现
//

#include "stdafx.h"

#include "ShuTuView.h"

#include "Function.h"

#include "../cryptopp/modes.h"
#include "../cryptopp/aes.h"
#include "../cryptopp/filters.h"
#include "../cryptopp/base64.h"
#include "../cryptopp/hex.h"
#include "../cryptopp/files.h"
#include "../cryptopp/md5.h"

#include <ExDispid.h>
#include "DlgIE.h"


// CShuTuView 构造/析构
namespace DuiLib
{
	CShuTuView::CShuTuView()
	{
		g_info = _T("");
	}

	CShuTuView::~CShuTuView()
	{
	}

	CString GetModuleDir() 
	{ 
		HMODULE module = GetModuleHandle(0); 
		TCHAR pFileName[MAX_PATH]; 
		GetModuleFileName(module, pFileName, MAX_PATH); 

		CString csFullPath(pFileName); 
		int nPos = csFullPath.ReverseFind( _T('\\') ); 
		if( nPos < 0 ) 
			return CString(""); 
		else 
			return csFullPath.Left( nPos ); 
	}


	// CShuTuView 消息处理程序

	// 进行Url编码 UTF-8 
	CString UniToUrlEncode(const char* szEncode)
	{
		CString strEncoded;
		static const char szUnsafe[] = {"/<>%//^[]`+$,@:;/!#?=&\"" };
		static const char szHexChr[] = {"0123456789ABCDEF"};
		int nLength = strlen(szEncode);

		for(int i = 0; i < nLength; i++)
		{
			TCHAR ch = szEncode[i];

			if((NULL == strchr(szUnsafe, ch))
				&& ((int)ch > 32)
				&& ((int)ch < 123))
			{
				strEncoded += ch;
			}
			else
			{
				strEncoded += CString("%");
				strEncoded += szHexChr[(int)((ch >> 4) & 0x0f)];
				strEncoded += szHexChr[(int)(ch & 0x0f)];
			}
		}
		return strEncoded;
	}

	std::string AES_Encrypt(std::string plaintext)
	{
		//Key and IV setup
		//AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256-   
		//bit). This key is secretly exchanged between two parties before communication   
		//begins. DEFAULT_KEYLENGTH= 16 bytes
		byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH+1 ];
		memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH+1 );

		strcpy((char*)key, "d5fc9e6b8345d05b");

		//
		// String and Sink setup
		//
		std::string ciphertext;


		//
		// Create Cipher Text
		//
		CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
		CryptoPP::ECB_Mode_ExternalCipher::Encryption ecbEncryption( aesEncryption );

		CryptoPP::StreamTransformationFilter stfEncryptor(ecbEncryption, new CryptoPP::StringSink( ciphertext ), CryptoPP::BlockPaddingSchemeDef::PKCS_PADDING );
		stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() + 1 );
		stfEncryptor.MessageEnd();

		return ciphertext;
	}

	/*
	 * Description: to calculate the hash of the message, and return it(string).
	 * Input:
	 * message: need to calculate its hash value
	 * Output:
	 * return the hash value(string) of the message.
	 */
	std::string md5_string(const std::string & message)
	{
		std::string digest;
		CryptoPP::Weak1::MD5 md5;
		CryptoPP::StringSource(message, true,
					new CryptoPP::HashFilter(md5, new CryptoPP::HexEncoder(new CryptoPP::StringSink(digest))));
		return digest;
	}

	CString BuildEncodeInfo(CString loginName, CString password, CString bookCode, CString chapter, CString section)
	{
		CString str_info = L"";

		CStringA pwd_str(password.GetBuffer(0));
		password.ReleaseBuffer();

		std::string password_str(pwd_str.GetBuffer(0)); 
		pwd_str.ReleaseBuffer();

		std::string md5_str = md5_string(password_str);

		pwd_str = md5_str.c_str();

		password = pwd_str.GetBuffer(0);
		pwd_str.ReleaseBuffer();

		password = password.MakeLower();

		str_info.Format(_T("{\"loginName\":\"%s\",\"password\":\"%s\",\"bookCode\":\"%s\",\"chapter\":\"%s\",\"section\":\"%s\"}"), loginName, password, bookCode, chapter, section);

		CStringA stra(str_info.GetBuffer(0));
		str_info.ReleaseBuffer();

		std::string info(stra.GetBuffer(0));
		stra.ReleaseBuffer();

		std::string encode_info = "";
		std::string ciphertext = AES_Encrypt(info);

		CryptoPP::Base64Encoder encoder;

		encoder.Put((byte*)ciphertext.c_str(), ciphertext.size());
		encoder.MessageEnd();

		CryptoPP::word64 size = encoder.MaxRetrievable();
		if(size)
		{
			encode_info.resize(size);		
			encoder.Get((byte*)encode_info.data(), encode_info.size());
		}

		return UniToUrlEncode(encode_info.c_str());
	}

	CString g_url_server = _T("http://demo.digitallib.com/");
	std::string plaintext = "{\"bookCode\":\"XB-2007012-15F-01-01\",\"chapter\":\"Module 1 Getting to know you Unit 1 Greetings\",\"loginName\":\"zhangsan\",\"password\":\"96e79218965eb72c92a549dd5a330112\"}";

	void CShuTuView::OnXiaoben(CString userName, CString password, CString bookName, CString unitName, CString lesson)
	{
		if (_T("")==g_info)
		{
			g_info = BuildEncodeInfo(_T("zhangsan"),_T("111111"),_T("XB-2007012-15F-01-01"),_T("Module 1 Getting to know you Unit 1 Greetings"),_T(""));
		}
		g_info = BuildEncodeInfo(userName,password,bookName,unitName,lesson);

		CString sUrl = g_url_server +"getTextbookSelfResource.action?info=" + g_info;

		Navigate2(sUrl);
	}

	void CShuTuView::OnTuijian(CString userName, CString password, CString bookName, CString unitName, CString lesson)
	{
		if (_T("")==g_info)
		{
			g_info = BuildEncodeInfo(_T("zhangsan"),_T("111111"),_T("XB-2007012-15F-01-01"),_T("Module 1 Getting to know you Unit 1 Greetings"),_T(""));
		}
		g_info = BuildEncodeInfo(userName,password,bookName,unitName,lesson);

		CString sUrl = g_url_server +"getRecommendResource.action?info=" + g_info;

		Navigate2(sUrl);
	}


	void CShuTuView::OnBeforeNavigate2(LPCTSTR lpszURL, DWORD nFlags, LPCTSTR lpszTargetFrameName, CByteArray& baPostedData, LPCTSTR lpszHeaders, BOOL* pbCancel)
	{
		CString flag_more = _T("mode=system");
		CString sUrl(lpszURL);
		int nPos = 0;
		nPos = sUrl.Find(flag_more);
		if (nPos>0)
		{
			sUrl.Delete(nPos, flag_more.GetLength());
			sUrl += _T("info=") + g_info;

			CDlgIE dlg_ie;
			dlg_ie.m_url = sUrl;
			dlg_ie.DoModal();

			*pbCancel = TRUE;
			return;
		}
	}

	void CShuTuView::NewWindow3( IDispatch **ppDisp, VARIANT_BOOL *Cancel, DWORD dwFlags, BSTR bstrUrlContext, BSTR bstrUrl )
	{
		*Cancel = VARIANT_TRUE;

		CString flag_file = _T("mode=open");
		CString flag_more = _T("mode=system");
		CString sUrl(bstrUrl);
		int nPos = sUrl.Find(flag_file);
		if (nPos>0)
		{
			sUrl.Delete(nPos, flag_file.GetLength());
			sUrl += _T("request_source=textbook_client&");

			sUrl += _T("info=") + g_info;

			CDlgIE dlg_ie;
			dlg_ie.m_url = sUrl;
			dlg_ie.DoModal();
		} 
		else
		{
			nPos = sUrl.Find(flag_more);
			if (nPos>0)
			{
				sUrl.Delete(nPos, flag_more.GetLength());
				sUrl += _T("info=") + g_info;

				CDlgIE dlg_ie;
				dlg_ie.m_url = sUrl;
				dlg_ie.DoModal();
			} 
			else
			{
				Navigate2(bstrUrl);	
			}
		}
	}
}