//DisposeData.h
#pragma once
#include "DES.h"
#include <vector>
using namespace std;
class DisposeData
{
public:
DisposeData();
void SetValue(char *key,char *data);
void SetValue(char *data);
//加密解密文件名
CString EncryptFileName();
CString DecryptFileName();
//解密文件内容
CString ProcessDecode();
//处理文件内容,参数为真表示加密,为假表示解密
CString ProcessData(BOOL IsEncode);
virtual ~DisposeData();
private:
//随机产生8位密码
CString RandStringKey();
//加密密钥
CString ProcessKey(BOOL IsEncode);
void ASCAdd(string &s);
void ASCPlus(string &s);
void ChangeChar(string &s);
void TurnRound(string &s,bool IsTurnRight);
//拆分字符串
void SplitData();
DES m_des;
char* m_key;
char* m_data;
vector<CString> m_finaldata;
};
//DisposeData.cpp
#include "stdafx.h"
#include "DisposeData.h"
DisposeData::DisposeData()
{
m_data = new char();
m_data = '\0';
}
DisposeData::~DisposeData()
{
m_finaldata.clear();
if(m_data != NULL)
{
delete[]m_data;
m_data = NULL;
}
}
void DisposeData::SetValue(char *data)
{
m_data = new char[strlen(data)+1];
memcpy(m_data,data,strlen(data));
m_data[strlen(data)] = 0;
}
void DisposeData::SplitData()
{
size_t k = 0;
int round = strlen(m_data) / 8;
while(round-- > 0)
{
char tmpdata[9];
for(int i = 0; i < 8; i++)
{
tmpdata[i] = m_data[k++];
}
tmpdata[8] = 0;
CString tmpstr(tmpdata);
m_finaldata.push_back(tmpstr);
}
if(k < strlen(m_data))
{
char tmpdata[9];
int j = 0;
while(k < strlen(m_data))
{
tmpdata[j++] = m_data[k++];
}
tmpdata[j] = '\0';
CString tmpstr(tmpdata);
m_finaldata.push_back(tmpstr);
}
}
CString DisposeData::ProcessData(BOOL IsEncode)
{
CString str("");
CString key("12345678");
m_finaldata.clear();
SplitData();
vector<CString>::iterator iterStr = m_finaldata.begin();
if(IsEncode)
{
while(1)
{
CString cipherdata("");//加密后的密文
iterStr = m_finaldata.begin();
//加密
while(iterStr != m_finaldata.end())
{
char *tempData = (*iterStr).GetBuffer(0);
(*iterStr).ReleaseBuffer();
char *tempKey = key.GetBuffer(0);
key.ReleaseBuffer();
m_des.SetEncodeValue(tempKey,tempData);
m_des.EncodeData();
cipherdata += m_des.GetCipherData();
iterStr++;
}
//判断密码是否能恢复密文
CString detectionStr;
CString tmpCipherdata;
DisposeData tempDis;
BOOL tempb = FALSE;
char *tmpkey = key.GetBuffer(0);
key.ReleaseBuffer();
tempDis.SetValue(tmpkey);
tmpCipherdata.Format("%s%s",tempDis.ProcessKey(TRUE),cipherdata);
char *tempData = tmpCipherdata.GetBuffer(0);
tmpCipherdata.ReleaseBuffer();
tempDis.SetValue(tempData);
detectionStr = tempDis.ProcessData(tempb);
if(detectionStr.Compare(m_data) == 0)
{
DisposeData encodeKey;
tempData = key.GetBuffer(0);
key.ReleaseBuffer();
encodeKey.SetValue(tempData);
//将密码的密文和密文一起保留并加特殊字符
str.Format("%s%s%s",encodeKey.ProcessKey(TRUE),cipherdata,"&ENCTYPT&");
break;
}
else
{
key = RandStringKey();
}
}
}
else
{
//提取密码的密文
char *cipherkey = (*iterStr).GetBuffer(0);
(*iterStr).ReleaseBuffer();
iterStr++;
DisposeData decodekey;
decodekey.SetValue(cipherkey);
//解析出密码
key = decodekey.ProcessKey(FALSE);
//解密
while(iterStr != m_finaldata.end())
{
char *tempData = (*iterStr).GetBuffer(0);
(*iterStr).ReleaseBuffer();
char *tempKey = key.GetBuffer(0);
key.ReleaseBuffer();
m_des.SetDecodeValue(tempKey,tempData);
m_des.DecodeData();
str += m_des.GetPlainData();
iterStr++;
}
}
return str;
}
CString DisposeData::RandStringKey()
{
int i;
CString code;
CString s;
char temp;
unsigned m;
for(char ch = 'A'; ch <= 'Z';ch++)
code += ch;
for(char ch = '0'; ch <= '9';ch++)
code += ch;
for(char ch = 'a'; ch <= 'z';ch++)
code += ch;
for(i = 0; i < 8; i++)
{
m = rand() % 62;
temp = code[m];
s += temp;
}
return s;
}
CString DisposeData::ProcessKey(BOOL IsEncode)
{
CString str("");
if(IsEncode)
{
m_des.SetEncodeValue("12345678",m_data);
m_des.EncodeData();
str += m_des.GetCipherData();
}
else
{
m_des.SetDecodeValue("12345678",m_data);
m_des.DecodeData();
str += m_des.GetPlainData();
}
return str;
}
CString DisposeData::ProcessDecode()
{
CString tmpDecodeData(m_data);
CString encodeData("");
int start = 0;
// char ch = -128;
while((start = tmpDecodeData.Find("ENCTYPT")) != -1)
{
CString tmpData("");
tmpData = tmpDecodeData.Left(start-1);//取start左边的一段
tmpDecodeData = tmpDecodeData.Right(tmpDecodeData.GetLength() - start - 8);//start右边的一段
DisposeData tmpDis;
char *tmp = tmpData.GetBuffer(0);
tmpData.ReleaseBuffer();
tmpDis.SetValue(tmp);
encodeData += tmpDis.ProcessData(FALSE);
}
return encodeData;
}
void DisposeData::ASCAdd(string &s)
{
for(size_t i = 0; i < s.size(); i++)
{
if(s[i] >= 'C' && s[i] <= 'Z')
{
s[i] = 135 - s[i];
}
else if(s[i] >= 'a' && s[i] <= 'z')
{
s[i] -= 32;
}
else if(s[i] == 'A')
{
s[i] = '.';
}
else if(s[i] == 'B')
{
s[i] = '_';
}
}
}
void DisposeData::ASCPlus(string &s)
{
for(size_t i = 0; i < s.size(); i++)
{
if(s[i] >= 'A' && s[i] <= 'Z')
{
s[i] += 32;
}
else if(s[i] >= '0' && s[i] <= '9' || s[i] == '-')
{
s[i] = 135 - s[i];
}
else if(s[i] == '.')
{
s[i] = 'A';
}
else if(s[i] == '_')
{
s[i] = 'B';
}
}
}
void DisposeData::ChangeChar(string &s)
{
for(size_t i = 0; i < s.size()-1; i += 2)
{
char temp = s[i];
s[i] = s[i+1];
s[i+1] = temp;
}
}
void DisposeData::TurnRound(string &s,bool IsTurnRight)
{
// 交换以第一个字母的数字确定右移多少位置,
int b;
char tmpCh = 'F';
b = s[0] % 10;
b %= s.size();
string tmpS;
string::iterator iter = s.begin();
string::iterator iterTmp = tmpS.begin();
if(IsTurnRight)
{
tmpS.insert(iterTmp, iter, iter+s.size()-b);
iterTmp = tmpS.begin();
tmpS.insert(iterTmp, iter+(s.size()-b), s.end());
iterTmp = tmpS.begin();
tmpCh += b;
iterTmp = tmpS.insert(iterTmp, tmpCh);
}
else
{
tmpS.insert(iterTmp, iter+1, iter+b+1);
iterTmp = tmpS.begin();
tmpS.insert(iterTmp, iter+b+1, iter+s.size());
}
s = tmpS;
}
CString DisposeData::EncryptFileName()
{
CString FileName("");
string s(m_data);
ASCPlus(s);
ChangeChar(s);
TurnRound(s,true);
const char *data = s.c_str();
FileName.Format("%s",data);
return FileName;
}
CString DisposeData::DecryptFileName()
{
CString FileName("");
string s(m_data);
TurnRound(s,false);
ChangeChar(s);
ASCAdd(s);
const char *data = s.c_str();
FileName.Format("%s",data);
return FileName;
}