Export (0) Print
Expand All

CryptoAPITransform Class

Performs a cryptographic transformation of data. This class cannot be inherited.

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

[ComVisibleAttribute(true)] 
public sealed class CryptoAPITransform : ICryptoTransform, IDisposable
/** @attribute ComVisibleAttribute(true) */ 
public final class CryptoAPITransform implements ICryptoTransform, IDisposable
ComVisibleAttribute(true) 
public final class CryptoAPITransform implements ICryptoTransform, IDisposable
Not applicable.

The following code example demonstrates how to use members of the CryptoAPITransform class.

using System;
using System.Security.Cryptography;
using System.Collections;
using System.Text;

class Members
{
    // Use a public service provider for encryption and decryption.
    static DESCryptoServiceProvider des = new DESCryptoServiceProvider();

    [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 CryptoAPITranform.
    private static byte[] EncodeBytes(byte[] sourceBytes)
    {
        int currentPosition = 0;
        byte[] targetBytes = new byte[1024];
        int sourceByteLength = sourceBytes.Length;

        // Create a DES encryptor from this instance to perform encryption.
        CryptoAPITransform cryptoTransform =
            (CryptoAPITransform)des.CreateEncryptor();

        // Retrieve the block size to read the bytes.
        int inputBlockSize = cryptoTransform.InputBlockSize;

        // Retrieve the key handle.
        IntPtr keyHandle = cryptoTransform.KeyHandle;

        // Retrieve the block size to write the bytes.
        int outputBlockSize = cryptoTransform.OutputBlockSize;

        try
        {
            // Determine if multiple blocks can be transformed.
            if (cryptoTransform.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 = cryptoTransform.TransformBlock(
                        sourceBytes,
                        currentPosition,
                        inputBlockSize,
                        targetBytes,
                        currentPosition);

                    // Advance the current position in the sourceBytes array.
                    currentPosition += numBytesRead;
                }

                // Transform the final block of bytes.
                byte[] finalBytes = cryptoTransform.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 (!cryptoTransform.CanReuseTransform)
        {
            // Free up any used resources.
            cryptoTransform.Clear();
        }

        // Trim the extra bytes in the array that were not used.
        return TrimArray(targetBytes);
    }

    // Decode the specified byte array using CryptoAPITranform.
    private static byte[] DecodeBytes(byte[] sourceBytes)
    {
        byte[] targetBytes = new byte[1024];
        int currentPosition = 0;

        // Create a DES decryptor from this instance to perform decryption.
        CryptoAPITransform cryptoTransform =
            (CryptoAPITransform)des.CreateDecryptor();

        int inputBlockSize = cryptoTransform.InputBlockSize;
        int sourceByteLength = sourceBytes.Length;

        try
        {
            int numBytesRead = 0;
            while (sourceByteLength - currentPosition >= inputBlockSize)
            {
                // Transform the bytes from current position in the 
                // sourceBytes array, writing the bytes to the targetBytes
                // array.
                numBytesRead = cryptoTransform.TransformBlock(
                    sourceBytes,
                    currentPosition,
                    inputBlockSize,
                    targetBytes,
                    currentPosition);

                // Advance the current position in the source array.
                currentPosition += numBytesRead;
            }

            // Transform the final block of bytes.
            byte[] finalBytes = cryptoTransform.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());
        }

        // Strip out the second block of bytes.
        Array.Copy(targetBytes, (inputBlockSize * 2), targetBytes, inputBlockSize, targetBytes.Length - (inputBlockSize * 2));

        // 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;
    }
}
//
// This sample produces the following output:
//
// ** Phrase to be encoded: 012345678901234567890
// ** Phrase after encoding: AIGC(+b7X?^djAU?15ve?o
// ** Phrase after decoding: 012345678901234567890
// Sample ended successfully; press Enter to continue.

import System.*;
import System.Security.Cryptography.*;
import System.Collections.*;
import System.Text.*;

class Members
{
    // Use a public service provider for encryption and decryption.
    private static DESCryptoServiceProvider des
        = new DESCryptoServiceProvider();

    /** @attribute STAThread()
     */
    public static void main(String[] args)
    {
        String message = "012345678901234567890";
        ubyte sourceBytes[] = Encoding.get_ASCII().GetBytes(message);
        Console.WriteLine("** Phrase to be encoded: " + message);

        ubyte encodedBytes[] = EncodeBytes(sourceBytes);
        Console.WriteLine("** Phrase after encoding: "
            + Encoding.get_ASCII().GetString(encodedBytes));

        ubyte decodedBytes[] = DecodeBytes(encodedBytes);
        Console.WriteLine("** Phrase after decoding: "
            + Encoding.get_ASCII().GetString(decodedBytes));

        Console.WriteLine("Sample ended successfully; "
            + "press Enter to continue.");
        Console.ReadLine();
    } //main

