The Code Project Click here for Dundas Software's TCP/IP Development Kit
Home >> Files & Folders >> General

File Stream Encryption (using a Context Menu)
By Daniel Madden

An article on file stream encryption (using Crypt++ v3.2) from an Explorer Context Menu 
 Beginner
 VC6, (SP3), Win98, NT4, W2K
 Posted 14 Aug 2000
Articles by this author
Send to a friend
Printer friendly version
Latest Articles Logon Message Boards Submit an Article
Broken links? Email us!
2 users have rated this article. result:
5 out of 5.

  • Download source files - 250 Kb
  • This article makes use of the Crypto++ library, available at http://www.eskimo.com/~weidai/cryptlib.html. To run compile the demo code you will need to download the windows version of the crypto++ library and source files.

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

    Overview
    Obtaining the Crypto++ Library
    Implementation Notes
    Acknowledgements
    Other Articles using Crypt++ v3.2

    Overview

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

    From a File (Context Menu)

    File Context Menu Screen Image

    From a Folder (Context Menu)

    File Context Menu Screen Image

    Implementation Notes

    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).

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

    I also didn't want the user to encrypt files in the Windows Directory, so I look into the registry (via Robert Pittenger's CRegistry class) and find the SystemRoot Entry and check it against the Input file path.

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

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

    That's it!

    Obtaining the Crypto++ Library

    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.  You can set these up using the Tools | Options | Directories page from the VC ide.

    Download Crypt++ v3.2

    >Acknowledgements

    Along with the Crypto++ library (see above), the demo was done by using a very good Explorer Context Menu Example written by Smaller Animals Software 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).

    The demo makes use of Robert Pittenger's CRegistry class and Chris Maunder's CHyperlink class (both classes have been included in the demo download).

    Other Articles using Crypt++ v3.2

    CLogIt by Daniel Madden.

    Coming Soon..."MFCCryptLib" (it's the Crypt++ demo (found in the Zip) with an MFC Interface).

    [Top] Rate this Article for us!     PoorExcellent  
    Hint: For improved responsiveness, use Internet Explorer 4 (or above) with Javascript enabled, choose 'Dynamic' from the View dropdown and hit 'Refresh'
     Keyword Filter
     View   Per page   Messages since
    New threadMessages 1 to 2 of 2 (Total: 2)First | Prev | Next | Last
    Subject 
    Author 
    Date 
      Brilliant But,
    Colin Davies 22:36 15 Aug 00 
      Re: Brilliant But,
    Daniel Madden (Author) 5:08 16 Aug 00 
    Last Visit: 12:00 Friday 1st January, 1999First | Prev | Next | Last

    Home >> Files & Folders >> General
    last updated 14 Aug 2000
    Article content copyright Daniel Madden, 2000
    everthing else © CodeProject, 1999-2001.
    The Code Project Click here for the free Dundas Upload control