Click here to Skip to main content
11,713,845 members (85,959 online)
Click here to Skip to main content

File Stream Encryption (using a Context Menu)

, 13 Aug 2000 88.3K 1.7K 49
Rate this:
Please Sign up or sign in to vote.
An article on file stream encryption (using Crypto++ v3.2) from an Explorer Context Menu.

This article makes use of the Crypto++ library, available here. 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 explanation on Debugging this DLL, along with source notes.

Overview

This was put together because I wanted to implement the file stream encryption (Crypto++ 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 recursive 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 Crypto++ 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 Crypto++ v3.2

  • CLogIt by Daniel Madden.
  • Coming Soon..."MFCCryptLib" (it's the Crypto++ demo (found in the Zip) with an MFC interface).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Dan Madden
Product Manager
Germany Germany
I have been programming (as a hobby) for 20+ years (Unix C, Scripting, VB, C/C++, C#). I am getting too old to talk about it and been in the Security line of work (both Military/Civilian) for 25+ years.

You may also be interested in...

Comments and Discussions

 
GeneralIt Shuts Off Run As Administrator Pin
TV Mogul27-Dec-09 15:00
memberTV Mogul27-Dec-09 15:00 
GeneralRe: It Shuts Off Run As Administrator Pin
Dan Madden29-Dec-09 11:33
memberDan Madden29-Dec-09 11:33 
GeneralWARNING: Your code compiles and encrypts in vs2008 on Vista BUT does something very destructive! Pin
TV Mogul27-Dec-09 12:49
memberTV Mogul27-Dec-09 12:49 
GeneralRe: WARNING: Your code compiles and encrypts in vs2008 on Vista BUT does something very destructive! Pin
Dan Madden29-Dec-09 11:35
memberDan Madden29-Dec-09 11:35 
GeneralRe: WARNING: Your code compiles and encrypts in vs2008 on Vista BUT does something very destructive! Pin
TV Mogul29-Dec-09 19:12
memberTV Mogul29-Dec-09 19:12 
GeneralCrypto++ Integration Article Pin
Jeffrey Walton8-Dec-06 18:36
memberJeffrey Walton8-Dec-06 18:36 
Questioncan this Code Convert to C#? Pin
tareqGamal5-Jan-06 11:38
membertareqGamal5-Jan-06 11:38 
AnswerRe: can this Code Convert to C#? Pin
Bandoleiro7-Dec-06 0:33
memberBandoleiro7-Dec-06 0:33 
GeneralUse this example in command prompt Pin
Adolfo Dias10-Oct-05 1:08
memberAdolfo Dias10-Oct-05 1:08 
GeneralWin2000 Pin
Simon Walton25-Feb-02 9:31
memberSimon Walton25-Feb-02 9:31 
GeneralRe: Win2000 Pin
Daniel Madden11-Apr-02 19:28
memberDaniel Madden11-Apr-02 19:28 
GeneralCrypto++ Library with an MFC Interface Pin
Daniel Madden14-Jan-02 11:54
memberDaniel Madden14-Jan-02 11:54 
GeneralBrilliant But, Pin
Colin Davies15-Aug-00 16:36
sussColin Davies15-Aug-00 16:36 
GeneralRe: Brilliant But, Pin
Daniel Madden (Author)15-Aug-00 23:08
sussDaniel Madden (Author)15-Aug-00 23:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 14 Aug 2000
Article Copyright 2000 by Dan Madden
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid