英語で読む

次の方法で共有


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 によって使用されているアンマネージド リソースを解放し、オプションでマネージド リソースも解放します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
Finalize()

ToBase64Transform で使用されるアンマネージ リソースを解放します。

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 で使用されるアンマネージ リソースを解放します。

適用対象

製品 バージョン
.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

こちらもご覧ください