RijndaelManagedTransform Class
Assembly: mscorlib (in mscorlib.dll)
'Declaration <ComVisibleAttribute(True)> _ Public NotInheritable Class RijndaelManagedTransform Implements ICryptoTransform, IDisposable 'Usage Dim instance As RijndaelManagedTransform
/** @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.
Imports System Imports System.Security.Cryptography Imports System.Collections Imports System.Text Class Members ' Use a RijndaelManaged object for encryption and decryption. Private Shared rij As New RijndaelManaged() <STAThread()> _ Shared Sub Main(ByVal args() As String) Dim message As String = "012345678901234567890" Dim sourceBytes As Byte() = Encoding.ASCII.GetBytes(message) Console.WriteLine("** Phrase to be encoded: " + message) Dim encodedBytes As Byte() = EncodeBytes(sourceBytes) Console.WriteLine("** Phrase after encoding: " + Encoding.ASCII.GetString(encodedBytes)) Dim decodedBytes As Byte() = DecodeBytes(encodedBytes) Console.WriteLine("** Phrase after decoding: " + Encoding.ASCII.GetString(decodedBytes)) Console.WriteLine("Sample ended successfully; " + "press Enter to continue.") Console.ReadLine() End Sub 'Main ' Encode the specified byte array by using RijndaelManagedTransform. Private Shared Function EncodeBytes(ByVal sourceBytes() As Byte) As Byte() Dim currentPosition As Integer = 0 Dim targetBytes(1023) As Byte Dim sourceByteLength As Integer = sourceBytes.Length ' Create a Rijndael encryptor from this instance to perform encryption. Dim rijTransform As RijndaelManagedTransform = CType(rij.CreateEncryptor(), RijndaelManagedTransform) ' Retrieve the block size to read the bytes. Dim inputBlockSize As Integer = rijTransform.InputBlockSize ' Retrieve the block size to write the bytes. Dim outputBlockSize As Integer = rijTransform.OutputBlockSize Try ' Determine if multiple blocks can be transformed. If rijTransform.CanTransformMultipleBlocks Then Dim numBytesRead As Integer = 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 End While ' Transform the final block of bytes. Dim finalBytes As Byte() = rijTransform.TransformFinalBlock(sourceBytes, currentPosition, sourceByteLength - currentPosition) ' Copy the contents of the finalBytes array to the ' targetBytes array. finalBytes.CopyTo(targetBytes, currentPosition) End If Catch ex As Exception Console.WriteLine("Caught unexpected exception:" + ex.ToString()) End Try ' Determine if the current transform can be reused. If Not rijTransform.CanReuseTransform Then ' Free up any used resources. rijTransform.Clear() ' Trim the extra bytes in the array that were not used. Return TrimArray(targetBytes) End Function 'EncodeBytes ' Decode the specified byte array using RijndaelManagedTransform. Private Shared Function DecodeBytes(ByVal sourceBytes() As Byte) As Byte() Dim targetBytes(1023) As Byte Dim currentPosition As Integer = 0 ' Create a Rijndael decryptor from this instance to perform decryption. Dim rijTransform As RijndaelManagedTransform = CType(rij.CreateDecryptor(), RijndaelManagedTransform) Dim inputBlockSize As Integer = rijTransform.InputBlockSize ' Compensate for VB padding of arrays. Dim sourceByteLength As Integer = sourceBytes.Length - 1 Try Dim numBytesRead As Integer = 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 End While ' Transform the final block of bytes. Dim finalBytes As Byte() = rijTransform.TransformFinalBlock(sourceBytes, currentPosition, sourceByteLength - currentPosition) ' Copy the contents of the finalBytes array to the targetBytes ' array. finalBytes.CopyTo(targetBytes, currentPosition) Catch ex As Exception Console.WriteLine("Caught unexpected exception:" + ex.ToString()) End Try ' Trim the extra bytes in the array that were not used. Return TrimArray(targetBytes) End Function 'DecodeBytes ' Resize the dimensions of the array to a size that contains only valid ' bytes. Private Shared Function TrimArray(ByVal targetArray() As Byte) As Byte() Dim enum1 As IEnumerator = targetArray.GetEnumerator() Dim i As Integer = 0 While enum1.MoveNext() If enum1.Current.ToString().Equals("0") Then Exit While End If i += 1 End While ' Create a new array with the number of valid bytes. Dim returnedArray(i) As Byte Dim j As Integer For j = 0 To i returnedArray(j) = targetArray(j) Next j Return returnedArray End Function 'TrimArray End Class 'Members
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.