Monday 10 October 2011

Symmetric Encryption with Rijndael Algorithm

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace MyProject
{
public interface IEncryptionManager
{
string Encrypt(string originalText);
string Decrypt(string encryptedText);
}

/// <summary>
/// This class uses the Rijndael Encryption Algorithm
/// </summary>
public class RijndaelEncryptionManager : IEncryptionManager
{
#region Fields

/// <summary>
/// The key can only be 16, 24 or 32 bytes.
/// </summary>
private static readonly byte[] EncryptionKey = new byte[] { 0x00, 0x11, 0x22, 0x03, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xAA, 0xBB, 0xC0, 0xDD, 0x0E, 0xFF };

/// <summary>
/// The vector also can only be 16, 24 or 32 bytes.
/// </summary>
private static readonly byte[] InitializationVector = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0xFD, 0x0E, 0x0F};

#endregion

#region Methods

public string Encrypt(string originalText)
{
using (var myRijndael = new RijndaelManaged { Key = EncryptionKey, IV = InitializationVector })
{
// Encrypt the string to an array of bytes.
var encryptedBytes = EncryptStringToBytes(originalText, myRijndael.Key, myRijndael.IV);

return Convert.ToBase64String(encryptedBytes);
}
}

public string Decrypt(string encryptedText)
{
using (var myRijndael = new RijndaelManaged { Key = EncryptionKey, IV = InitializationVector })
{
// Encrypt the string to an array of bytes.
var encryptedBytes = Convert.FromBase64String(encryptedText);

// Decrypt the bytes to a string.
return DecryptStringFromBytes(encryptedBytes, myRijndael.Key, myRijndael.IV);
}
}

/// <summary>
/// Encrypts the string to bytes.
/// </summary>
/// <param name="plainText">The plain text.</param>
/// <param name="key">The key.</param>
/// <param name="iv">The IV.</param>
/// <returns></returns>
static byte[] EncryptStringToBytes(string plainText, byte[] key, byte[] iv)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
if (iv == null || iv.Length <= 0)
throw new ArgumentNullException("key");
byte[] encrypted;
// Create an Rijndael object
// with the specified key and IV.
using (var rijAlg = Rijndael.Create())
{
rijAlg.Key = key;
rijAlg.IV = iv;

// Create a decrytor to perform the stream transform.
var encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

// Create the streams used for encryption.
using (var msEncrypt = new MemoryStream())
{
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (var swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}

// Return the encrypted bytes from the memory stream.
return encrypted;

}

/// <summary>
/// Decrypts the string from bytes.
/// </summary>
/// <param name="cipherText">The cipher text.</param>
/// <param name="key">The key.</param>
/// <param name="iv">The IV.</param>
/// <returns></returns>
static string DecryptStringFromBytes(byte[] cipherText, byte[] key, byte[] iv)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
if (iv == null || iv.Length <= 0)
throw new ArgumentNullException("key");

// Declare the string used to hold
// the decrypted text.
string plaintext = null;

// Create an Rijndael object
// with the specified key and IV.
using (var rijAlg = Rijndael.Create())
{
rijAlg.Key = key;
rijAlg.IV = iv;

// Create a decrytor to perform the stream transform.
var decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

// Create the streams used for decryption.
using (var msDecrypt = new MemoryStream(cipherText))
{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (var srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}

}

return plaintext;

}

#endregion
}
}

No comments: