How To Encrypt and Decrypt Files Using The AES Encryption Algorithm in C#
How To Encrypt and Decrypt Files Using The AES Encryption Algorithm in C#
RELATED ARTICLES - C#
How to encrypt and decrypt files using the AES encryption algorithm in C#
JUNE 13TH 2017 70.1K 21 COMMENTS
How to allow only plain text inside a RichTextBox
in your C# WinForms Application
DECEMBER 9TH 2019
If the les of your users contain sensitive information, you can encrypt it so C#
that no one can open that le but the user itself. Encrypting your les
makes them di cult for anyone to access and read without your password.
How to retrieve the RAM amount available on the
If you're into the encryption theme in your project, we'll show you in this System in WinForms with C#
article how to encrypt and decrypt les using the AES algorithm easily. AUGUST 26TH 2019
C#
Note
How to retrieve (list) the titles and process id of all
the opened applications in the taskbar of Windows
This article shows you a way to encrypt and decrypt easily and quickly les with C# in WinForms
using simple methods like encrypt and decrypt. They're the result of a AUGUST 22ND 2019
using System.Security.Cryptography;
using System.Runtime.InteropServices;
The reference to InteropServices in the top of your class will allow you to
use later the DllImport method in our class.
The method will be used and explained in the step #3, for now, copy and
include the methods in your project:
price drop
LIKE US FOLLOW US STAR US ON
GITHUB
SUBSCRIBE SUBSCRIBE FOLLOW US
// Call this function to remove the key from memory after
[DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
public static extern bool ZeroMemory(IntPtr Destination, int
/// <summary>
/// Creates a random salt that will be used to encrypt your
/// </summary>
/// <returns></returns>
public static byte[] GenerateRandomSalt()
{
byte[] data = new byte[32];
return data;
}
/// <summary>
/// Encrypts a file from its path and a plain password.
/// </summary>
/// <param name="inputFile"></param>
/// <param name="password"></param>
private void FileEncrypt(string inputFile, string password)
{
//http://stackoverflow.com/questions/27645527/aes-encry
//http://stackoverflow.com/questions/2659214/why-do-i-ne
//"What it does is repeatedly hash the user password alo
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
try
{
while ((read = fsIn.Read(buffer, 0, buffer.Length))
{
Application.DoEvents(); // -> for responsive GU
cs.Write(buffer, 0, read);
}
// Close up
fsIn.Close();
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
finally
{
cs.Close();
fsCrypt.Close();
}
}
/// <summary>
/// Decrypts an encrypted file with the FileEncrypt method t
/// </summary>
/// <param name="inputFile"></param>
/// <param name="outputFile"></param>
/// <param name="password"></param>
private void FileDecrypt(string inputFile, string outputFile
{
byte[] passwordBytes = System.Text.Encoding.UTF8.GetByte
byte[] salt = new byte[32];
int read;
byte[] buffer = new byte[1048576];
try
{
while ((read = cs.Read(buffer, 0, buffer.Length)) >
{
Application.DoEvents();
fsOut.Write(buffer, 0, read);
}
}
catch (CryptographicException ex_CryptographicException
{
Console.WriteLine("CryptographicException error: " +
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
try
{
cs.Close();
}
catch (Exception ex)
{
Console.WriteLine("Error by closing CryptoStream: "
}
finally
{
fsOut.Close();
fsCrypt.Close();
}
}
They're not necessarily perfect and they can (and need to) be modi ed to
handle more exceptions in case they appear and how you work with your
application.
3. Using the methods
From the required methods, you will only need to use 2 of them (FileEncrypt
and FileDecrypt) obviously and 1 of them optional, the fourth
(GenerateRandomSalt) is used internally by the FileEncrypt method.
Encrypt File
Encrypt a le using the FileEncrypt method that expects as rst argument
the path to the le that will be encrypted and as second argument the
password that will be used to encrypt it. The password can be used to
decrypt the le later. To make everything right, we recommend you to
delete the password from the memory using the ZeroMemory method. Call
this function to remove the key from memory after use for security
purposes:
Decrypt File
To decrypt the le, we'll follow the same process but using FileDecrypt
instead. This method expects as rst argument the path to the encrypted
le and as second argument the path where the decrypted le should be
placed. As third argument you need to provide the string that was used to
encrypt the le originally:
string password = "ThePasswordToDecryptAndEncryptTheFile";
Final notes
The encryption/decryption process is memory consumming and take
time so it's recommendable to run those tasks in another thread to
prevent your main UI from freezing.
The extension aes can be changed for the extension that you want.
Happy coding !
Carlos Delgado
How to solve Visual How CRM Boosts IT How to detect if the use
Studio Code can't be … Companies Rates prefers a light or dark …
Our Comments Section is open to every developer, so you can contribute (even
code) to the main idea of the Article.
Please read our Comment Policy before commenting.
J i th di i
Our Code World is a free blog about programming, where you will nd Privacy Policy About
solutions to simple and complex tasks of your daily life as a developer. Comments Policy Advertise with us
Authors
Contact
Write for us
ADVERTISING