Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

FromBase64Transform Class

Converts a CryptoStream from base 64.

For a list of all members of this type, see FromBase64Transform Members.

System.Object
   System.Security.Cryptography.FromBase64Transform

[Visual Basic]
Public Class FromBase64Transform
   Implements ICryptoTransform, IDisposable
[C#]
public class FromBase64Transform : ICryptoTransform, IDisposable
[C++]
public __gc class FromBase64Transform : public ICryptoTransform,
   IDisposable
[JScript]
public class FromBase64Transform implements ICryptoTransform,
   IDisposable

Thread Safety

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

Remarks

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

Example

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Security.Cryptography

Namespace ToBase64Transform_Examples
    Class MyMainClass
        Public Shared Sub Main()
            'Insert your file names into this method call.
            DecodeFromFile("c:\encoded.txt", "c:\roundtrip.txt")
        End Sub 'Main

        Public Shared Sub DecodeFromFile(ByVal inFileName As String, ByVal outFileName As String)
            Dim myTransform As New FromBase64Transform(FromBase64TransformMode.IgnoreWhiteSpaces)

            Dim myOutputBytes(myTransform.OutputBlockSize - 1) As Byte

            'Open the input and output files.
            Dim myInputFile As New FileStream(inFileName, FileMode.Open, FileAccess.Read)
            Dim myOutputFile As New FileStream(outFileName, FileMode.Create, FileAccess.Write)

            'Retrieve the file contents into a byte array.
            Dim myInputBytes(myInputFile.Length - 1) As Byte
            myInputFile.Read(myInputBytes, 0, myInputBytes.Length)

            'Transform the data in chunks the size of InputBlockSize.
            Dim i As Integer = 0
            While myInputBytes.Length - i > 4 'myTransform.InputBlockSize
                myTransform.TransformBlock(myInputBytes, i, 4, myOutputBytes, 0) 'myTransform.InputBlockSize
                i += 4 'myTransform.InputBlockSize
                myOutputFile.Write(myOutputBytes, 0, myTransform.OutputBlockSize)
            End While

            'Transform the final block of data.
            myOutputBytes = myTransform.TransformFinalBlock(myInputBytes, i, myInputBytes.Length - i)
            myOutputFile.Write(myOutputBytes, 0, myOutputBytes.Length)

            'Free up any used resources.
            myTransform.Clear()

            myInputFile.Close()
            myOutputFile.Close()
        End Sub 'DecodeFromFile
    End Class 'MyMainClass
End Namespace 'ToBase64Transform_Examples

[C#] 
using System;
using System.IO;
using System.Security.Cryptography;

namespace ToBase64Transform_Examples
{
    class MyMainClass
    {
        public static void Main()
        {
            //Insert your file names into this method call.
            DecodeFromFile("c:\\encoded.txt", "c:\\roundtrip.txt");
        }

        public static void DecodeFromFile(string inFileName, string outFileName)
        {
            FromBase64Transform myTransform = new FromBase64Transform(FromBase64TransformMode.IgnoreWhiteSpaces);

            byte[] myOutputBytes = new byte[myTransform.OutputBlockSize];

            //Open the input and output files.
            FileStream myInputFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
            FileStream myOutputFile = new FileStream(outFileName, FileMode.Create, FileAccess.Write);

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

            //Transform the data in chunks the size of InputBlockSize.
            int i = 0;
            while(myInputBytes.Length - i > 4/*myTransform.InputBlockSize*/)
            {
                myTransform.TransformBlock(myInputBytes, i, 4/*myTransform.InputBlockSize*/, myOutputBytes, 0);
                i += 4/*myTransform.InputBlockSize*/;
                myOutputFile.Write(myOutputBytes, 0, myTransform.OutputBlockSize);
            }
            
            //Transform the final block of data.
            myOutputBytes = myTransform.TransformFinalBlock(myInputBytes, i, myInputBytes.Length - i);
            myOutputFile.Write(myOutputBytes, 0, myOutputBytes.Length);

            //Free up any used resources.
            myTransform.Clear();

            myInputFile.Close();
            myOutputFile.Close();
        }
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
using namespace System::Security::Cryptography;

class MyMainClass
{
public:
    static void DecodeFromFile(String *inFileName, String *outFileName)
    {
        FromBase64Transform * myTransform = new FromBase64Transform(FromBase64TransformMode::IgnoreWhiteSpaces);

        Byte myOutputBytes __gc[] = new Byte __gc[myTransform->OutputBlockSize];

        //Open the input and output files.
        FileStream * myInputFile = new FileStream(inFileName, FileMode::Open, FileAccess::Read);
        FileStream * myOutputFile = new FileStream(outFileName, FileMode::Create, FileAccess::Write);

        //Retrieve the file contents into a Byte array.
        Byte myInputBytes __gc[] = new Byte __gc[myInputFile->Length];
        myInputFile->Read(myInputBytes, 0, myInputBytes->Length);

        //Transform the data in chunks the size of InputBlockSize.
        int i = 0;
        while(myInputBytes->Length - i > 4/*myTransform->InputBlockSize*/)
        {
            myTransform->TransformBlock(myInputBytes, i, 4/*myTransform->InputBlockSize*/, myOutputBytes, 0);
            i += 4/*myTransform->InputBlockSize*/;
            myOutputFile->Write(myOutputBytes, 0, myTransform->OutputBlockSize);
        }
        
        //Transform the final block of data.
        myOutputBytes = myTransform->TransformFinalBlock(myInputBytes, i, myInputBytes->Length - i);
        myOutputFile->Write(myOutputBytes, 0, myOutputBytes->Length);

        //Free up any used resources.
        myTransform->Clear();

        myInputFile->Close();
        myOutputFile->Close();
    }
};

int main()
{
    MyMainClass * m = new MyMainClass();
    //Insert your file names into this method call.
    m->DecodeFromFile("c:\\encoded.txt", "c:\\roundtrip.txt");
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Security.Cryptography

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

FromBase64Transform Members | System.Security.Cryptography Namespace | Cryptographic Services

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.