Game Development Reference
In-Depth Information
byte[] result = transform.TransformFinalBlock(cipherText, 0, cipherText.Length);
return result;
}
You will notice in the above example that TransformFinalBlock is called, but
TransformBlock is never called. Since we know that CanTransformMultipleBlocks will
always return true with any of the symmetric algorithms in the .NET Class
Framework, we can transform all of our data in one pass.
The following code shows a simple example using the code shown in this chapter.
using System.Text;
static void Main(string[] args)
{
byte[] key = Encoding.Default.GetBytes(“This is my secret key!”);
string plainText = “This is a test!”;
// You can also use RC2, DES, and TripleDES
RijndaelManaged cipher = new RijndaelManaged();
cipher.Mode = CipherMode.CBC;
Console.WriteLine(“Original: [“ + plainText + “]”);
byte[] encryptedData = EncryptMessage(cipher, key,
Encoding.Default.GetBytes(plainText));
Console.WriteLine(“Encrypted: [“ +
Encoding.Default.GetString(encryptedData) + “]”);
byte[] decryptedData = DecryptMessage(cipher, key, encryptedData);
string decryptedText = Encoding.Default.GetString(decryptedData);
Console.WriteLine(“Decrypted: [“ + decryptedText + “]”);
Console.WriteLine(“”);
Console.WriteLine(“Press any key to continue.”);
Console.Read();
}
Conclusion
This chapter covered the theory and implementation details of using the built-in
cryptography functionality in the .NET framework. While the presented solution
could be improved upon and extended into a more reusable encryption manager,
the fundamental code stays the same.
Search Nedrilad ::




Custom Search