ToBase64Transform 類別

定義

CryptoStream 轉換為 Base 64。

C#
public class ToBase64Transform : IDisposable, System.Security.Cryptography.ICryptoTransform
C#
public class ToBase64Transform : System.Security.Cryptography.ICryptoTransform
C#
[System.Runtime.InteropServices.ComVisible(true)]
public class ToBase64Transform : System.Security.Cryptography.ICryptoTransform
繼承
ToBase64Transform
屬性
實作

範例

下列程式代碼範例示範如何使用 類別的成員 ToBase64Transform

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

class Members
{
    [STAThread]
    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.");
        Console.ReadLine();
    }

    // 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.");
            return;
        }

        // 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)
            {
                base64Transform.TransformBlock(
                    inputBytes,
                    inputOffset,
                    inputBytes.Length - inputOffset,
                    outputBytes,
                    0);

                inputOffset += base64Transform.InputBlockSize;
                outputFileStream.Write(
                    outputBytes, 
                    0, 
                    base64Transform.OutputBlockSize);
            }

            // Transform the final block of data.
            outputBytes = base64Transform.TransformFinalBlock(
                inputBytes,
                inputOffset,
                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.
            base64Transform.Clear();
        }

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

備註

Base 64 Content-Transfer-Encoding 代表非人類可讀形式的任意位序列。

建構函式

ToBase64Transform()

初始化 ToBase64Transform 類別的新執行個體。

屬性

CanReuseTransform

取得值,表示目前的轉換是否可重複使用。

CanTransformMultipleBlocks

取得值,指出多個區塊是否可以轉換。

InputBlockSize

取得輸入區塊的大小。

OutputBlockSize

取得輸出區塊的大小。

方法

Clear()

釋放 ToBase64Transform 所使用的所有資源。

Dispose()

釋放 ToBase64Transform 類別目前的執行個體所使用的全部資源。

Dispose(Boolean)

釋放 ToBase64Transform 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Finalize()

釋放 ToBase64Transform 使用的 Unmanaged 資源。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
TransformBlock(Byte[], Int32, Int32, Byte[], Int32)

將輸入位元組陣列的指定區域轉換為 Base 64,並且將結果複製至輸出位元組陣列的指定區域。

TransformFinalBlock(Byte[], Int32, Int32)

將指定位元組陣列的指定區域轉換為 Base 64。

明確介面實作

IDisposable.Dispose()

釋放 ToBase64Transform 使用的 Unmanaged 資源。

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另請參閱