Showing posts with label Cryptography. Show all posts
Showing posts with label Cryptography. Show all posts

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

Data Protection API

System.Security.Cryptography.ProtectedData

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

using System;
using System.Collections;
using System.Security.Cryptography;
using System.Text;

public class DataProtectionSample
{
// Create byte array for additional entropy when using Protect method
static byte[] _additionalEntropy = { 9, 8, 7, 6, 5 };

public static void Main()
{
//Original data
string secret = "Magic";
PrintValues("The original data is:", secret);

//Convert to bytes
byte[] secretInBytes = Encoding.UTF8.GetBytes(secret);
PrintValues("The original data in bytes is:", secretInBytes);

//Encrypt the bytes
byte[] encryptedSecret = Protect(secretInBytes);
PrintValues("The encrypted byte array is:", encryptedSecret);

// Decrypt the bytes
byte[] originalDataInBytes = Unprotect(encryptedSecret);
PrintValues("The original data in bytes is:", originalDataInBytes);

//Convert to string
string originalData = Encoding.UTF8.GetString(originalDataInBytes);
PrintValues("The original data is:", originalData);
}

public static byte[] Protect(byte[] data)
{
// Encrypt the data using DataProtectionScope.CurrentUser.
// The result can be decrypted only by the same current user.
return ProtectedData.Protect(data, _additionalEntropy, DataProtectionScope.CurrentUser);
}

public static byte[] Unprotect(byte[] data)
{
//Decrypt the data using DataProtectionScope.CurrentUser.
return ProtectedData.Unprotect(data, _additionalEntropy, DataProtectionScope.CurrentUser);
}

public static void PrintValues(string header, IEnumerable detail)
{
Console.WriteLine(header);
foreach (var item in detail) { Console.Write("\t{0}", item); }
Console.WriteLine();
}
}
image

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