#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "afxcmn.h"
#include "afxwin.h"
class CCode
{
public:
static CString Decode(CString szData, CString strKey);
static CString Encode(CString szData, CString strKey);
CCode();
virtual ~CCode();
};
#include
#include "stdafx.h"
#include "EncodeDecode.h"
#include "time.h"
CCode::CCode()
{
}
CCode::~CCode()
{
}
CString CCode::Encode(CString szData, CString strKey)
{
//szData为待加密文本
//strKey为加密秘钥
int en[4];
srand((int)time(0));//使得每次生成的密文不相同
for (int i = 0; i < 4; i++)
{
en[i] = 1+(int)(16.0*rand()/(RAND_MAX+1.0));//生成随机密文(范围可自己调整0-15)
}
DWORD num = MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)szData, -1, NULL, 0);
intnData = szData.GetLength();//取生成的16进制字符串长度
WCHAR *pData; //定义一个UNICODE的指针
pData = (WCHAR*)calloc(num,sizeof(WCHAR));//动态的申请空间存字
if(pData == NULL) //判断申请到空间没有
{
free(pData);
}
memset(pData, 0, num * sizeof(WCHAR)); //初始化动作
MultiByteToWideChar(CP_ACP,0, (LPCTSTR)szData, -1, pData, num);
WCHAR *pData_R = pData;
DWORD num2 = MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strKey, -1, NULL, 0);
WCHAR *pKey;
pKey = (WCHAR*)calloc(num2,sizeof(WCHAR));
if(pKey == NULL) //判断申请到空间没有
{
free(pKey);
}
memset(pKey, 0, num2 * sizeof(WCHAR)); //初始化动作
MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strKey, -1, pKey, num2);
intnKeyLen = strKey.GetLength();//取生成的16进制字符串长度
int tmpi = 0;
CString tmps, enstr;
for (int i = 0; i
{
tmpi = *pData + pKey[i%nKeyLen] + i*en[i%4];//按位加密运算
int strtmp;
strtmp = *pData;
tmpi = tmpi>65535 ? tmpi - 65535 : tmpi;//范围修正
tmps.Format(_T("%x"), tmpi);
//格式输出
if (tmpi >= 4096)
{
enstr += tmps;
}
else if (tmpi >= 256)
{
enstr += _T("0") + tmps;
}
else if (tmpi >= 16)
{
enstr += _T("00") + tmps;
}
else
{
enstr += _T("000") + tmps;
}
pData++;
}
*pData = 0;
CString ens;
for (int i = 0; i <4; i++)
{
tmps.Format(_T("%x"), en[i]);//随机秘钥放在返回字串前占用1个字符
ens += tmps;
}
free(pData_R);
free(pKey);
return ens + enstr;
}
CString CCode::Decode(CString szData, CString strKey)
{
//szData为待解密文本
//strKey为解密秘钥(同加密秘钥)
int de[4];
for (int i = 0; i < 4; i++)
{
CString szTemp;
szTemp = szData.Left(1);
CString temp;
de[i] = _tcstoul(szTemp,0,16); //获得随机秘钥
szData.Delete(0, 1);//剩下的密文
}
int dn = szData.GetLength() / 4 ;
DWORD num3 = MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strKey, -1, NULL, 0);
WCHAR *pKey;
pKey = (WCHAR*)calloc(num3,sizeof(WCHAR));
if(pKey == NULL)
{
free(pKey);
}
memset(pKey, 0, num3 * sizeof(WCHAR)); //初始化动作
MultiByteToWideChar(CP_ACP,0, (LPCTSTR)strKey, -1, pKey, num3);
intnKeyLen = strKey.GetLength();
WCHAR *strc;
strc = (WCHAR*)calloc(dn+1,sizeof(WCHAR));
if(strc == NULL)
{
free(strc);
}
memset(strc, 0, (dn + 1) * sizeof(WCHAR)); //初始化动作
//MultiByteToWideChar(CP_ACP,0, (LPCTSTR)strKey, -1, pKey, num3);
WCHAR *enstr = strc;
CString tmps;
unsigned int tmpi = 0;
for (int i = 0; i < dn; i++)
{
tmps = szData.Left(4);//取4个字符(16进制字符)
szData.Delete(0, 4);//剩下的字符
*strc = (WCHAR) _tcstoul(tmps,0,16);
tmpi = *strc - pKey[i%nKeyLen] - i*de[i % 4];//解密运算
tmpi < 0 ? tmpi + 65535 : tmpi;
*strc = tmpi;
strc++;
}
*strc = 0;
tmps = enstr;
free(pKey);
free(enstr);
return tmps;
}
//解密和加密运算中,求数组内的值只能用%,不能用/号,否则越界,造成内存问题。
//对指针申请了内存空间,要记得归还。如果对指针进行了操作,记得先将这个指针初始化另一个指针,最后将另一个指针释放掉