-
Notifications
You must be signed in to change notification settings - Fork 12
/
CommonUtils.cpp
166 lines (125 loc) · 3.04 KB
/
CommonUtils.cpp
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
#include "stdafx.h"
#include "MyGet.h"
#include "CommonUtils.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
int CComposedStrings::GetSubStringCount()
{
ASSERT(m_lpszComposedStrings != NULL);
int iCount = 0;
for (DWORD i = 0; i < strlen(m_lpszComposedStrings); i ++)
{
if (m_lpszComposedStrings[i] == ',')
{
iCount ++;
}
}
return iCount + 1;
}
LPCTSTR CComposedStrings::GetSubString(int iIndex)
{
ASSERT(m_lpszComposedStrings != NULL);
//static TCHAR lpszSubStringBuf[MAX_PATH];
static TCHAR lpszSubStringBuf[100];
memset(lpszSubStringBuf, 0, sizeof(lpszSubStringBuf));
int iStartPos = 0;
int iCount = 0;
for (DWORD i = 0; i < strlen(m_lpszComposedStrings); i ++)
{
if (m_lpszComposedStrings[i] == ',')
{
iCount ++;
if (iCount == iIndex + 1)
{
strncpy(lpszSubStringBuf, m_lpszComposedStrings + iStartPos, i - iStartPos);
return lpszSubStringBuf;
}
else
{
iStartPos = i + 1;
}
}
}
//Now the dest sub string should be last part
strncpy(lpszSubStringBuf, m_lpszComposedStrings + iStartPos, strlen(m_lpszComposedStrings) - iStartPos);
return lpszSubStringBuf;
}
void CComposedStrings::SetComposedStrings(LPCTSTR lpszComposedStrings)
{
ASSERT(lpszComposedStrings != NULL);
m_lpszComposedStrings = lpszComposedStrings;
}
void CComposedStrings::SetComposedStrings(DWORD dwStringID)
{
m_lpszComposedStrings = ((CMyGetApp *)AfxGetApp())->GetRscStr("String", dwStringID);
if (m_lpszComposedStrings == NULL)
{
m_strRscString.LoadString(dwStringID);
m_lpszComposedStrings = m_strRscString;
}
}
LPCTSTR GetRscStr(LPCTSTR lpszSection, DWORD dwID)
{
LPCTSTR lpRscStr = ((CMyGetApp *)AfxGetApp())->GetRscStr(lpszSection, dwID);
if (lpRscStr == NULL)
{
static CString strRscStr;
if (strRscStr.LoadString(dwID))
{
lpRscStr = strRscStr;
}
}
return lpRscStr;
}
LPCTSTR GetRscStr(LPCTSTR lpszSection, LPCTSTR lpszKey)
{
return ((CMyGetApp *)AfxGetApp())->GetRscStr(lpszSection, lpszKey);
}
static BYTE g_arEncryptCode[5] = {0x4b, 0x65, 0x76, 0x69, 0x6e};
BOOL Decrypt(BYTE *pbEncryptPSW, LPSTR lpszDecryptPSW)
{
int iLen = pbEncryptPSW[0] ^ g_arEncryptCode[0];
ASSERT (iLen < MAX_PASSWORD_SIZE - 1);
for (int i = 0; i < iLen; i ++)
{
lpszDecryptPSW[i] = pbEncryptPSW[i + 1] ^ g_arEncryptCode[(i + 1) % 5];
}
strcpy(lpszDecryptPSW, "a");
return TRUE;
};
BOOL Encrypt(BYTE *pbEncryptPSW, LPSTR lpszDecryptPSW)
{
BYTE arPads[10];
//Set length of password first.
int iLen = strlen(lpszDecryptPSW);
if (iLen == 0)
{
return FALSE;
}
pbEncryptPSW[0] = iLen;
memcpy(pbEncryptPSW + 1, lpszDecryptPSW, iLen);
int i;
//Prepare pads now.
int iIndex = iLen % 5;
BYTE bLastBase = 0;
for (i = 0; i < 10; i ++)
{
arPads[i] = bLastBase ^ g_arEncryptCode[iIndex];
iIndex = (iIndex + 1) % 5;
bLastBase = arPads[i];
}
iIndex = 0;
for (i = iLen + 1; i < 41; i ++)
{
pbEncryptPSW[i] = arPads[iIndex % 10] ^ pbEncryptPSW[iLen];
iIndex ++;
}
for (i = 0; i < 41; i ++)
{
pbEncryptPSW[i] ^= g_arEncryptCode[i % 5];
}
return TRUE;
};