Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

MFC PowerShells Easily: Encrypt and Decrypt

1.33/5 (2 votes)
27 Feb 2015CPOL3 min read 8.9K   120  
An easy way to encrypt files that are automatically decrypted in the same user account

Introduction

The purpose of this tip is to demonstrate how easy it is to use the MFC class CPowerShell from http://www.codeproject.com/Articles/880154/MFC-PowerShells-Easily for tasks like encrypting and decrypting files (see below) that otherwise require extensive and many times complicated code.

The Encrypt method is explained here, where we can read this:

The Encrypt method allows you to encrypt a file so that only the account used to call this method can decrypt it.

You'll need the Visual Studio solution from the article mentioned above. To run the scripts below, press Ctrl+A and start typing the new code/script (better yet, copy and paste), and then press Alt+R+R or just F5.

Added on 02/28/2015: To make things simpler, I uploaded a zipped file with a Visual Studio solution demonstrating the use of the code below. Instead of declaring CPowerShell variable a data member of the class, I chose to declare one in each button click event handler.

Using the Code

First of all, it's necessary to warn you that I'm talking about encrypted files that are automatically decrypted by anyone that logs into your account (successfully, of course). This kind of encryption protects files against theft (some takes away your HD), and against other users registered on the same computer. How can this be done in C++? I have no idea, even using MFC! Nevertheless, using PowerShell through the splendid, magnific, etc... (LOL) CPowerShell MFC class, this task is ridiculously simple.

Let's suppose you want to encrypt TXT files contained in the C:/temp folder. Then, just run this script:

Warning: By the end of the script below, the Windows 8.1 Operating System will present to you a wizard to save credentials (if you haven't done it yet). I recommend you follow the default choices presented by the wizard.

$folder='c:/temp'
dir $folder *txt| 
    % encrypt

That's all!

Don't forget to press Alt+R+R (or just F5) if you are using my MFC code from the other article.

Check that the files encrypted do appear in File Explorer with a distinguishing color.

But, maybe, this is not what you want. Maybe, you want your client (the one that'll be using your MFC application) have a chance to choose which files to encrypt. No problem, just "borrow" the PowerShell window implemented in the Out-GridView cmdlet, and you are done:

$folder='c:/temp'
dir $folder *txt| 
    ogv -title 'Choose the files to be encrypted' -pass| 
    % encrypt

Once more, that's all!

Now, test what has been done:

  • Using File Explorer, double click the encrypted file(s) and check that they are open just like any non-encrypted file, i.e., without any decryption key.
  • Log off your account, and log into any other, and try to open any of those previously encrypted files - you'll have access negated!

If you ever need to decrypt any file, just run this very simple script:

$folder='c:/temp'
dir $folder *txt -attributes encrypted|
    ogv -title 'Choose the files to be decrypted' -pass|
    % decrypt

Points of Interest

CPowerShell MFC class makes these tasks ridiculously simple, through the powerful PowerShell scripting engine. This is CROM (LOL) technology in action.

Even C#-pians don't have such an easy time to accomplish this task.

History

  • 27th February, 2015: Initial version
  • 28th February, 2015: Added a VS solution to simplify testing the code

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)