Share via


CryptoProvider.Encrypt(Byte[]) メソッド

定義

クリア テキストを暗号化テキストに暗号化します。

public:
 cli::array <System::Byte> ^ Encrypt(cli::array <System::Byte> ^ clearText);
public byte[] Encrypt (byte[] clearText);
member this.Encrypt : byte[] -> byte[]
Public Function Encrypt (clearText As Byte()) As Byte()

パラメーター

clearText
Byte[]

暗号化するクリア テキスト コンテンツ。

戻り値

Byte[]

指定された clearText の暗号化済みの暗号化テキスト。

例外

clearText が null です。

暗号化は許可されていません。

次の例では、 メソッドを Encrypt 使用してクリア テキスト データを暗号化テキスト データに変換する方法を示します。

WriteStatus("   Binding the author's UseLicense and");
WriteStatus("       obtaining the CryptoProvider.");
using (CryptoProvider cryptoProvider =
            authorsUseLicense.Bind(_secureEnv))
{
    WriteStatus("   Writing encrypted content.");
    using (Stream clearTextStream =
                File.OpenRead(contentFile) )
    {
        using (Stream cryptoTextStream =
                    File.OpenWrite(encryptedFile))
        {
            // Write the length of the source content file
            // as the first four bytes of the encrypted file.
            cryptoTextStream.Write(
                BitConverter.GetBytes(clearTextStream.Length),
                0, sizeof(Int32));

            // Allocate clearText buffer.
            byte[] clearTextBlock =
                new byte[cryptoProvider.BlockSize];

            // Encrypt clearText to cryptoText block by block.
            for(;;)
            {   // Read clearText block.
                int readCount = ReliableRead(
                                    clearTextStream,
                                    clearTextBlock, 0 ,
                                    cryptoProvider.BlockSize);
                // readCount of zero is end of data.
                if (readCount == 0)  break; // for

                // Encrypt clearText to cryptoText.
                byte[] cryptoTextBlock =
                    cryptoProvider.Encrypt(clearTextBlock);

                // Write cryptoText block.
                cryptoTextStream.Write(cryptoTextBlock, 0,
                                       cryptoTextBlock.Length);
            }
            WriteStatus("   Closing '" + encryptedFilename + "'.");
        }// end:using (Stream cryptoTextStream =
    }// end:using (Stream clearTextStream =
}// end:using (CryptoProvider cryptoProvider =
WriteStatus("   Done - Content encryption complete.");
WriteStatus("   Binding the author's UseLicense and")
WriteStatus("       obtaining the CryptoProvider.")
Using cryptoProvider As CryptoProvider = authorsUseLicense.Bind(_secureEnv)
    WriteStatus("   Writing encrypted content.")
    Using clearTextStream As Stream = File.OpenRead(contentFile)
        Using cryptoTextStream As Stream = File.OpenWrite(encryptedFile)
            ' Write the length of the source content file
            ' as the first four bytes of the encrypted file.
            Dim expression As Int32
            cryptoTextStream.Write(BitConverter.GetBytes(clearTextStream.Length), 0, Len(expression))

            ' Allocate clearText buffer.
            Dim clearTextBlock(cryptoProvider.BlockSize - 1) As Byte

            ' Encrypt clearText to cryptoText block by block.
            Do
                Dim readCount As Integer = ReliableRead(clearTextStream, clearTextBlock, 0, cryptoProvider.BlockSize)
                ' readCount of zero is end of data.
                If readCount = 0 Then ' for
                    Exit Do
                End If

                ' Encrypt clearText to cryptoText.
                Dim cryptoTextBlock() As Byte = cryptoProvider.Encrypt(clearTextBlock)

                ' Write cryptoText block.
                cryptoTextStream.Write(cryptoTextBlock, 0, cryptoTextBlock.Length)
            Loop
            WriteStatus("   Closing '" & encryptedFilename & "'.")
        End Using ' end:using (Stream cryptoTextStream =
    End Using ' end:using (Stream clearTextStream =
End Using ' end:using (CryptoProvider cryptoProvider =
WriteStatus("   Done - Content encryption complete.")

注釈

バッファーの clearText バイト長は、暗号 BlockSize プロパティの倍数である必要があります。

デジタル著作権管理システムは、AES ブロック暗号を使用します。 AES では、ブロックは独立して暗号化され、同一のクリア テキストの 2 つのブロックによって同じ暗号テキスト結果が生成されます。 独立したブロック暗号化による潜在的な復号化の脅威を最小限に抑えるために、アプリケーションでは、同じクリア テキスト ブロックを暗号化しないように、圧縮などのコンテンツを変更する方法を採用する必要があります。

適用対象

こちらもご覧ください