This documentation is archived and is not being maintained.

ToBase64Transform Class

Converts a CryptoStream to base 64.

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

public class ToBase64Transform : ICryptoTransform, 

Base 64 Content-Transfer-Encoding represents arbitrary bit sequences in a form that is not human readable.

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

using System;
using System.IO;
using System.Security.Cryptography;

class Members
    static void Main(string[] args)
        string appPath = (System.IO.Directory.GetCurrentDirectory() + "\\");

        // Insert your file names into this method call.
        EncodeFromFile(appPath + "members.cs", appPath + "members.enc");

        Console.WriteLine("This sample completed successfully; " +
            "press Enter to exit.");

    // Read in the specified source file and write out an encoded target file. 
    private static void EncodeFromFile(string sourceFile, string targetFile) 
        // Verify members.cs exists at the specified directory. 
        if (!File.Exists(sourceFile))
            Console.Write("Unable to locate source file located at ");
            Console.WriteLine(sourceFile + ".");
            Console.Write("Please correct the path and run the ");
            Console.WriteLine("sample again.");

        // Retrieve the input and output file streams.
        FileStream inputFileStream = 
            new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
        FileStream outputFileStream = 
            new FileStream(targetFile, FileMode.Create, FileAccess.Write);

        // Create a new ToBase64Transform object to convert to base 64.
        ToBase64Transform base64Transform = new ToBase64Transform();

        // Create a new byte array with the size of the output block size.
        byte[] outputBytes = new byte[base64Transform.OutputBlockSize];

        // Retrieve the file contents into a byte array.
        byte[] inputBytes = new byte[inputFileStream.Length];
        inputFileStream.Read(inputBytes, 0, inputBytes.Length);

        // Verify that multiple blocks can not be transformed. 
        if (!base64Transform.CanTransformMultipleBlocks)
            // Initializie the offset size. 
            int inputOffset = 0;

            // Iterate through inputBytes transforming by blockSize. 
            int inputBlockSize = base64Transform.InputBlockSize;

            while(inputBytes.Length - inputOffset > inputBlockSize)
                    inputBytes.Length - inputOffset,

                inputOffset += base64Transform.InputBlockSize;

            // Transform the final block of data.
            outputBytes = base64Transform.TransformFinalBlock(
                inputBytes.Length - inputOffset);

            outputFileStream.Write(outputBytes, 0, outputBytes.Length);
            Console.WriteLine("Created encoded file at " + targetFile);

        // Determine if the current transform can be reused. 
        if (! base64Transform.CanReuseTransform)
            // Free up any used resources.

        // Close file streams.
// This sample produces the following output: 
// Created encoded file at C:\ConsoleApplication1\\membersvcs.enc 
// This sample completed successfully; press Enter to exit.


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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0