#include <iostream.h>
#include <windows.h>
#include <tchar.h> void EncodeString(LPCTSTR lpszText, LPTSTR *lpszReturn, LPCTSTR lpszKey)
{
int nTextLen = ;
char *cPos = NULL;
char *pDest = NULL;
if(!lpszReturn) // 加密
{
nTextLen = ::_tcslen(lpszText);
pDest = (LPTSTR)lpszText;
}
else // 解密
{
// 查找自定的中止标记
cPos = (LPTSTR)lpszText;
while(true) // 从这里可以看到,除非搜索到我们自定的中止标记,否则会一直搜索下去
{
if(*cPos == '=')
if(cPos[] == '=')
if(cPos[] == '\0')
break;
cPos++;
}
if(!cPos) // 没有找到结束标记,也不是加密
return;
nTextLen = cPos - lpszText;
pDest = new char[nTextLen + ]; // ==\0
} int nKeyLen = ::_tcslen(lpszKey);
int i = ;
int k = ;
for(; i < nTextLen; i++)
{
pDest[i] = lpszText[i] ^ lpszKey[k];
k++;
if(k >= nKeyLen)
k = ;
} if(!cPos)
memcpy(pDest + nTextLen, _T("==\0"), * sizeof(TCHAR));
else
{
memset(pDest + nTextLen, _T('\0'), sizeof(TCHAR));
*lpszReturn = pDest;
}
} int main(int argc, char* argv[])
{
char strText[] = "Hello world! I'm zimmerk. I'm a boy. What's your name?";
char *lpszDest = NULL;
cout<<strText<<endl;
cout<<"========================================"<<endl;
EncodeString(strText , NULL, "Zimmerk"); // 加密
cout<<strText<<endl;
cout<<"========================================"<<endl;
EncodeString(strText, &lpszDest, "Zimmerk"); // 解密
if(*lpszDest)
{
cout<<lpszDest<<endl;
delete [] lpszDest;
}
else
cout<<_T("(NULL)")<<endl;
return ;
}