#include "stdafx.h" #include "OTSCrypt.h" #include "OTSHelper.h" #include #ifdef _DEBUG #define new DEBUG_NEW #endif namespace OTSMODEL { #define LICENSE_STRING_LENGTH_MAX 1024 #define LICENSE_STRING_LENGTH_MIN 16 #define LICENSE_KEYSTRING_LENGTH_MAX 1024 #define LICENSE_KEYSTRING_LENGTH_MIN 16 COTSCrypt::COTSCrypt(void) { } COTSCrypt::~COTSCrypt(void) { } void COTSCrypt::Encrypt(const LPCTSTR& a_sKey, CString& a_sLicense) { char* sKey = NULL; char* sLicense = NULL; #ifdef UNICODE sKey = new char[LICENSE_KEYSTRING_LENGTH_MAX]; //wchar_t* wsKey = a_sKey.GetBuffer(); COTSHelper::WCharToChar(a_sKey, sKey); //a_sKey.ReleaseBuffer(); sLicense = new char[LICENSE_STRING_LENGTH_MAX]; wchar_t* wsLicense = (wchar_t*)a_sLicense.GetBuffer(); COTSHelper::WCharToChar(wsLicense, sLicense); a_sLicense.ReleaseBuffer(); #else sKey = (char *)a_sKey; sLicense = a_sLicense.GetBuffer(); #endif //UNICODE m_oCrypt.TransformString(sKey, sLicense); #ifdef UNICODE wchar_t* psz = new wchar_t[LICENSE_KEYSTRING_LENGTH_MAX]; memset(psz, 0, sizeof(wchar_t) * LICENSE_KEYSTRING_LENGTH_MAX); COTSHelper::CharToWChar(sLicense, psz); CString cl(psz); a_sLicense = cl; delete[] psz; #else CString cl(sLicense); #endif //delete[] sKey; //delete[] sLicense; } void COTSCrypt::Decrypt(const LPCTSTR& a_sKey, CString& a_sLicense) { char* sKey = NULL; char* sLicense = NULL; #ifdef UNICODE sKey = new char[LICENSE_KEYSTRING_LENGTH_MAX]; //wchar_t* wsKey = a_sKey.GetBuffer(); COTSHelper::WCharToChar(a_sKey, sKey); //a_sKey.ReleaseBuffer(); sLicense = new char[LICENSE_STRING_LENGTH_MAX]; wchar_t* wsLicense = (wchar_t*)a_sLicense.GetBuffer(); // begin tangchenglong modify 20140927 // DWORD dwNum = WideCharToMultiByte(CP_ACP, NULL, wsLicense, -1, NULL, 0, NULL, FALSE); // WideCharToMultiByte(CP_ACP, NULL, wsLicense, -1, sLicense, dwNum, NULL, FALSE); COTSHelper::WCharToChar(wsLicense, sLicense); // end tangchenglong modify 20140927 // COTSHelper::WCharToChar(wsLicense, sLicense); a_sLicense.ReleaseBuffer(); #else sKey = (char *)a_sKey; sLicense = a_sLicense.GetBuffer(); #endif m_oCrypt.TransformString(sKey, sLicense); // begin tangchenglong modify 20140927 //#ifdef UNICODE // CString cl(sLicense); // a_sLicense = cl; // delete[] sKey; // delete[] sLicense; //#else // //a_sKey.ReleaseBuffer(); // a_sLicense.ReleaseBuffer(); //#endif #ifdef UNICODE wchar_t * psz = new wchar_t[LICENSE_KEYSTRING_LENGTH_MAX]; memset(psz, 0, sizeof(wchar_t) * LICENSE_KEYSTRING_LENGTH_MAX); COTSHelper::CharToWChar(sLicense, psz); CString cl(psz); a_sLicense = cl; delete[] psz; delete[] sKey; delete[] sLicense; #else CString cl(sLicense); #endif // end tangchenglong modify 20140927 } }