
June 14, 2009 20:54 by
Simon
I've written an encryption utility that I find quite useful. There is nothing special about the code that does the encryption, but I have given it a command line interface and kept the actual encryption logic in a separate assembly which can be added to other applications. What I use this for is to manually encrypt data which I then put in a database which can then be decrypted by my application by adding a reference to the dll which has the encryption logic.

The code used to perform the encryption is below, you can also download the whole project.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace Sharpcoder.Utils
{
public static class EncryptionUtil
{
private static byte[] salt = Encoding.ASCII.GetBytes("RsOpn47TAsGVH0orf83jzGOn");
/// <summary>
/// Encrypts the string passed in.
/// </summary>
/// <param name="password">The password used to generate a key, to use to encrypt the string.</param>
/// <param name="data">The string to encrypt.</param>
/// <returns>The encrypted string.</returns>
public static String Encrypt(String password, String data)
{
return Convert.ToBase64String(Encrypt(password, Encoding.UTF8.GetBytes(data)));
}
/// <summary>
/// Encrypts the data passed in.
/// </summary>
/// <param name="password">The password used to generate a key, to use to encrypt the data.</param>
/// <param name="data">The data to encrypt.</param>
/// <returns>The encrypted data.</returns>
private static byte[] Encrypt(String password, byte[] data)
{
SymmetricAlgorithm alg = GenerateKey(password);
using (ICryptoTransform encryptor = alg.CreateEncryptor())
{
return EncryptDecrypt(data, encryptor);
}
}
/// <summary>
/// Decrypts the encrypted string passed in.
/// </summary>
/// <param name="password">The password used to create the key, used to encrypt the string.</param>
/// <param name="data">The string to decrypt.</param>
/// <returns>The decrypted string.</returns>
public static String Decrypt(String password, String data)
{
return Encoding.UTF8.GetString(Decrypt(password, Convert.FromBase64String(data)));
}
/// <summary>
/// Decrypts the encrypted data passed in.
/// </summary>
/// <param name="password">The password used to create the key, used to encrypt the data.</param>
/// <param name="data">The data to decrypt.</param>
/// <returns>The decrypted data.</returns>
public static byte[] Decrypt(String password, byte[] data)
{
SymmetricAlgorithm alg = GenerateKey(password);
using (ICryptoTransform decryptor = alg.CreateDecryptor())
{
return EncryptDecrypt(data, decryptor);
}
}
/// <summary>
/// Creates the symetric algorithm object and generates the key from the password passed
/// in.
/// </summary>
/// <param name="password">The password to use for the encryption / decryption.</param>
/// <returns>The symetric algorithm object.</returns>
private static SymmetricAlgorithm GenerateKey(String password)
{
SymmetricAlgorithm alg = new RijndaelManaged();
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt);
alg.Key = key.GetBytes(alg.KeySize / 8);
alg.IV = key.GetBytes(alg.BlockSize / 8);
return alg;
}
/// <summary>
/// Encrypts (or decrypts) the data using the crypto transform object.
/// </summary>
/// <param name="data">The data to encrypt/decrypt.</param>
/// <param name="cryptor">The object to use to perform the encryption/decryption.</param>
/// <returns></returns>
private static byte[] EncryptDecrypt(byte[] data, ICryptoTransform cryptor)
{
using (MemoryStream outStream = new MemoryStream())
{
using (CryptoStream decryptStream = new CryptoStream(outStream, cryptor, CryptoStreamMode.Write))
{
decryptStream.Write(data, 0, data.Length);
decryptStream.FlushFinalBlock();
return outStream.ToArray();
}
}
}
}
}
057f93f2-946c-438a-9798-4b96c1d8993b|0|.0