#include <iostream.h>
#include <windows.h>
#include <tchar.h>
void EncodeString(LPCTSTR lpszText, LPTSTR *lpszReturn, LPCTSTR lpszKey)
{
int nTextLen = 0;
char *cPos = NULL;
char *pDest = NULL;
if(!lpszReturn) // 加密
{
nTextLen = ::_tcslen(lpszText);
pDest = (LPTSTR)lpszText;
}
else // 解密
{
// 查找自定的中止标记
cPos = (LPTSTR)lpszText;
while(true) // 从这里可以看到,除非搜索到我们自定的中止标记,否则会一直搜索下去
{
if(*cPos == '=')
if(cPos[1] == '=')
if(cPos[2] == '\0')
break;
cPos++;
}
if(!cPos) // 没有找到结束标记,也不是加密
return;
nTextLen = cPos - lpszText;
pDest = new char[nTextLen + 3]; // ==\0
}
int nKeyLen = ::_tcslen(lpszKey);
int i = 0;
int k = 0;
for(; i < nTextLen; i++)
{
pDest[i] = lpszText[i] ^ lpszKey[k];
k++;
if(k >= nKeyLen)
k = 0;
}
if(!cPos)
memcpy(pDest + nTextLen, _T("==\0"), 3 * 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 0;
}
原文链接: https://www.cnblogs.com/xuandi/p/5855669.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/240223
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!