|
Broken links? Email us! |
|
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
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)
From a Folder (Context Menu)
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!
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.
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).
CLogIt by Daniel Madden.
Coming Soon..."MFCCryptLib" (it's the Crypt++ demo (found in the Zip) with an MFC Interface).
Hint: For improved responsiveness, use Internet Explorer 4 (or above) with Javascript enabled, choose 'Dynamic' from the View dropdown and hit 'Refresh' | ||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||
|
|