It's been a long while since i've been reading in my 70-536 training kit because i was caught up with learning wpf and wcf. I am, however, revisiting it this weekend, and hopefully going to get through the last 200-odd pages. First step along the way was working with Encryption. You remember the first time your math teacher showed you a linear algebra equation, before you even knew what variables were? That's about how I felt reading some of the descriptions in this book.
The first thing they wanted to talk about was the SymmetricAlgorithm sub-classes, Rjindael, Des, TripleDes, AES .... There's quite a few properties on here and the descriptions in the book made me scratch my head, but I think after doing the example it started to make sense.
from what i've read, these are the conclusions i've come to.
Basically you Will need to create a key and IV (initialization vector) that both the cryptor and decryptor will need to have when they generate your SymmetricAlgorithm. Both of these Key and IV properties of your Algorithm class are Byte arrays. The size that they need to be (ironically) is defined in bits by the KeySize and BlockSize properties of the algorithm(respectively). So we have to take the size and divide it by 8 to figure out how big of a key we need.
Once we have our algorithm initialized with it's key and IV, we can create an instance of an ICryptoTransform as an Encryptor or a Decryptor. (the CreateEncryptor() and CreateDecryptor() methods on your provider, respectively)
Here's my example:(based on the 70-535 training kit example)
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace EncryptingSample1
{
class Program
{
static void Main(string[] args)
{
string password =@"password";
RijndaelManaged myAlg = new RijndaelManaged();
byte[] salt = Encoding.ASCII.GetBytes("salt");
/*salt has to be at least 8 bytes so we pad it out if it isn't... normally
* we should probably make an 8 byte or bigger salt...
* */
List<byte> byteList = new List<byte>();
byteList.AddRange(salt);
if (byteList.Count < 8)
{
for (int i = byteList.Count - 1; i < 8; i++)
{
byteList.Insert(i, new byte());
}
}
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password,byteList.ToArray());
//this is from the mcts training kit, not sure how likely you'd ever use the same key for the key and IV
//now let's get the key bytes, the Keysize is in bits so we divide by 8
myAlg.Key = key.GetBytes(myAlg.KeySize / 8);
//now the initialization vector, again stored in size of bits so dividing by 8
myAlg.IV = key.GetBytes(myAlg.BlockSize / 8);
string message = "Hello, whats up?";
byte[] messageBytes = Encoding.ASCII.GetBytes(message);
ICryptoTransform cryptor = myAlg.CreateEncryptor();
byte[] encryptedBytes = cryptor.TransformFinalBlock(messageBytes, 0, messageBytes.Length);
Console.WriteLine("Encrypted: {0}",Encoding.ASCII.GetString(encryptedBytes));
ICryptoTransform decryptor = myAlg.CreateDecryptor();
byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
string decryptedMessage = Encoding.ASCII.GetString(decryptedBytes);
Console.WriteLine("Decrypted: {0}",decryptedMessage);
Console.ReadKey();
}
}
}