CryptoProvider.BlockSize Eigenschap

Definitie

Hiermee haalt u de grootte van het coderingsblok op in bytes.

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

Waarde van eigenschap

De grootte van het coderingsblok, in bytes. De standaardblokgrootte voor Advanced Encryption Standard (AES) is 8.

Voorbeelden

In het volgende voorbeeld ziet u hoe u de eigenschap gebruikt bij het BlockSize converteren van gegevens met duidelijke tekst naar versleutelde tekstgegevens.

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.")

Opmerkingen

De clearText en cipherText buffers die worden doorgegeven aan Encrypt en Decrypt moeten n*BlockSize bytes lang zijn, waarbij 'n' een geheel getal is dat groter is dan of gelijk is aan 1.

Als CanMergeBlocks dat het is false, moeten buffers die worden Encrypt doorgegeven, dezelfde lengte hebben als de buffers die aan Decryptworden doorgegeven.

Als CanMergeBlocks dat het zo is true, kunnen buffers die worden Encrypt doorgegeven, een andere lengte hebben dan de buffers die worden Decrypt doorgegeven (alle buffergrootten moeten nog steeds een veelvoud van BlockSize bytes lang zijn).

A BlockSize van 1 geeft aan dat de codering een stroom-codering is; een BlockSize van 2 of hoger geeft een blok-codering aan.

Van toepassing op