Saturday, 21 April 2012

Rijndael

System.Security.Cryptography.RijndaelManaged

Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)

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

namespace RijndaelManaged_Example
{
class RijndaelExample
{
public static void Main()
{
try
{
string original = "Here is some data to encrypt!";

using (RijndaelManaged myRijndael = new RijndaelManaged())
{
byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);
string roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);

Console.WriteLine("Original: {0}", original);
Console.WriteLine("Round Trip: {0}", roundtrip);
}
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
}

static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
{
if (string.IsNullOrEmpty(plainText)) 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;

using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;

ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

using (MemoryStream msEncrypt = new MemoryStream())
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
swEncrypt.Write(plainText);

encrypted = msEncrypt.ToArray();
}
}

return encrypted;
}

static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
{
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");

string plaintext;

using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;

ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

using (MemoryStream msDecrypt = new MemoryStream(cipherText))
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
plaintext = srDecrypt.ReadToEnd();
}
}

return plaintext;
}
}
}

image


Taken from MSDN: http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx

Source:  http://stevenhollidge.com/blog-source-code/RijndaelExample.zip

No comments:

Post a Comment