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

📄 filecrypt32.html

📁 加密技术VC++
💻 HTML
字号:
<!-- The Code Project article submission template (HTML version)

 Using this template will help us post your article sooner. We are using MS 
 IIS and ASP pages on the server, allowing us to simplify the templates used 
 to create the articles.

 To fill in this template, just follow the 3 easy steps below:

   1. Fill in the article description details
   2. Add links to your images and downloads
   3. Include the main article text

 That's all there is to it! All formatting will be done by the ASP script 
 engine.
-->
<html>

<head>
<title>The Code Project</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>
BODY { FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; FONT-SIZE: 10pt }
H2 { COLOR: #0066ff; FONT-SIZE: 13pt; FONT-WEIGHT: bold; }
H3 { COLOR: #0066ff; FONT-SIZE: 12pt; FONT-WEIGHT: bold; font-style: italic; }
H3 { COLOR: #0066ff; FONT-SIZE: 11pt; FONT-WEIGHT: bold; }
PRE { BACKGROUND-COLOR: #FBEDBB; FONT-FAMILY: "Courier New", Courier, mono; WHITE-SPACE: pre; }
CODE { COLOR: #990000; FONT-FAMILY: "Courier New", Courier, mono; }
</style>
</head>

<body bgcolor="#FFFFFF" color="#000000">
<!-- STEP 1. Fill in the details (CodeProject will reformat this section for you) -->

<pre>
Title:       File Stream Encryption (using a Context Menu)
Author:      Daniel Madden 
Email:       daniel.madden@compaq.com
Environment: VC++ 6.0 (SP3), Win 98, WinNT 4.0, Win2000
Keywords:    Control, Dialog, MFC, DLL
Level:       Beginner
Description: An article on file stream encryption (using Crypt++ v3.2) from an Explorer Context Menu
Section      Files &amp; Folders
SubSection   General
</pre>

<hr width="100%" noshade>

<p><em>This article makes use of the Crypto++ library, available at <a
href="http://www.eskimo.com/~weidai/cryptlib.html">http://www.eskimo.com/~weidai/cryptlib.html</a>.
<b>To run compile the demo code you will need to download the windows version of the crypto++
library and source files.</b></em></p>

<p>I suggest you read the "<b>Readme.txt</b>" file (found in the zip file) which contains a very good explination 
on Debugging this dll along with source notes.</p>

<p>
<a href="#Overview">Overview</a><br>
<a href="#Obtaining">Obtaining the Crypto++ Library</a><br>
<a href="#Implementation">Implementation Notes</a><br>
<a href="#Acknowledge">Acknowledgements</a><br>
<a href="#OtherLinks">Other Articles using Crypt++ v3.2</a><br>
</p>

<p><a name="Overview"></a></p>

<hr>

<h2>Overview</h2>

<p>This was put together because I wanted to implement the file stream encryption (Crypt++ Library v3.2) from a Context Menu. </p>

<p>From a File (Context Menu)</p>
<p><img src="FileCrypt321.gif" alt="File Context Menu Screen Image"></p>

<p>From a Folder (Context Menu)</p>
<p><img src="FileCrypt322.gif" alt="File Context Menu Screen Image"></p>

<p><a name="Implementation"></a></p>

<hr>

<h2>Implementation Notes</h2>

<p>Because I was en/decrypting files, I needed to preserve the Attributes of the file(s)...and check if the file was created Ok (this was 
necessary because if the file was not created (e.g.: Wrong PassPhrase), I didn't want to try and set the attributes back to what the original 
File was).</p>

<pre>
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 ));

}
</pre>

<p>I also didn't want the user to encrypt files in the Windows Directory, so I look into the registry (via 
<A href="http://codeproject.com/system/registry.asp">Robert Pittenger's 
CRegistry</A> class) and find the SystemRoot Entry and check it against the Input file path.</p>

<p>I also created a Function to Rercursive through the directories and en/decrypt the files...</p>

<pre>
void CFileProcess::FindDirFiles(CString csDirPath)
{

	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;

				// Recursively search
				FindDirFiles(csDirIn);
			}
		} 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 we are decrypting...
				if (!pThreadInfo->bEncrypt) {
					csOut = csIn.Left(csIn.GetLength() - 4);
				}
				else { // we are encrypting...just appent the extention
					csOut = csIn + ".enf";
				}

				// Now, add the filename into the Array.
				//
				// this will be used later when we 
				// actually perform the en/decryption
				m_csFileArrayIn.Add(csIn);
				m_csFileArrayOut.Add(csOut);
			}
		} while (FindNextFile(hFind, &wfd));

		// This is a MUST
		FindClose(hFind);

	}
}
</pre>

<p>That's it!</p>

<p><a name="Obtaining"></a></p>

<hr>

<h2>Obtaining the Crypto++ Library</h2>

<p>The simplest way to compile the demo application, having downloaded the Crypto++ files,
is to provide the compiler with the paths needed to find the header files and the compiled
library.&nbsp; You can set these up using the Tools | Options | Directories page from the
VC ide.</p>

<p><a href="http://www.eskimo.com/~weidai/cryptlib.html">Download Crypt++ v3.2</a> </p>

<p><a name="Acknowledge"></a></p>

<hr>

<h2>Acknowledgements</h2>

<p>Along with the Crypto++ library (see above), the demo was done by using a very good Explorer 
Context Menu Example written by <a href="http://www.smalleranimals.com">Smaller Animals Software</a> 
Copyright 1999, All Rights Reserved and this program may be freely distributed (see About Box and included Zip).
This example has been included in the demo download (Zipped).</p>

<P>The demo makes use of <A href="http://codeproject.com/system/registry.asp">Robert Pittenger's 
CRegistry</A> class and <A href="http://www.codeproject.com/miscctrl/hyperlink.asp">Chris Maunder's CHyperlink</A> 
class (both classes have been included in the demo download).</P>

<p><a name="OtherLinks"></a></p>

<hr>

<h2>Other Articles using Crypt++ v3.2</h2>

<P><A href="http://codeproject.com/file/logit.asp">CLogIt</A> by Daniel Madden.</P>
<P>Coming Soon..."MFCCryptLib" (it's the Crypt++ demo (found in the Zip) with an MFC Interface).</P>

<p>&nbsp;</p>
</body>
</html>

⌨️ 快捷键说明

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