Introduction

The purpose of this article is demonstrate how to build a Windows Forms application that encrypts files through the use of the RijndaelManaged class, a symmetric algorithm, which is used to encrypt and decrypt data by using its automatically generated Key and IV. Encryption involves the creation of a cipher (an algorithm) that takes data and a generated key as its input. The algorithm will behave in accordance with the length of the key. A symmetric algorithm is one that uses the same key to both decrypt and encrypt the data file. In our case, we will use the RSACryptoServiceProvider, an asymmetric algorithm, to encrypt and decrypt the key to the data encrypted by RijndaelManaged. Asymmetric algorithms are best used for smaller amounts of data, such as a key, as asymmetric algorithms have more overhead than symmetric algorithms. Symmetric algorithms have less overhead when generating them. As its name implies, the RijndaelManaged class is used by the .NET Framework for managed code; as a government encryption standard, this algorithm is also known as Advanced Encryption Standard, or AES. The algorithm RijndaelManaged works with a key length that ranges between 128 through 256 bits, in 32 bit increments. The term IV stands for Initialization Vector: it is the data that symmetric encryption algorithms use to further obscure the first block of data being encrypted, making unauthorized decrypting more difficult. The IV, then, gets or sets the initialization vector for the symmetric algorithm. Like the Key property, both the encryptor and the decryptor must specify the same value. The Key gets or sets the secret key pair for the symmetric algorithm. After encryption, you must store this value and transfer it to the decryptor. During decryption, you must specify the same key used for encryption. The downloadable solution files were built using Visual Studio 2010, but the code presented in the later section is constructed as one file for the command line.

The first step in this process is to make three folders: c:\docs, c:\Encrypt, and c:\Decrypt. Write out some text files and place them into the C:\docs directory. The Forms application requires six buttons, two labels, and two OpenFileDialog controls to be dragged and dropped onto the surface of the form. The AutoSize property of the first label (which goes on the top of the form) should be set to false. This will give you room to enlarge the font and change the color. This is where is the textual output appears when we click the Create Keys button. So leave the text property for the first label blank. The text for the second label should state: “Secure Your Files”. That label is also has its AutoSize property set to false.

The Source Code in One File

Notice the private methods, Encrypt and Decrypt, and the event handlers for the button controls. The Encrypt button handler invokes a method that is private to that module, as does the Decrypt method. Note that there are no user input fields in this application. We are using the OpenFileDialog controls to access files that are to be encrypted:

It is always good practice to build the forms application, examine the object browser, and set the properties of the classes that are, in this case, controls. This file was built, however, on the command line prompt of the .NET Framework. Here is how it looks before it is used:

To Create Keys, Encrypt, and Decrypt

Click the Create Keys button. The label displays the key name and shows that it is a full key pair.

Click the Export Public Key button. Note that exporting the public key parameters does not change the current key.

Click the Encrypt File button and select a file.

Click the Decrypt Filebutton and select the file just encrypted.

Examine the file just decrypted.

Close the application and restart it to test retrieving persisted key containers in the next scenario.

To Encrypt using the Public Key

Click the Import Public Key button. The label displays the key name and shows that it is public only.

Click the Encrypt File button and select a file.

Click the Decrypt Filebutton and select the file just encrypted. This will fail because you must have the private key to decrypt.

This scenario demonstrates having only the public key to encrypt a file for another person. Typically, that person would give you only the public key and withhold the private key for decryption.

To Decrypt using the Private Key

Click the Get Private Key button. The label displays the key name and shows whether it is the full key pair.

Click the Decrypt File button and select the file just encrypted. This will be successful because you have the full key pair to decrypt.

Share

About the Author

I started electronics training at age 33. I began studying microprocessor technology in an RF communications oriented program. I am 43 years old now. I have studied C code, opcode (mainly x86 and AT+T) for around 3 years in order to learn how to recognize viral code and the use of procedural languages. I am currently learning C# and the other virtual runtime system languages. I guess I started with the egg rather than the chicken. My past work would indicate that my primary strength is in applied mathematics.

Comments and Discussions

I am new to programming using .Net language The code is good ,but i have also discovered something that i need help . can someone help me to understand the 3rd step. On my computer the code is getting me into a debugg mode , if i proceed to do step 3. How can i fix the code so that it will not go into the debugg mode.

In this article my word documents are transfered to text document , even when i want to open from the decrypt folder is there anything that can be done to open the documents in word after going through encryption.

Have you tried to encrypt pdf files, i am having problem in encrypting pdf files.

To Encrypt using the Public Key

1. Click the Import Public Key button. The label displays the key name and shows that it is public only. 2. Click the Encrypt File button and select a file. 3. Click the Decrypt Filebutton and select the file just encrypted. This will fail because you must have the private key to decrypt.