CryptoProvider.BlockSize 屬性

定義

取得密碼區塊大小 (以位元組為單位)。Gets the cipher block size, in bytes.

public:
 property int BlockSize { int get(); };
public int BlockSize { get; }
member this.BlockSize : int
Public ReadOnly Property BlockSize As Integer

屬性值

Int32

密碼區塊大小 (以位元組為單位)。The cipher block size, in bytes. 進階加密標準 (AES) 的預設區塊大小為8。The default block size for Advanced Encryption Standard (AES) is 8.

範例

下列範例顯示如何 BlockSize 在將純文字資料轉換成加密文字資料時,使用屬性。The following example shows how to use the BlockSize property in converting clear-text data to encrypted-text data.

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 cipherText 傳遞至的和緩衝區 Encrypt Decrypt 長度必須是 n * BlockSize 個位元組,其中 ' n ' 是大於或等於1的整數。The clearText and cipherText buffers passed to Encrypt and Decrypt must be n*BlockSize bytes in length, where 'n' is an integer greater than or equal to 1.

如果 CanMergeBlocksfalse ,則傳遞至的緩衝區 Encrypt 必須與傳遞至的緩衝區長度相同 DecryptIf CanMergeBlocks is false, buffers passed to Encrypt must be the same length as the buffers passed to Decrypt.

如果 CanMergeBlockstrue ,則傳遞至 Encrypt 的緩衝區可以與傳遞給 (的緩衝區長度不同,而 Decrypt 所有緩衝區大小仍然必須一律為位元組的倍數, BlockSize) 。If CanMergeBlocks is true, buffers passed to Encrypt can be a different length from the buffers passed to Decrypt (all buffer sizes must still always be a multiple of BlockSize bytes in length).

BlockSize的為1表示加密為數據流加密, BlockSize 而或大於2則表示區塊密碼。A BlockSize of 1 indicates that the cipher is a stream cipher; a BlockSize of 2 or greater indicates a block cipher.

適用於