DLTools.cpp 4.03 KB
#include "stdafx.h"
#include "DLTools.h"
#include <locale>


void Log(CString sLog)
{
	CStdioFile fileLog;
	CString sPath;
	TCHAR szPath[MAX_PATH];
	GetModuleFileName(NULL, szPath, MAX_PATH);
	CString sMudolePath = szPath;
	CString sMudoleName = AfxGetAppName();
	sPath = sMudolePath.Left(sMudolePath.GetLength() - sMudoleName.GetLength() - 4);

	if (_access(sPath + "Log", 0) == -1)
	{
		_mkdir(sPath + "Log");
	}

	CFileStatus statusFile;
	statusFile.m_size = 0;
	CString sLogPath = sPath + "Log\\DLCardTool.log";
	CFile::GetStatus(sLogPath, statusFile);

	CTime tmCurTime;
	tmCurTime = CTime::GetCurrentTime();
	CString sData = tmCurTime.Format("%y%m%d-%H%M%S");

	setlocale(LC_CTYPE, ("chs"));

	CString sText = sData + L" " + sLog;
	if (statusFile.m_size > 1024 * 1024 * 10)
	{
		if (fileLog.Open(sLogPath, CFile::modeCreate | CFile::modeWrite))
		{
			fileLog.WriteString(sText);
			fileLog.WriteString(L"\n");
			fileLog.Close();
		}
	}
	else
	{
		if (fileLog.Open(sLogPath, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate))
		{
			fileLog.SeekToEnd();
			fileLog.WriteString(sText);
			fileLog.WriteString(L"\n");
			fileLog.Close();
		}
	}
}

int Encrypt(unsigned char* pSrc, unsigned char u8Len, unsigned char* pKey, unsigned char* pCiphertText)
{
	int iResult = 0;

	uint8_t au8DataBlock[50][8] = { 0 };

	int iBlockCount = 0, iCoverPos = 0, iBlockNum = 0;
	iBlockCount = u8Len / 8;
	iCoverPos = u8Len % 8;

	while (iBlockCount > 0)
	{
		memcpy(au8DataBlock[iBlockNum], pSrc, 8);
		pSrc += 8;
		--iBlockCount;
		++iBlockNum;
	}

	if (iCoverPos)
	{
		memcpy(au8DataBlock[iBlockNum], pSrc, iCoverPos);
		au8DataBlock[iBlockNum][iCoverPos] = 0x80;
		++iCoverPos;
		for (int index = iCoverPos; iCoverPos < 8; ++iCoverPos)
		{
			au8DataBlock[iBlockNum][iCoverPos] = 0x00;
		}
	}
	else
	{
		--iBlockNum;
	}

	uint8_t au8KeyL[8] = { 0 }, au8KeyR[8] = { 0 };
	memcpy(au8KeyL, pKey, 8);
	memcpy(au8KeyR, pKey, 8);
	for (int index = 0; index <= iBlockNum; ++index)
	{
		uint8_t au8Cipher1[8] = { 0 }, au8Cipher2[8] = { 0 };
		DES(au8KeyL, au8DataBlock[index], au8Cipher1);
		_DES(au8KeyR, au8Cipher1, au8Cipher2);
		DES(au8KeyL, au8Cipher2, &pCiphertText[index * 8]);
	}

	return iResult;
}

bool HexToChar(unsigned char* pHex, int iHexLen, unsigned char* pChar)
{
	bool bResult = true;
	if (pHex && iHexLen && pChar)
	{
		int iCharCount = 0;
		for (int index = 0; index < iHexLen; ++index)
		{
			char cNum[3] = { 0 };
			if (pHex[index] <= 0x0f)
			{
				pChar[iCharCount] = '0';
				++iCharCount;
				sprintf_s(cNum, "%X", pHex[index]);
				pChar[iCharCount] = cNum[0];
				++iCharCount;
			}
			else
			{
				sprintf_s(cNum, "%2X", pHex[index]);
				pChar[iCharCount] = cNum[0];
				++iCharCount;
				pChar[iCharCount] = cNum[1];
				++iCharCount;
			}
		}
	}
	return bResult;
}

bool CharToHex(unsigned char* pStr, unsigned char& cHex)
{
	bool bResult = true;

	unsigned char aHex[2] = { 0 }, cTemp = 0;
	if (pStr)
	{
		int iCount = 2;
		do
		{
			char cStr = tolower(pStr[iCount - 1]);
			switch (cStr)
			{
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			{
				cTemp = atoi(&cStr);
				break;
			}
			case 'a':
			{
				cTemp = 10;
				break;
			}
			case 'b':
			{
				cTemp = 11;
				break;
			}
			case 'c':
			{
				cTemp = 12;
				break;
			}
			case 'd':
			{
				cTemp = 13;
				break;
			}
			case 'e':
			{
				cTemp = 14;
				break;
			}
			case 'f':
			{
				cTemp = 15;
				break;
			}
			}
			aHex[iCount - 1] = cTemp;
			--iCount;
		} while (iCount);

		cHex = (aHex[0] << 4) + aHex[1];
	}
	else
	{
		bResult = false;
	}
	return bResult;
}

bool StringToHex(unsigned char* pStr, int iLen, unsigned char* pHex)
{
	bool bResult = true;

	int iStrIndex = 0;
	int index = 0;
	while (bResult && iStrIndex < iLen)
	{
		bResult = CharToHex(&pStr[iStrIndex], pHex[index]);
		++index;
		iStrIndex += 2;
	}

	if (!bResult)
	{
		throw std::exception("9999");
	}
	/*for (int index = 0, iStrIndex = 0; iStrIndex < iLen; ++index, iStrIndex += 2)
	{
	CharToHex(&pStr[iStrIndex], pHex[index]);
	}*/

	return bResult;
}