BlockSize Property

CryptoProvider.BlockSize Property


Gets the cipher block size, in bytes.

Namespace:   System.Security.RightsManagement
Assembly:  WindowsBase (in WindowsBase.dll)

public int BlockSize { get; }

Property Value

Type: System.Int32

The cipher block size, in bytes. The default block size for Advanced Encryption Standard (AES) is 8.

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.

If CanMergeBlocks is false, buffers passed to Encrypt must be the same length as the buffers passed to Decrypt.

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

A BlockSize of 1 indicates that the cipher is a stream cipher; a BlockSize of 2 or greater indicates a block cipher.

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 =
    WriteStatus("   Writing encrypted content.");
    using (Stream clearTextStream =
                File.OpenRead(contentFile) )
        using (Stream cryptoTextStream =
            // Write the length of the source content file
            // as the first four bytes of the encrypted file.
                0, sizeof(Int32));

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

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

                // Encrypt clearText to cryptoText.
                byte[] cryptoTextBlock =

                // Write cryptoText block.
                cryptoTextStream.Write(cryptoTextBlock, 0,
            WriteStatus("   Closing '" + encryptedFilename + "'.");
        }// end:using (Stream cryptoTextStream =
    }// end:using (Stream clearTextStream =
}// end:using (CryptoProvider cryptoProvider =
WriteStatus("   Done - Content encryption complete.");

.NET Framework
Available since 3.0
Return to top
© 2015 Microsoft