aes 256 encryption decryption c sharp

Secure AES-256 Encryption and Decryption in C#

In the digital age, data security is of utmost importance. When dealing with sensitive information, robust encryption techniques become crucial to safeguard data from unauthorized access.

In this article, we’ll explore how to implement AES-256 encryption and decryption in C# to ensure the confidentiality of your data. Utilizing the BCrypt-Official package, we’ll create a strong encryption system that keeps your secrets safe from prying eyes.

Understanding AES-256 Encryption in C#

Before we dive into the code, let’s understand the fundamental concepts behind AES-256 encryption:

  • AES: Advanced Encryption Standard (AES) is a symmetric encryption algorithm used to secure data by converting plaintext into ciphertext using a secret key.
  • 256-bit Key: AES-256 employs a 256-bit key, offering an immensely large key space, making it computationally infeasible to break through brute-force attacks.
  • Cipher Block Chaining (CBC) Mode: CBC mode is a block cipher mode of operation that adds an Initialization Vector (IV) to the encryption process, ensuring uniqueness and preventing patterns in the ciphertext.

Installing BCrypt-Official Package

To get started, ensure you have the BCrypt-Official package installed in your C# project. BCrypt is a powerful cryptographic library that aids in generating secure hash passwords.

Implementing AES-256 Encryption and Decryption in C#

Below is the C# code to perform AES-256 encryption and decryption using the BCrypt-Official package:

using System.Security.Cryptography;
using System.IO;
using System.Text;
using System;

public class AESEncryption
{
    public string EncryptString(string plainText, byte[] key, byte[] iv)
    {
        Aes encryptor = Aes.Create();

        encryptor.Mode = CipherMode.CBC;
        encryptor.Key = key.Take(32).ToArray();
        encryptor.IV = iv;

        MemoryStream memoryStream = new MemoryStream();
        ICryptoTransform aesEncryptor = encryptor.CreateEncryptor();
        CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write);

        Byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);
        cryptoStream.Write(plainBytes, 0, plainBytes.Length);
        cryptoStream.FlushFinalBlock();

        Byte[] cipherBytes = memoryStream.ToArray();
        memoryStream.Close();
        cryptoStream.Close();

        string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);
        return cipherText;
    }

    public string DecryptString(string cipherText, byte[] key, byte[] iv)
    {
        Aes encryptor = Aes.Create();

        encryptor.Mode = CipherMode.CBC;
        encryptor.Key = key.Take(32).ToArray();
        encryptor.IV = iv;

        MemoryStream memoryStream = new MemoryStream();
        ICryptoTransform aesDecryptor = encryptor.CreateDecryptor();
        CryptoStream cryptoStream = new CryptoStream(memoryStream, aesDecryptor, CryptoStreamMode.Write);

        string plainText = String.Empty;

        try
        {
            Byte[] cipherBytes = Convert.FromBase64String(cipherText);
            cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);
            cryptoStream.FlushFinalBlock();

            Byte[] plainBytes = memoryStream.ToArray();
            plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);
        }
        finally
        {
            memoryStream.Close();
            cryptoStream.Close();
        }

        return plainText;
    }
}

Encrypting and Decrypting Data

Now, let’s see how to encrypt and decrypt data using the AES-256 implementation:

// Message to be encrypted
string message = "My secret message 1234";

// Password used to generate the encryption key
string password = "3sc3RLrpd17";

// Hash the password using BCrypt with a cost factor of 12
string hashedPassword = BCrypt.Net.BCrypt.HashPassword(password, 12);

// Convert the hashed password to an array to use as the encryption key
Byte[] key = Encoding.ASCII.GetBytes(hashedPassword);


// Create an Initialization Vector (IV)
Random rnd = new Random();
Byte[] b = new Byte[16];
Byte[] iv = rnd.NextBytes(b);

// Instantiate the AESEncryption class
AESEncryption aesEncryption = new AESEncryption();

// Encrypt the message
string encrypted = aesEncryption.EncryptString(message, key, iv);

// Decrypt the message
string decrypted = aesEncryption.DecryptString(encrypted, key, iv);

// Output the results
Console.WriteLine("Encrypted Message: " + encrypted);
Console.WriteLine("Decrypted Message: " + decrypted);

Conclusion

In this tutorial, we’ve explored AES-256 encryption and decryption in C# using the BCrypt-Official package.

By implementing strong encryption techniques, such as AES-256, you can protect your data from potential threats and ensure data confidentiality.

Remember to use BCrypt to generate secure hash passwords, and always handle encryption keys and initialization vectors responsibly.

By following these best practices, you can confidently secure your sensitive information and keep it safe from unauthorized access.