C# Code Snippets

C# Encryption Class

Visual C# Kicks

The class makes it easy to quickly encrypt data with a password using built-in .NET Framework functions.

Platform: .NET Framework 2.0

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

class Encryptor
{
    public static byte[] Encrypt(byte[] input, string password)
    {
        try
        {
            TripleDESCryptoServiceProvider service = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            byte[] key = md5.ComputeHash(Encoding.ASCII.GetBytes(password));
            byte[] iv = md5.ComputeHash(Encoding.ASCII.GetBytes(password));

            return Transform(input, service.CreateEncryptor(key, iv));
        }
        catch (Exception)
        {
            return new byte[0];
        }
    }

    public static byte[] Decrypt(byte[] input, string password)
    {
        try
        {
            TripleDESCryptoServiceProvider service = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            byte[] key = md5.ComputeHash(Encoding.ASCII.GetBytes(password));
            byte[] iv = md5.ComputeHash(Encoding.ASCII.GetBytes(password));

            return Transform(input, service.CreateDecryptor(key, iv));
        }
        catch (Exception)
        {
            return new byte[0];
        }
    }

    public static string Encrypt(string text, string password)
    {
        byte[] input = Encoding.UTF8.GetBytes(text);
        byte[] output = Encrypt(input, password);
        return Convert.ToBase64String(output);
    }

    public static string Decrypt(string text, string password)
    {
        byte[] input = Convert.FromBase64String(text);
        byte[] output = Decrypt(input, password);
        return Encoding.UTF8.GetString(output);
    }

    private static byte[] Transform(byte[] input, ICryptoTransform CryptoTransform)
    {
        MemoryStream memStream = new MemoryStream();
        CryptoStream cryptStream = new CryptoStream(memStream, CryptoTransform, CryptoStreamMode.Write);

        cryptStream.Write(input, 0, input.Length);
        cryptStream.FlushFinalBlock();

        memStream.Position = 0;
        byte[] result = new byte[Convert.ToInt32(memStream.Length)];
        memStream.Read(result, 0, Convert.ToInt32(result.Length));

        memStream.Close();
        cryptStream.Close();

        return result;
    }
}

Back to C# Code Snippet List