Skip to main content
SymmetricKeyAlgorithmProvider.BlockLength | blockLength property

Applies to Windows and Windows Phone

Gets the size, in bytes, of the cipher block for the open algorithm.

Syntax


var blockLength = symmetricKeyAlgorithmProvider.blockLength;

public uint BlockLength { get; }

Public ReadOnly Property BlockLength As UInteger

public:
property unsigned int BlockLength { 
   unsigned int get();
}

Property value

Type: Number [JavaScript] | System.UInt32 [.NET] | uint32 [C++]

Block size.

Remarks

If you are not using a PKCS#7 block padding algorithm (which automatically pads your content to the correct length), you must ensure that the message to be encrypted is an exact multiple of the length supported by the algorithm. For more information, see the following code example.

Examples


public IBuffer SampleCipherEncryption(
    String strMsg,
    String strAlgName,
    UInt32 keyLength,
    out BinaryStringEncoding encoding,
    out IBuffer iv,
    out CryptographicKey key)
{
    // Initialize the initialization vector.
    iv = null;

    // Initialize the binary encoding value.
    encoding = BinaryStringEncoding.Utf8;

    // Create a buffer that contains the encoded message to be encrypted. 
    IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(strMsg, encoding);

    // Open a symmetric algorithm provider for the specified algorithm. 
    SymmetricKeyAlgorithmProvider objAlg = SymmetricKeyAlgorithmProvider.OpenAlgorithm(strAlgName);

    // Demonstrate how to retrieve the name of the algorithm used.
    String strAlgNameUsed = objAlg.AlgorithmName;

    // Determine whether the message length is a multiple of the block length.
    // This is not necessary for PKCS #7 algorithms which automatically pad the
    // message to an appropriate length.
    if (!strAlgName.Contains("PKCS7"))
    {
       if ((buffMsg.Length % objAlg.BlockLength) != 0)
        {
            throw new Exception("Message buffer length must be multiple of block length.");
        }
    }

    // Create a symmetric key.
    IBuffer keyMaterial = CryptographicBuffer.GenerateRandom(keyLength);
    key = objAlg.CreateSymmetricKey(keyMaterial);

    // CBC algorithms require an initialization vector. Here, a random
    // number is used for the vector.
    if (strAlgName.Contains("CBC"))
    {
        iv = CryptographicBuffer.GenerateRandom(objAlg.BlockLength);
    }

    // Encrypt the data and return.
    IBuffer buffEncrypt = CryptographicEngine.Encrypt(key, buffMsg, iv);
    return buffEncrypt;
}

Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.Security.Cryptography.Core
Windows::Security::Cryptography::Core [C++]

Metadata

Windows.winmd

See also

SymmetricKeyAlgorithmProvider
Symmetric Key Encryption