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

📄 fileprocess.cpp

📁 加密技术VC++
💻 CPP
字号:
// FileProcess.cpp: implementation of the CFileProcess class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CtxMenu.h"
#include "FileProcess.h"
#include "SHUtils.h"

#include "pch.h"
#include "hex.h"
#include "default.h"
#include "files.h"

#include "Registry.h"

#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <memory>
#include <exception>
#include <list>

#if (_MSC_VER >= 1000)
#include <crtdbg.h>		// for the debug heap
#endif


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

using namespace CryptoPP;
using namespace std;

/////////////////////////////////////////////////////////////////////////////
// CFileProcess construction
CFileProcess::CFileProcess() 
{

	CFileCrypt32App* pcCryptor = (CFileCrypt32App*) AfxGetApp();

	// Pass Phrase
	m_csPassPhrase = pcCryptor->m_csPassPhrase;

	// Some variables
	m_bGoodPassPhrase = TRUE;
	m_bAllOk = TRUE;

}

void CFileProcess::FindDirFiles(CString csDirPath, ThreadInfo *pThreadInfo)
{

	WIN32_FIND_DATA wfd;
	HANDLE hFind;
	CString csText = _T("");

	// Check if the last char is a back-slash
	// (If not, put it there)
	if (csDirPath.Right(1) != "\\")
		csDirPath += _T("\\");

	// set the variable and add an astrix for 
	// the beginning of the directory search.
	csText = csDirPath + _T("*");

	// Iterate through dirs
	hFind = FindFirstFile(csText, &wfd);
	if (hFind != INVALID_HANDLE_VALUE) {
		do {
			
			// Check if "." or "..", if not...
			// Check if its a directory.
			if ((strcmp(wfd.cFileName,_T("."))) && (strcmp(wfd.cFileName,_T(".."))) && 
				(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {

				CString csDirIn = _T("");

				// Set to the directory found.
				csDirIn = csDirPath + wfd.cFileName;

				// Call this function again.
				FindDirFiles(csDirIn, pThreadInfo);
			}
		} while (FindNextFile(hFind, &wfd));
		
		// This is a MUST
		FindClose(hFind);
	}

	// Iterate through files
	//
	// set the variable and add an astrix-dot-astrix "*.*"
	// for the beginning of the file search.
	csText = csDirPath + _T("*.*");
	hFind = FindFirstFile(csText, &wfd);
	if (hFind != INVALID_HANDLE_VALUE) {
		do {
			
			// If NOT a directory...
			if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {

				CString csIn = _T("");
				CString csOut = _T("");

				// Set to the file found.
				csIn = csDirPath + wfd.cFileName;
				
				if (!pThreadInfo->bEncrypt) {
					csOut = csIn.Left(csIn.GetLength() - 4);
				}
				else {
					csOut = csIn + ".enf";
				}

				// Now, add the filename into the Array.
				m_csFileArrayIn.Add(csIn);
				m_csFileArrayOut.Add(csOut);
			}
		} while (FindNextFile(hFind, &wfd));

		// This is a MUST
		FindClose(hFind);

	}
}

void CFileProcess::SetAttributes(CString csFilename, BYTE btAttrib) 
{
	char* pFileName = csFilename.GetBuffer(csFilename.GetLength() + 1);
	CFileStatus status;

	// This is a MUST
	status.m_mtime = 0;

	// Set the file attribute member
	status.m_attribute = btAttrib;

	// Set the file attribute
	CFile::SetStatus( pFileName, status );
}

BYTE CFileProcess::GetAttributes(CString csFilename) 
{
	char* pFileName = csFilename.GetBuffer(csFilename.GetLength() + 1);
	CFileStatus status;

	// Get the file attribute
	CFile::GetStatus( pFileName, status );

	// return the file attribute
	return status.m_attribute;
}

BOOL CFileProcess::DoesFileExist(CString csFilename) 
{

	char* pFileName = csFilename.GetBuffer(csFilename.GetLength() + 1);
	CFileStatus status;

	// Return the existance of the file
	return (CFile::GetStatus( pFileName, status ));

}

int CFileProcess::ProcessFile(ThreadInfo *pThreadInfo) 
{

	CFileCrypt32App* pcCryptor = (CFileCrypt32App*) AfxGetApp();

	int res = 0;

	// This means can we proceed...
	if (pcCryptor->bDoIt) {

		CString csOut = _T("");
		CString csIn = _T("");

		// Get the file/directory name
		csIn = GET_SAFE(pThreadInfo->csFile);

		CStringArray csaTmp;

		csIn.MakeLower();

		CRegistry Reg;

		CString csKey = _T("");
		CString csKey1 = _T("");
		CString csSystemFolder = _T("");

		csKey = _T("Software\\Microsoft\\Windows NT\\CurrentVersion");
		csKey1 = _T("Software\\Microsoft\\Windows\\CurrentVersion");

		Reg.SetRootKey(HKEY_LOCAL_MACHINE);

		if (Reg.SetKey(csKey, FALSE)) {

			csSystemFolder = Reg.ReadString("SystemRoot", _T(""));

			if (csSystemFolder.IsEmpty()) {
				Reg.SetKey(csKey1, FALSE);
				csSystemFolder = Reg.ReadString("SystemRoot", _T(""));
			}

			csSystemFolder.MakeLower();

			// Lets make sure we do not encrypt files in the Windows Directory
			if (csIn.Left(csSystemFolder.GetLength()) != csSystemFolder) {
				
				BYTE btRet = 0x00;

				m_bGoodPassPhrase = TRUE;

				BOOL bContinue = TRUE;

				if (!pThreadInfo->bDir) {

					if (!csIn.IsEmpty()) {

						if (!pThreadInfo->bEncrypt) {
							if (csIn.Right(3) != "enf") {
								MessageBox(NULL, "This program is unable to Decrypt a file that is not encrypted!\r\n\r\n" + csIn, "Error Input", MB_OK);
								bContinue = FALSE;
							}
							else {
								if (m_bGoodPassPhrase) {

									// Get the file attribute
									btRet = GetAttributes(csIn);

									// Set the file attribute to normal
									SetAttributes(csIn, 0x00);

									// New files name
									csOut = csIn.Left(csIn.GetLength() - 4);
									DecryptFile(csIn, csOut);

									// Set the file attribute back to
									// what it was before the operation
									if (DoesFileExist(csOut))
										SetAttributes(csOut, btRet);
								}
							}
						}
						else {
							btRet = GetAttributes(csIn);
							SetAttributes(csIn, 0x00);

							csOut = csIn + ".enf";
							EncryptFile(csIn, csOut);

							if (DoesFileExist(csOut))
								SetAttributes(csOut, btRet);
						}

						if (bContinue) {
							if (m_bGoodPassPhrase) {
								// Delete the file that was the source
								if (DoesFileExist(csOut))
									DeleteFile(csIn);
							}
						}
					}
				}
				else {

					FindDirFiles(csIn, pThreadInfo);
					
					int nUBound = m_csFileArrayIn.GetUpperBound();
					int n = 0;

					for (n = 0; n<=nUBound; n++) {
						csIn = m_csFileArrayIn.GetAt(n);
						csOut = m_csFileArrayOut.GetAt(n);

						if (!pThreadInfo->bEncrypt) {
							if (csIn.Right(3) != "enf") {
								MessageBox(NULL, "This program is unable to Decrypt a file that is not encrypted!\r\n\r\n" + csIn, "Error Input", MB_OK);
								for (int nn = 0; nn<=nUBound; nn++) {
									if (m_csFileArrayIn.GetAt(nn) == csIn) {
										m_csFileArrayIn.SetAt(nn, _T("ERROR"));
										m_csFileArrayOut.SetAt(nn, _T("ERROR"));
										break;
									}
								}
							}
							else {
								if (m_bGoodPassPhrase) {

									btRet = GetAttributes(csIn);
									SetAttributes(csIn, 0x00);

									csOut = csIn.Left(csIn.GetLength() - 4);
									DecryptFile(csIn, csOut);

									// Set the file attribute back to
									// what it was before the operation
									if (DoesFileExist(csOut))
										SetAttributes(csOut, btRet);
								}
							}
						}
						else {
							btRet = GetAttributes(csIn);
							SetAttributes(csIn, 0x00);

							csOut = csIn + ".enf";
							EncryptFile(csIn, csOut);

							if (DoesFileExist(csOut))
								SetAttributes(csOut, btRet);
						}

					}
					
					if (m_bGoodPassPhrase) {
						for (n = 0; n<=nUBound; n++) {
							csIn = m_csFileArrayIn.GetAt(n);
							if (csIn != _T("ERROR")) {
								// Delete the file that was the source
								if (DoesFileExist(csOut))
									DeleteFile(csIn);
							}
						}

						if (!m_bAllOk)
							if (!pThreadInfo->bEncrypt)
								MessageBox(NULL, "Not All files have been decrypted!", "Errors", MB_OK);

					}

				}
			}
			else {
				MessageBox(NULL, "This program does not encrypt files in the Windows Directory!", "Warning", MB_OK);
			}

		}
		else {
			MessageBox(NULL, "Problem Setting the registry key!", "Problems!", MB_OK);
		}
	}

	return res;
}

void CFileProcess::EncryptFile(CString csIn, CString csOut)
{
	m_bGoodPassPhrase = TRUE;
	FileSource f(csIn, true, new DefaultEncryptorWithMAC(m_csPassPhrase, new FileSink(csOut)));
}

void CFileProcess::DecryptFile(CString csIn, CString csOut)
{
	
	if (m_bGoodPassPhrase) {

		int nUBound = m_csFileArrayIn.GetUpperBound();

		DefaultDecryptorWithMAC *p;
		FileSource file(csIn, false, p = new DefaultDecryptorWithMAC(m_csPassPhrase));
		file.Pump(256);
		if (p->CurrentState() != DefaultDecryptorWithMAC::KEY_GOOD) {
			
			MessageBox(NULL, "Incorrect passphrase.", "Error", MB_OK);
			
			m_bGoodPassPhrase = FALSE;
			m_bAllOk = FALSE;
			
			if (nUBound != -1) {
				for (int n = 0; n<=nUBound; n++) {
					if (m_csFileArrayIn.GetAt(n) == csIn) {
						m_csFileArrayIn.SetAt(n, _T("ERROR"));
						m_csFileArrayOut.SetAt(n, _T("ERROR"));
						break;
					}
				}
			}
			return;
		}

		file.Attach(new FileSink(csOut));
		file.PumpAll();
		file.Close();
		if (p->CurrentState() != DefaultDecryptorWithMAC::MAC_GOOD) {

			MessageBox(NULL, "Invalid MAC. The file may have been tempered with.", "Error", MB_OK);
			
			m_bGoodPassPhrase = FALSE;
			m_bAllOk = FALSE;

			if (nUBound != -1) {
				for (int n = 0; n<=nUBound; n++) {
					if (m_csFileArrayIn.GetAt(n) == csIn) {
						m_csFileArrayIn.SetAt(n, _T("ERROR"));
						m_csFileArrayOut.SetAt(n, _T("ERROR"));
						break;
					}
				}
			}
		}
	}
}

void CFileProcess::Split(CString Source, CString Deliminator, CStringArray& AddIt, BOOL bAddEmpty)
{
	// initialize the variables
	CString		 newCString = Source;
	CString		 tmpCString = "";
	CString		 AddCString = "";

	int pos1 = 0;
	int pos = 0;

	AddIt.RemoveAll();

	if (Deliminator.IsEmpty()) {
		// Add default [comma] if empty!
		// acknowledgement: Prasad [gprasad@rti.ie]
		Deliminator = ","; 
	}

	// do this loop as long as you have a deliminator
	do {
		// set to zero
		pos1 = 0;
		// position of deliminator starting at pos1 (0)
		pos = newCString.Find(Deliminator, pos1);
		// if the deliminator is found...
		if ( pos != -1 ) {

			// load a new var with the info left
			// of the position
			CString AddCString = newCString.Left(pos);// - 1);

			if (!AddCString.IsEmpty()) {
				// if there is a string to add, then
				// add it to the Array
				AddIt.Add(AddCString);
			}
			else if (bAddEmpty) {
				// if empty strings are ok, then add them
				AddIt.Add(AddCString);
			}

			// make a copy of the of this var. with the info
			// right of the deliminator
			tmpCString = newCString.Mid(pos + Deliminator.GetLength());
			
			// reset this var with new info
			newCString = tmpCString;
		}
	} while ( pos != -1 );
	
	if (!newCString.IsEmpty()) {
		// as long as the variable is not emty, add it
		AddIt.Add(newCString);
	}

}

// this is the worker thread function.
UINT FileProcessThreadFunc(LPVOID data)
{
	if (data==NULL) {
		AfxEndThread(0);
		ASSERT(0);
	}

	ThreadInfo *pThreadInfo = (ThreadInfo *)data;

	// we're not done yet!
	SET_SAFE(pThreadInfo->bDone, FALSE);

	// no errors
	SET_SAFE(pThreadInfo->iErr, 0);

	// process that file
	CFileProcess fp;
	int res = fp.ProcessFile(pThreadInfo);

	// tell the caller about any errors
	SET_SAFE(pThreadInfo->iErr, res);

	// now we're done.
	// the main thread is watching this value. when it sees
	// we've set bDone = TRUE, it will stop watching us
	SET_SAFE(pThreadInfo->bDone, TRUE);

	return 0;
}

⌨️ 快捷键说明

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