    // Encode the specified byte array by using CryptoAPITranform.
    private static ubyte[] EncodeBytes(ubyte sourceBytes[])
    {
        int currentPosition = 0;
        ubyte targetBytes[] = new ubyte[1024];
        int sourceByteLength = sourceBytes.get_Length();
        // Create a DES encryptor from this instance to perform encryption.
        CryptoAPITransform cryptoTransform = (CryptoAPITransform)des.
            CreateEncryptor();
        // Retrieve the block size to read the bytes.
        int inputBlockSize = cryptoTransform.get_InputBlockSize();

        // Retrieve the key handle.
        IntPtr keyHandle = cryptoTransform.get_KeyHandle();

        // Retrieve the block size to write the bytes.
        int outputBlockSize = cryptoTransform.get_OutputBlockSize();

        try {
            // Determine if multiple blocks can be transformed.
            if (cryptoTransform.get_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 = cryptoTransform.TransformBlock(sourceBytes,
                        currentPosition, inputBlockSize, targetBytes,
                        currentPosition);

                    // Advance the current position in the sourceBytes array.
                    currentPosition += numBytesRead;
                }

                // Transform the final block of bytes.
                ubyte finalBytes[] = cryptoTransform.TransformFinalBlock(
                      sourceBytes, currentPosition,
                      sourceByteLength - currentPosition);

                // Copy the contents of the finalBytes array to the
                // targetBytes array.
                finalBytes.CopyTo(targetBytes, currentPosition);
            }
        }
        catch (System.Exception ex) {
            Console.WriteLine("Caught unexpected exception:" + ex.ToString());
        }

        // Determine if the current transform can be reused.
        if (!(cryptoTransform.get_CanReuseTransform())) {
            // Free up any used resources.
            cryptoTransform.Clear();
        }
        // Trim the extra bytes in the array that were not used.
        return TrimArray(targetBytes);
    } //EncodeBytes

    // Decode the specified byte array using CryptoAPITranform.
    private static ubyte[] DecodeBytes(ubyte sourceBytes[])
    {
        ubyte targetBytes[] = new ubyte[1024];
        int currentPosition = 0;
        // Create a DES decryptor from this instance to perform decryption.
        CryptoAPITransform cryptoTransform = (CryptoAPITransform)des.
            CreateDecryptor();
        int inputBlockSize = cryptoTransform.get_InputBlockSize();
        int sourceByteLength = sourceBytes.get_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 = cryptoTransform.TransformBlock(sourceBytes,
                    currentPosition, inputBlockSize, targetBytes,
                    currentPosition);
                // Advance the current position in the source array.
                currentPosition += numBytesRead;
            }
            // Transform the final block of bytes.
            ubyte finalBytes[] = cryptoTransform.TransformFinalBlock(sourceBytes,
                currentPosition, sourceByteLength - currentPosition);
            // Copy the contents of the finalBytes array to the targetBytes
            // array.
            finalBytes.CopyTo(targetBytes, currentPosition);
        }
        catch (System.Exception ex) {
            Console.WriteLine("Caught unexpected exception:" + ex.ToString());
        }

	// Strip out the second block of bytes.
        Array.Copy(targetBytes, (inputBlockSize * 2), targetBytes, inputBlockSize, targetBytes.length - (inputBlockSize * 2));

        // Trim the extra bytes in the array that were not used.
        return TrimArray(targetBytes);
    } //DecodeBytes

    // Resize the dimensions of the array to a size that contains only valid
    // bytes.
    private static ubyte[] TrimArray(ubyte targetArray[])
    {
        IEnumerator enum1 = targetArray.GetEnumerator();
        int i = 0;
        while (enum1.MoveNext()) {
            if (enum1.get_Current().ToString().Equals("0")) {
                break;
            }
            i++;
        }
        // Create a new array with the number of valid bytes.
        ubyte returnedArray[] = new ubyte[i];
        for (int j = 0; j < i; j++) {
            returnedArray.set_Item(j, targetArray.get_Item(j));
        }
        return returnedArray;
    } //TrimArray
} //Members
//
// This sample produces the following output:
//
// ** Phrase to be encoded: 012345678901234567890
// ** Phrase after encoding: AIGC(+b7X?^djAU?15ve?o
// ** Phrase after decoding: 012345678901234567890
// Sample ended successfully; press Enter to continue.

System.Object
  System.Security.Cryptography.CryptoAPITransform

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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft