RijndaelManagedTransform Class
Assembly: mscorlib (in mscorlib.dll)
Both the CreateEncryptor and CreateDecryptor methods return an instance of the RijndaelManagedTransform class that is initialized with the key information of the current RijndaelManaged object. To encrypt or decrypt data using the Rijndael algorithm, pass the RijndaelManagedTransform object returned by these methods to a CryptoStream object.
The following code example demonstrates how to use members of the RijndaelManagedTransform class.
using System; using System.Security.Cryptography; using System.Collections; using System.Text; class Members { // Use a RijndaelManaged object for encryption and decryption. static RijndaelManaged rij = new RijndaelManaged(); [STAThread] static void Main(string[] args) { string message = "012345678901234567890"; byte[] sourceBytes = Encoding.ASCII.GetBytes(message); Console.WriteLine("** Phrase to be encoded: " + message); byte[] encodedBytes = EncodeBytes(sourceBytes); Console.WriteLine("** Phrase after encoding: " + Encoding.ASCII.GetString(encodedBytes)); byte[] decodedBytes = DecodeBytes(encodedBytes); Console.WriteLine("** Phrase after decoding: " + Encoding.ASCII.GetString(decodedBytes)); Console.WriteLine("Sample ended successfully; " + "press Enter to continue."); Console.ReadLine(); } // Encode the specified byte array by using RijndaelManagedTransform. private static byte[] EncodeBytes(byte[] sourceBytes) { int currentPosition = 0; byte[] targetBytes = new byte[1024]; int sourceByteLength = sourceBytes.Length; // Create a Rijndael encryptor from this instance to perform encryption. RijndaelManagedTransform rijTransform = (RijndaelManagedTransform)rij.CreateEncryptor(); // Retrieve the block size to read the bytes. int inputBlockSize = rijTransform.InputBlockSize; // Retrieve the block size to write the bytes. int outputBlockSize = rijTransform.OutputBlockSize; try { // Determine if multiple blocks can be transformed. if (rijTransform.CanTransformMultipleBlocks) { int numBytesRead = 0; while (sourceByteLength - currentPosition >= inputBlockSize) { // Transform the bytes from currentPosition in the // sourceBytes array, writing the bytes to the targetBytes // array. numBytesRead = rijTransform.TransformBlock( sourceBytes, currentPosition, inputBlockSize, targetBytes, currentPosition); // Advance the current position in the sourceBytes array. currentPosition += numBytesRead; } // Transform the final block of bytes. byte[] finalBytes = rijTransform.TransformFinalBlock( sourceBytes, currentPosition, sourceByteLength - currentPosition); // Copy the contents of the finalBytes array to the // targetBytes array. finalBytes.CopyTo(targetBytes, currentPosition); } } catch (Exception ex) { Console.WriteLine("Caught unexpected exception:" + ex.ToString()); } // Determine if the current transform can be reused. if (!rijTransform.CanReuseTransform) { // Free up any used resources. rijTransform.Clear(); } // Trim the extra bytes in the array that were not used. return TrimArray(targetBytes); } // Decode the specified byte array using RijndaelManagedTransform. private static byte[] DecodeBytes(byte[] sourceBytes) { byte[] targetBytes = new byte[1024]; int currentPosition = 0; // Create a Rijndael decryptor from this instance to perform decryption. RijndaelManagedTransform rijTransform = (RijndaelManagedTransform)rij.CreateDecryptor(); int inputBlockSize = rijTransform.InputBlockSize; int sourceByteLength = sourceBytes.Length; try { int numBytesRead = 0; while (sourceByteLength - currentPosition >= inputBlockSize) { // Transform the bytes from currentposition in the // sourceBytes array, writing the bytes to the targetBytes // array. numBytesRead = rijTransform.TransformBlock( sourceBytes, currentPosition, inputBlockSize, targetBytes, currentPosition); // Advance the current position in the source array. currentPosition += numBytesRead; } // Transform the final block of bytes. byte[] finalBytes = rijTransform.TransformFinalBlock( sourceBytes, currentPosition, sourceByteLength - currentPosition); // Copy the contents of the finalBytes array to the targetBytes // array. finalBytes.CopyTo(targetBytes, currentPosition); } catch (Exception ex) { Console.WriteLine("Caught unexpected exception:" + ex.ToString()); } // Trim the extra bytes in the array that were not used. return TrimArray(targetBytes); } // Resize the dimensions of the array to a size that contains only valid // bytes. private static byte[] TrimArray(byte[] targetArray) { IEnumerator enum1 = targetArray.GetEnumerator(); int i = 0; while (enum1.MoveNext()) { if (enum1.Current.ToString().Equals("0")) { break; } i++; } // Create a new array with the number of valid bytes. byte[] returnedArray = new byte[i]; for (int j = 0; j < i; j++) { returnedArray[j] = targetArray[j]; } return returnedArray; } }
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Reference
RijndaelManagedTransform MembersSystem.Security.Cryptography Namespace
Other Resources
Cryptographic Services** Phrase to be encoded: 012345678901234567890
** Phrase after encoding: ????WA????i $?r??.N?M??_x-
** Phrase after decoding: 0123456789012345????b,3????
?1X?67890
- 11/3/2009
- dgxhubbard
I noticed that the trim array function looked very akward... why getting an enumerator of an byte array and then looping with that enumerator to find the first zero byte. And looking for the zero byte by converting the object to a string and checking if it equals "0".....
Not to mention that somehow all the ending " } " are lost from the c# and c++ functions.
Anyway I just wanted to add the following suggestion for the TrimArray function (because trimming an array can be used in a lot of other applications)
private static byte[] TrimArray(byte[] targetArray)
{
int i = 0;
while (i < targetArray.Length)
{
if (targetArray[i] == 0)
break;
i++;
}
// Create a new array with the number of valid bytes.
byte[] returnedArray = new byte[i];
for (int j = 0; j < i; j++)
{
returnedArray[j] = targetArray[j];
}
return returnedArray;
}
there could be some other optimalisation, but this gets to the right direction :)
- 4/10/2007
- Davy Landman
- 8/13/2007
- Davy Landman