⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 recoveselectfile.cpp

📁 COM 组建的开发
💻 CPP
字号:
// RecoveSelectFile.cpp: implementation of the CRecoveSelectFile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"
#include "RecoveSelectFile.h"
#include "MSXMLParser.h"
#include <shlwapi.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRecoveSelectFile::CRecoveSelectFile()
{
	m_hWnd = NULL;
	m_nHeadLen = 0;
	m_strHeadXML = "";
}

CRecoveSelectFile::~CRecoveSelectFile()
{

}
// 设置接收消息的对话框 [5/27/2008 By willing]
void CRecoveSelectFile::SetDlgWnd(const HWND hWnd)
{
	m_hWnd = hWnd;
}
// 获取接收消息的对话框的句柄 [5/27/2008 By willing]
HWND CRecoveSelectFile::GetDlgWnd()
{
	return m_hWnd;
}
// 判断指定的文件是否是加密过的文件 [5/28/2008 By willing]
int CRecoveSelectFile::IsEncFile(CSelectFile &SelectFile)
{
	CString strMsg = "";
	//strMsg.Format("开始校验文件 %s。",SelectFile.GetFileName());
	//PstMsg(strMsg,CXListBox::Black);
	strMsg.Format("CRecoveSelectFile::IsEncFile 要校验的文件 %s",SelectFile.GetFileName());
	OutputDebugString(strMsg);
	if (SelectFile.IsExist() == FALSE) 
	{
		PstMsg("文件不存在!",CXListBox::Red);
		return -1;
	}
	OutputDebugString("已经校验过,该文件存在");
	CString strExt = "";
	if (0 != SelectFile.GetFileExt(strExt))
	{
		PstMsg("获取文件扩展名失败!",CXListBox::Red);
		return -2;
	}
	OutputDebugString("获取到扩展名,开始比较扩展名是否正确");
	// 判断扩展名 [5/28/2008 By willing]
	if (strExt != "jde")
	{
		PstMsg("不是加密文件,无法还原!",CXListBox::Red);
		return -3;
	}
	OutputDebugString("开始打开文件从文件中获取加密到的信息");
	// 以下判断该文件是否是加密过的文件 [5/28/2008 By willing]
	DWORD dwHeadLen = 0;
	CFile checkFile;
	if (FALSE == checkFile.Open(SelectFile.GetFilePatchName(),CFile::modeRead|CFile::typeBinary))
	{
		DWORD dwErrCode = GetLastError();
		if (32 == dwErrCode)
		{
			strMsg = "校验文件时打开文件失败,文件已被占用!";
		}else{
			strMsg = "校验文件时打开文件失败!";
		}
		PstMsg(strMsg,CXListBox::Red);
		return -4;
	}
	OutputDebugString("开始读加密文件内容长度");
	if (sizeof(DWORD) != checkFile.Read(&dwHeadLen,sizeof(DWORD)))
	{
		PstMsg("校验文件时读文件失败!",CXListBox::Red);
		checkFile.Close();
		return -5;
	}
	strMsg.Format("加密文件中读到的加密后的头长度 %d",dwHeadLen);
	OutputDebugString(strMsg);
	// 度加密后的头信息 [5/28/2008 By willing]
	char* pszBuffer = new char[dwHeadLen+1];
	if (NULL == pszBuffer)
	{
		PstMsg("读文件失时,申请内存失败!",CXListBox::Red);
		checkFile.Close();
		return -6;
	}
	memset(pszBuffer,0,dwHeadLen+1);
	OutputDebugString("开始读文件的加密信息头儿");
	if (dwHeadLen != checkFile.Read(pszBuffer,dwHeadLen))
	{
		PstMsg("校验文件时读文件失败!",CXListBox::Red);
		delete [] pszBuffer;
		pszBuffer = NULL;
		checkFile.Close();
		return -7;
	}
	
	StrTrim(pszBuffer,"");
	char* pszMd5 = new char[33];
	if (NULL == pszMd5)
	{
		PstMsg("读文件失时,申请内存失败!",CXListBox::Red);
		delete [] pszBuffer;
		pszBuffer = NULL;
		checkFile.Close();
		return -8;
	}
	memset(pszMd5,0,33);
	if (32 != checkFile.Read(pszMd5,32))
	{
		PstMsg("读文件失败!",CXListBox::Red);
		delete [] pszBuffer;
		pszBuffer = NULL;
		delete [] pszMd5;
		pszMd5 = NULL;
		checkFile.Close();
		return -9;
	}
	// 读取成功开始解密 [5/28/2008 By willing]
	CToolsKit ToolsKit;
	char* pszBuffer2 = new char[dwHeadLen+100];
	if (NULL == pszBuffer2)
	{
		PstMsg("读文件失时,申请内存失败!",CXListBox::Red);
		delete [] pszBuffer;
		pszBuffer = NULL;
		delete [] pszMd5;
		pszMd5 = NULL;
		checkFile.Close();
		return -10;
	}
	memset(pszBuffer2,0,dwHeadLen+100);
	CString strMD5 = GetPwdMD5();
	// 获取密码的md5 [5/28/2008 By willing]
	if (FALSE == ToolsKit.Recove(pszBuffer2,pszBuffer,dwHeadLen,strMD5,8))
	{
		PstMsg("解密文件头失败!",CXListBox::Red);
		delete [] pszBuffer2;
		pszBuffer2 = NULL;
		delete [] pszBuffer;
		pszBuffer = NULL;
		delete [] pszMd5;
		pszMd5 = NULL;
		checkFile.Close();
		return -11;
	}
	// 校验是否正确 [5/29/2008 By willing]
	CMD5Checksum MD5Checksum;
	OutputDebugString(pszBuffer2);
	OutputDebugString(MD5Checksum.GetMD5(pszBuffer2));
	OutputDebugString(pszMd5);
	
	if (MD5Checksum.GetMD5(pszBuffer2) != CString(pszMd5))
	{
		PstMsg("密码不正确!",CXListBox::Red);
		delete [] pszBuffer2;
		pszBuffer2 = NULL;
		delete [] pszBuffer;
		pszBuffer = NULL;
		delete [] pszMd5;
		pszMd5 = NULL;
		checkFile.Close();
		return -12;
	}
	OutputDebugString("开始解析加密文件XML");
	// 根据加密类型判断是否对硬件进行了限制 [6/2/2008 By willing]
	CMSXMLParser MSXMLParser;
	int nReCode = MSXMLParser.ParseEncFileInfo(pszBuffer2,encFileInfo);
	if (0 != nReCode)
	{
		strMsg.Format("返回值为 %d",nReCode);
		OutputDebugString(strMsg);
		PstMsg("解析加密文件信息失败!",CXListBox::Red);
		delete [] pszBuffer2;
		pszBuffer2 = NULL;
		delete [] pszBuffer;
		pszBuffer = NULL;
		delete [] pszMd5;
		pszMd5 = NULL;
		checkFile.Close();
		return -13;
	}
	OutputDebugString("解析加密文件XML完毕");
	if ((1 == encFileInfo.nEncType)||(2 == encFileInfo.nEncType))
	{
		// 加密时选择了对硬件的限制 [6/2/2008 By willing]
		CString strHostCode = ToolsKit.GetHostCode();
		if (CString(encFileInfo.szHostCode) != strHostCode)
		{
			PstMsg("该文件只能在加密机器上解密!",CXListBox::Blue);
			delete [] pszBuffer2;
			pszBuffer2 = NULL;
			delete [] pszBuffer;
			pszBuffer = NULL;
			delete [] pszMd5;
			pszMd5 = NULL;
			checkFile.Close();
			return -14;
		}else{
			PstMsg("验证通过,该文件是在本机上加密的!",CXListBox::Blue);
		}
	}
	m_strFilePatch = SelectFile.GetFilePatch();
	// 保留加密文件头XML[5/29/2008 By willing]
	m_strHeadXML = CString(pszBuffer2);
	delete [] pszBuffer2;
	pszBuffer2 = NULL;
	delete [] pszBuffer;
	pszBuffer = NULL;
	delete [] pszMd5;
	pszMd5 = NULL;
	checkFile.Close();
	m_nHeadLen = 4+dwHeadLen+32;
	return 0;	
}
// 向窗口发送还原消息 [5/28/2008 By willing]
void CRecoveSelectFile::PstMsg(CString strMsg,const int nTextColor)
{
	if (NULL == m_hWnd)
	{
		return;
	}
	if ("" == strMsg)
	{
		return;
	}
	RCV_MSG* ptrEncMsg = new RCV_MSG;
	if (NULL == ptrEncMsg)
	{
		return;
	}	
	ZeroMemory(ptrEncMsg,sizeof(ENC_MSG));
	sprintf(ptrEncMsg->szMsg,"%s",strMsg);
	ptrEncMsg->nTextColor = nTextColor;

	::PostMessage(m_hWnd,WM_RC_SENDMSG,(WPARAM)ptrEncMsg,NULL);
}
// 设置解密密码 [5/28/2008 By willing]
void CRecoveSelectFile::SetPwd(const CString strPwd)
{
	m_strPwd = strPwd;
	return;
}
// 获取密码的MD5 [5/28/2008 By willing]
CString CRecoveSelectFile::GetPwdMD5()
{
	CString str = "";
	CString strRe = "";
	if ("" == m_strPwd)
	{
		str = DES_MIYAO;
	}else{
		str = m_strPwd;
	}
	CToolsKit ToolKit;
	strRe = ToolKit.GetMD5_Str(str).Left(8);

	return strRe;
}
// 返回加密文件头的长度 [6/18/2008 By willing]
int CRecoveSelectFile::GetHeadLen()
{
	return m_nHeadLen;
}
// 获取使用的加密算法 [6/18/2008 By willing]
int CRecoveSelectFile::GetEncMethod()
{
	return encFileInfo.nEncMethod;
}
// 返回原来的文件名称 [6/18/2008 By willing]
CString CRecoveSelectFile::GetFileName()
{
	return m_strFilePatch+encFileInfo.strFileName;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -