📄 bodyprotectdlg.cpp
字号:
{
MessageBox("You must select a window to active!",
"Error",MB_OK|MB_APPLMODAL);
return;
}
HWND hWnd=(HWND)list->GetItemData(idx);
::PostMessage(hWnd,WM_QUIT,0,0L);
//EndDialog(IDOK);
}
void CBodyProtectDlg::OnBtnConfig()
{
// TODO: Add your control notification handler code here
CDlgPassInput dlgPass;
if(dlgPass.DoModal()==IDCANCEL) return;
CString strPass=dlgPass.m_strPass;
DESServer des;
CString strPassSaved=des.DecryptString(c_configData.m_strPass);
if(strPass.Compare(strPassSaved)!=0) {
MessageBox("密码错误,不能进入配置功能!","提示",MB_OK|MB_ICONEXCLAMATION);
strPass.Empty();
strPassSaved.Empty();
return;
}
CDlgConfig dlg;
dlg.SetConfigData(&c_configData);
if(dlg.DoModal()==IDOK){
CFileFind find;
CString strFile=m_strAppPath+"\\Config.dat";
c_configData.WriteConfigFile(strFile);
}
}
CConfigData::CConfigData(){
m_strLabel="BodyProtectConfigure";
m_strExeCode="未定义";
m_arrFiles.SetSize(0,1);
m_strPass="02405857";
DESServer des;
m_strPass=des.EncryptString(m_strPass);
};
bool CConfigData::ReadConfigFile(CString strFile)
{
CFileFind find;
if(!find.FindFile(strFile)) return false;
CFile file;
int nValue;
nValue=m_strLabel.GetLength();
//读入标记
file.Open(strFile,CFile::modeRead);
if(file.GetLength()<nValue) return false;
//读入标记
LPTSTR pStr;
pStr=m_strLabel.GetBuffer(nValue);
file.Read(pStr,nValue);
m_strLabel=pStr;
if(m_strLabel.CompareNoCase("BodyProtectConfigure")!=0) return false;
//读入初始密码
CString strPassEnc;
file.Read(&nValue,4);
pStr=strPassEnc.GetBuffer(nValue);
file.Read(pStr,nValue);
strPassEnc=pStr;
m_strPass=strPassEnc;
//读入可执行程序特征码
file.Read(&nValue,4);
m_strExeCode.Empty();
pStr=m_strExeCode.GetBuffer(nValue);
file.Read(pStr,nValue);
m_strExeCode=pStr;
//读入文件数目和文件名
file.Read(&nValue,4);
for(int i=0;i<nValue;i++){
int nValue2;
file.Read(&nValue2,4);
CString strFile;
pStr=strFile.GetBuffer(nValue2);
file.Read(pStr,nValue2);
strFile=pStr;
m_arrFiles.Add(strFile);
}
file.Close();
return true;
}
bool CConfigData::WriteConfigFile(CString strFile)
{
CFile file;
CFileException e;
if(!file.Open(strFile,CFile::modeWrite|CFile::modeCreate,&e)){
return false;
}
int nValue;
//写入标记
nValue=m_strLabel.GetLength();
//file.Write(&nValue,4);
file.Write(m_strLabel,nValue);
//写入初始密码
//DESServer des;
//CString strPassEnc=des.EncryptString(m_strPass);
nValue=m_strPass.GetLength();
file.Write(&nValue,4);
file.Write(m_strPass,nValue);
//写入可执行程序特征码
nValue=m_strExeCode.GetLength();
file.Write(&nValue,4);
file.Write(m_strExeCode,nValue);
//写入文件数目和文件名
nValue=m_arrFiles.GetSize();
file.Write(&nValue,4);
for(int i=0;i<nValue;i++){
int nValue2;
nValue2=m_arrFiles[i].GetLength();
file.Write(&nValue2,4);
file.Write(m_arrFiles[i],nValue2);
}
file.Close();
return true;
}
void CBodyProtectDlg::CloseProcesses()
{
GetProcessList();
int nList=m_listProcess.GetCount();
for(int i=0;i<nList;i++){
int idx=i;
HWND hWnd=(HWND)(m_listProcess.GetItemData(idx));
::PostMessage(hWnd,WM_CLOSE,0,0L);
}
GetProcessList();
}
void CBodyProtectDlg::EncryFiles()
{
int nFiles=c_configData.m_arrFiles.GetSize();
for(int i=0;i<nFiles;i++){
CString strSrcFileFull=c_configData.m_arrFiles[i];
CString strDestFile=GetFileNameFromFullPath(strSrcFileFull);
CString strDestFileFull=m_strAppPath+"\\EncData\\"+strDestFile+".enc";
EncryptDecryptFile(strSrcFileFull,strDestFileFull,false);
}
}
void CBodyProtectDlg::DecryptFiles()
{
int nFiles=c_configData.m_arrFiles.GetSize();
for(int i=0;i<nFiles;i++){
CString strDestFileFull=c_configData.m_arrFiles[i];
CString strSrcFile=GetFileNameFromFullPath(strDestFileFull);
CString strSrcFileFull=m_strAppPath+"\\EncData\\"+strSrcFile+".enc";
EncryptDecryptFile(strSrcFileFull,strDestFileFull,true);
}
}
bool CBodyProtectDlg::EncryptDecryptFile(CString strFileSrc, CString strFileDest, bool bDecrypt)
{
FILE *fileRead = fopen(strFileSrc, "rb");
if (fileRead == 0) {
return false;
}
const size_t bufferSize = 1024;
int barSize = 0;
// reaching the end of the file and getting position = getting file size (bytes).
fseek(fileRead, 0, SEEK_END);
barSize = ftell(fileRead);
fseek(fileRead, 0, SEEK_SET);
if (barSize == -1) {
return false;
//threadError("Unable to get file size (_filelength).");
}
barSize = barSize / bufferSize;
// initialize ProgressBar with the size of source file (k) and step size to 1.
//SendDlgItemMessage(ID_PROGRESSBAR, PBM_SETRANGE, 0, MAKELPARAM(0, barSize+1));
//SendDlgItemMessage(ID_PROGRESSBAR, PBM_SETSTEP, 1, 0);
char outfile[MAX_PATH];
/*
if (_cipher == ENCRYPT_EXE)
{
char *temp = generateTempFile();
strcpy(outfile, temp);
delete temp;
}
else
{*/
strcpy(outfile, strFileDest);
//}
FILE *fileWrite = fopen(outfile, "wb");
if (fileWrite == 0){
return false;//threadError("Unable to open destination file.");
}
char readBuffer[bufferSize];
char outBuffer[bufferSize];
size_t readRet = 0;
CString strPass=c_configData.m_strPass;
BlowFishEnc encryption(strPass);
bool abort = false;
int encRet;
while (!feof(fileRead))
{
readRet = fread(readBuffer, sizeof(char), bufferSize, fileRead);
if (bDecrypt && (!abort))
{
encRet = encryption.decryptStream(readBuffer, (DWORD)readRet, outBuffer);
if feof(fileRead)
{
int pos = 0;
// removing trailing zeros - encrypted file must be x8 bytes.
while ((pos < 8) && ((outBuffer + encRet - pos)[0] == 0)) pos++;
// if found trailing zeros - decreasing the writing buffer marker (not writing them).
if (pos) encRet -= (pos - 1);
}
}
else if (!abort)
{
encRet = encryption.encryptStream(readBuffer, (DWORD)readRet, outBuffer);
}
fwrite(outBuffer, sizeof(char), encRet, fileWrite);
//stepProgressbar();
// check if user aborted!
if (/*(shouldTerminate()) || */abort)
{
fclose(fileRead);
fclose(fileWrite);
GenLib::FileWipe::wipeFile(outfile);
return 0;
}
}
fflush(fileWrite);
fclose(fileWrite);
fclose(fileRead);
ZeroMemory(outBuffer, bufferSize);
ZeroMemory(readBuffer, bufferSize);
GenLib::FileWipe::wipeFile((LPCTSTR)strFileSrc);
return 0;
}
void CBodyProtectDlg::wipePwds(CString& strPwd, CString& strVerify)
{
strPwd.Empty();
strVerify.Empty();
}
void CBodyProtectDlg::OnOK()
{
// TODO: Add extra validation here
OnViewMainWindow();
}
void CBodyProtectDlg::OnBtnSetpass()
{
// TODO: Add your control notification handler code here
//如果存在加密文件,则不运行修改密码
if(IsDataProtected()){
MessageBox("保护状态下不允许修改密码,请先解除系统保护!","提示信息",MB_OK);
return;
}
CDlgPassModify dlg;
if(dlg.DoModal()==IDCANCEL) return;
CString strPassNew=dlg.m_strPassNew;
CString strPassOld=dlg.m_strPassOld;
DESServer des;
CString strPassOldEnc=des.EncryptString(strPassOld);
CString strPassSaved=c_configData.m_strPass;
if(strPassOldEnc.CompareNoCase(strPassSaved)!=0) {
MessageBox("旧密码错误,修改密码不成功!","提示信息",MB_OK);
return;
}
c_configData.m_strPass=des.EncryptString(strPassNew);
CFileFind find;
CString strFile=m_strAppPath+"\\Config.dat";
c_configData.WriteConfigFile(strFile);
MessageBox("密码修改成功!","提示信息",MB_OK);
}
bool CBodyProtectDlg::IsDataProtected()
{
bool bProteced=false;
int nFiles=c_configData.m_arrFiles.GetSize();
for(int i=0;i<nFiles;i++){
CString strDestFileFull=c_configData.m_arrFiles[i];
CString strSrcFile=GetFileNameFromFullPath(strDestFileFull);
CString strSrcFileFull=m_strAppPath+"\\EncData\\"+strSrcFile+".enc";
CFileFind find;
if(find.FindFile(strSrcFileFull)) bProteced=true;
}
return bProteced;
}
void CBodyProtectDlg::SetItemState(bool bProteced)
{
CWnd* pWnd;
pWnd=GetDlgItem(IDC_BTN_PROTECT); if(pWnd) pWnd->EnableWindow(!bProteced);
pWnd=GetDlgItem(IDC_BTN_RESTORE); if(pWnd) pWnd->EnableWindow(bProteced);
pWnd=GetDlgItem(IDC_BTN_SETPASS); if(pWnd) pWnd->EnableWindow(!bProteced);
pWnd=GetDlgItem(IDC_BTN_CONFIG); if(pWnd) pWnd->EnableWindow(!bProteced);
if(bProteced)
m_TrayIcon.SetIcon(IDI_ICONTRAYNORMAL);
else
m_TrayIcon.SetIcon(IDI_ICONTRAY);
}
void CBodyProtectDlg::Protect()
{
if(IsDataProtected()) return;
//关闭应用程序
CloseProcesses();
//加密数据文件
EncryFiles();
bool bProtected=true;
SetItemState(bProtected);
}
void CBodyProtectDlg::Restore()
{
if(!IsDataProtected()) return;
CDlgPassInput dlgPass;
if(dlgPass.DoModal()==IDCANCEL) return;
CString strPass=dlgPass.m_strPass;
DESServer des;
CString strPassEnc=des.EncryptString(strPass);
CString strPassSaved=c_configData.m_strPass;
if(strPassEnc.Compare(strPassSaved)!=0) {
MessageBox("密码错误,不能恢复数据","输入提示",MB_OK|MB_ICONEXCLAMATION);
strPass.Empty();
strPassSaved.Empty();
return;
}
DecryptFiles();
bool bProtected=false;
SetItemState(bProtected);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -