Export (0) Print
Expand All
0 out of 4 rated this helpful - Rate this topic

RijndaelManagedTransform Class

Note: This class is new in the .NET Framework version 2.0.

Performs a cryptographic transformation of data using the Rijndael algorithm. This class cannot be inherited.

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

[ComVisibleAttribute(true)] 
public sealed class RijndaelManagedTransform : ICryptoTransform, IDisposable
/** @attribute ComVisibleAttribute(true) */ 
public final class RijndaelManagedTransform implements ICryptoTransform, IDisposable
ComVisibleAttribute(true) 
public final class RijndaelManagedTransform implements ICryptoTransform, IDisposable

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;
    }
}

System.Object
  System.Security.Cryptography.RijndaelManagedTransform
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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.

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.