DLTools.cpp
4.03 KB
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
#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;
}