📄 encryptselectfile.cpp
字号:
// EncryptSelectFile.cpp: implementation of the CEncryptSelectFile class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h"
#include "EncryptSelectFile.h"
#include <io.h>
#include "XListBox.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CEncryptSelectFile::CEncryptSelectFile()
{
hWnd = NULL;
}
CEncryptSelectFile::~CEncryptSelectFile()
{
}
// 设置接收消息的窗口的句柄 [5/8/2008 By willing]
void CEncryptSelectFile::SetDlgWnd(const HWND wnd)
{
hWnd = wnd;
}
int CEncryptSelectFile::Encrypt(CSelectFile &SelectFile,
const CString strPwd,
const CString strHostCode,
const int nEncSuanFa,
const DWORD dwEncType)
{
OutputDebugString("CEncryptSelectFile::Encrypt");
CString strHeadXml = CreateEncFileHeadXml(SelectFile,strPwd,nEncSuanFa,dwEncType);
if ("" == strHeadXml)
{
// 生成加密文件头XML串失败 [5/8/2008 By willing]
SendMsg("生成加密文件头信息失败!",CXListBox::Red);
return -2;
}
// 判断待加密的文件是否存在 [5/8/2008 By willing]
if (SelectFile.IsExist() == FALSE)
{
SendMsg("待加密的文件不存在!",CXListBox::Red);
return -3;
}
// 判断文件长度 [5/9/2008 By willing]
int nFileLen = SelectFile.GetFileLen();
CString strMsg = "";
strMsg.Format("Encrypt中获取的文件长度 %d 字节",nFileLen);
OutputDebugString(strMsg);
if (nFileLen == 0)
{
SendMsg("文件长度为0,不加密!",CXListBox::Red);
SelectFile.SetRunState(nothing);// 没有对该文件做任何处理 [5/9/2008 By willing]
return -4;
}
if (nFileLen < 0)
{
SendMsg("获取文件长度失败,无法加密!",CXListBox::Red);
SelectFile.SetRunState(error);// 没有对该文件做任何处理 [5/9/2008 By willing]
return -5;
}
// 如果要生成的文件已经存在,则删除 [5/8/2008 By willing]
CString strEncFileName = SelectFile.GetEncFilePatchName();
if (_access(strEncFileName,0) == 0)
{
remove(strEncFileName);
}
CFile encFile;
if (FALSE == encFile.Open(strEncFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))
{
SendMsg("加密文件时创建新文件失败!",CXListBox::Red);
return -4;
}
int nRe = WriteEncFileHead(encFile,strHeadXml,strPwd);
if (0 != nRe)
{
SendMsg("加密文件时写文件失败!",CXListBox::Red);
return -4;
}
// 开始对文件内容进行加密 [5/9/2008 By willing]
nRe = EncFileCount(encFile,SelectFile,nEncSuanFa,strPwd,nFileLen);
if (0 != nRe)
{
if (-3 == nRe)
{
// 用户强制终止 [5/14/2008 By willing]
// 加密失败 [5/9/2008 By willing]
encFile.Close();
if (_access(strEncFileName,0) == 0)
{
remove(strEncFileName);
}
return -6;
}
// 加密失败 [5/9/2008 By willing]
encFile.Close();
if (_access(strEncFileName,0) == 0)
{
remove(strEncFileName);
}
return -5;
}
encFile.Close();
return 0;
}
CString CEncryptSelectFile::CreateEncFileHeadXml(CSelectFile SelectFile,
const CString strPwd,
const int nEncSuanFa,
const int nEncType)
{
if (SelectFile.IsFile() == FALSE)
{
return "";
}
CString strFormat = "<CONFIG>"
"<ENCTYPE>%d</ENCTYPE>"
"<ENCMETHOD>%d</ENCMETHOD>"
"<PWD>%s</PWD>"
"<HOSTCODE>%s</HOSTCODE>"
"<FILELEN>%d</FILELEN>"
"<FILETYPE>%s</FILETYPE>"
"<FILENAME>%s</FILENAME>"
"</CONFIG>";
CString strRe = "";
CString strFileExt = "";
if (SelectFile.GetFileExt(strFileExt) != 0)
{
return "";
}
DWORD filelen = SelectFile.GetFileLen();
if (filelen<= 0)
{
return "";
}
CString strHostCode = "";
if (0 != nEncType)
{
CToolsKit ToolsKit;
strHostCode = ToolsKit.GetHostCode();
}
strRe.Format(strFormat,nEncType,nEncSuanFa,strPwd,strHostCode,filelen,strFileExt,SelectFile.GetFileName());
return strRe;
}
// 向进度窗口传递加密过程中的详细信息 [5/8/2008 By willing]
void CEncryptSelectFile::SendMsg(CString strMsg,const int nTextColor)
{
if (NULL == hWnd)
{
return;
}
if ("" == strMsg)
{
return;
}
ENC_MSG* ptrEncMsg = new ENC_MSG;
if (NULL == ptrEncMsg)
{
return;
}
ZeroMemory(ptrEncMsg,sizeof(ENC_MSG));
sprintf(ptrEncMsg->szMsg,"%s",strMsg);
ptrEncMsg->nTextColor = nTextColor;
::PostMessage(hWnd,WM_SENDMSG,(WPARAM)ptrEncMsg,NULL);
return ;
}
// 写生成的文件的头文件 [5/8/2008 By willing]
int CEncryptSelectFile::WriteEncFileHead(CFile &encFile,const CString strXML,const CString strPwd)
{
// 获取XML的MD5签名 [5/8/2008 By willing]
CMD5Checksum MD5Checksum;
CString strXMLMD5 = MD5Checksum.GetMD5(strXML);
if (strXMLMD5 == "")
{
return -1;
}
CToolsKit ToolsKit;
int nXmlLen = strXML.GetLength();
char* pszXml = new char[nXmlLen+1];
if (NULL == pszXml)
{
return -2;
}
memset(pszXml,0,nXmlLen+1);
sprintf(pszXml,"%s",strXML);
char* pszOut = new char[nXmlLen*2];
if (NULL == pszOut)
{
delete pszXml;
pszXml = NULL;
}
memset(pszOut,0,nXmlLen*2);
BOOL bFlag = FALSE;
if ("" == strXML)
{
// 没有用密码进行加密 [5/8/2008 By willing]
bFlag = ToolsKit.Encrypt(pszOut,pszXml,nXmlLen,DES_MIYAO,strlen(DES_MIYAO));
}else{
// 加密时使用了密码,则用密码的MD5值做密钥 [5/9/2008 By willing]
CString strPwdMd5 = MD5Checksum.GetMD5(strPwd);
strPwdMd5 = strPwdMd5.Left(8);
char szPwdMD5[9]={0};
sprintf(szPwdMD5,"%s",strPwdMd5);
bFlag = ToolsKit.Encrypt(pszOut,pszXml,nXmlLen,szPwdMD5,strlen(szPwdMD5));
}
// 判断加密是否成功 [5/9/2008 By willing]
if (FALSE == bFlag)
{
return -3;
}
long len = strlen(pszOut);
CString str = "";
str.Format("要写入的加密后的头长度 %d",len);
OutputDebugString(str);
encFile.Write(&len,sizeof(long));
encFile.Write(pszOut,len);
encFile.Write(strXMLMD5,strXMLMD5.GetLength());
if (NULL != pszOut)
{
delete pszOut;
pszOut = NULL;
}
if (NULL != pszXml)
{
delete pszXml;
pszXml = NULL;
}
return 0;
}
// 对文件内容实现加密 [5/9/2008 By willing]
int CEncryptSelectFile::EncFileCount(CFile &encFile,
CSelectFile SelectFile,
const DWORD dwEncType,
const CString strPwd,
const int nFileLen)
{
int nReCode = 0;
if (ENCTYPE_DES == dwEncType)
{
OutputDebugString("ENCTYPE_DES");
nReCode = DesEncrypt(encFile,SelectFile,strPwd,nFileLen);
}else if (ENCTYPE_3DES == dwEncType)
{
OutputDebugString("ENCTYPE_3DES");
nReCode = Des3Encrypt(encFile,SelectFile,strPwd,nFileLen);
}
return nReCode;
}
int CEncryptSelectFile::DesEncrypt(CFile &encFile,
CSelectFile SelectFile,
const CString strPwd,
const int nFileLen)
{
CString strMsg = "";
CFile inFile;
if (inFile.Open(SelectFile.GetFilePatchName(),CFile::modeRead|CFile::typeBinary) == FALSE)
{
strMsg = "打开待加密文件失败!";
SendMsg(strMsg,CXListBox::Red);
inFile.Close();
return -1;
}
SendMessage(hWnd,WM_FILELEN,(WPARAM)nFileLen,NULL);// 报告文件长度 [5/9/2008 By willing]
// 定义输入缓冲区 [5/9/2008 By willing]
char szIn[BUFFER_SIZE]={0};
// 定义输出缓冲区 [5/9/2008 By willing]
char szOut[OUT_BUFFER_SIZE]={0};
UINT unTemp = inFile.Read(szIn,BUFFER_SIZE);
UINT unTotal = 0;
BOOL bFlag = FALSE;
CMD5Checksum MD5Checksum;
CString strPwdMD5 = "";
// 构造密钥 [5/9/2008 By willing]
if ("" == strPwd)
{
strPwdMD5 = (MD5Checksum.GetMD5(DES_MIYAO)).Left(8);
}else{
strPwdMD5 = (MD5Checksum.GetMD5(strPwd)).Left(8);
}
char szPwdMd5[9]={0};
sprintf(szPwdMd5,"%s",strPwdMD5);
CToolsKit ToolsKit;
OutputDebugString("开始循环");
strMsg.Format("unTemp = %d",unTemp);
OutputDebugString(strMsg);
int nReCode = 0;
while (0 != unTemp)
{
if (::WaitForSingleObject(g_ExitFlag.m_EncExitFlag,0) == WAIT_OBJECT_0)
{
// 检测到需要退出 [5/14/2008 By willing]
::ResetEvent(g_ExitFlag.m_EncExitFlag);
OutputDebugString("检测到需要退出");
nReCode = -1;
break;
}
//DES加密
bFlag = ToolsKit.Encrypt(szOut,szIn,unTemp,szPwdMd5,8);
if (FALSE == bFlag)
{
// 加密失败 [5/9/2008 By willing]
inFile.Close();
return -2;
}
encFile.Write(szOut,OUT_BUFFER_SIZE);
encFile.Flush();
unTotal += unTemp;
// 报告进度 [5/9/2008 By willing]
SendMessage(hWnd,WM_CURPOS,(WPARAM)unTotal,NULL);
memset(szIn,0,BUFFER_SIZE);
memset(szOut,0,OUT_BUFFER_SIZE);
unTemp = inFile.Read(szIn,BUFFER_SIZE);
}
// 判断是否是用户终止 [5/14/2008 By willing]
if (-1 == nReCode)
{
OutputDebugString("用户终止");
inFile.Close();
return -3;
}
// 判断是否加密成功 [5/9/2008 By willing]
if ((int)unTotal != nFileLen)
{
inFile.Close();
return -4;
}
inFile.Close();
return 0;
}
int CEncryptSelectFile::Des3Encrypt(CFile &encFile,
CSelectFile SelectFile,
const CString strPwd,
const int nFileLen)
{
CString strMsg = "";
CFile inFile;
if (inFile.Open(SelectFile.GetFilePatchName(),CFile::modeRead|CFile::typeBinary) == FALSE)
{
strMsg = "打开待加密文件失败!";
SendMsg(strMsg,CXListBox::Red);
inFile.Close();
return -1;
}
SendMessage(hWnd,WM_FILELEN,(WPARAM)nFileLen,NULL);// 报告文件长度 [5/9/2008 By willing]
// 定义输入缓冲区 [5/9/2008 By willing]
char szIn[BUFFER_SIZE]={0};
// 定义输出缓冲区 [5/9/2008 By willing]
char szOut[OUT_BUFFER_SIZE]={0};
UINT unTemp = inFile.Read(szIn,BUFFER_SIZE);
UINT unTotal = 0;
BOOL bFlag = FALSE;
CMD5Checksum MD5Checksum;
CString strPwdMD5 = "";
// 构造密钥 [5/9/2008 By willing]
if ("" == strPwd)
{
strPwdMD5 = (MD5Checksum.GetMD5(DES_MIYAO)).Left(16);
}else{
strPwdMD5 = (MD5Checksum.GetMD5(strPwd)).Left(16);
}
char szPwdMd5[17]={0};
sprintf(szPwdMd5,"%s",strPwdMD5);
CToolsKit ToolsKit;
OutputDebugString("开始循环");
strMsg.Format("unTemp = %d",unTemp);
OutputDebugString(strMsg);
int nReCode = 0;
while (0 != unTemp)
{
if (WaitForSingleObject(g_ExitFlag.m_EncExitFlag,1) == WAIT_OBJECT_0)
{
// 检测到需要退出 [5/14/2008 By willing]
::ResetEvent(g_ExitFlag.m_EncExitFlag);
nReCode = -1;
break;
}
//3DES加密
bFlag = ToolsKit.Encrypt(szOut,szIn,unTemp,szPwdMd5,16);
if (FALSE == bFlag)
{
// 加密失败 [5/9/2008 By willing]
inFile.Close();
return -2;
}
encFile.Write(szOut,OUT_BUFFER_SIZE);
encFile.Flush();
unTotal += unTemp;
// 报告进度 [5/9/2008 By willing]
SendMessage(hWnd,WM_CURPOS,(WPARAM)unTotal,NULL);
memset(szIn,0,BUFFER_SIZE);
memset(szOut,0,OUT_BUFFER_SIZE);
unTemp = inFile.Read(szIn,BUFFER_SIZE);
}
// 判断是否是用户终止 [5/14/2008 By willing]
if (-1 == nReCode)
{
inFile.Close();
return -3;
}
// 判断是否加密成功 [5/9/2008 By willing]
if ((int)unTotal != nFileLen)
{
inFile.Close();
return -4;
}
inFile.Close();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -