KeySize Property
Collapse the table of content
Expand the table of content

AsymmetricAlgorithm.KeySize Property

 

Gets or sets the size, in bits, of the key modulus used by the asymmetric algorithm.

Namespace:   System.Security.Cryptography
Assembly:  mscorlib (in mscorlib.dll)

public virtual int KeySize { get; set; }

Property Value

Type: System.Int32

The size, in bits, of the key modulus used by the asymmetric algorithm.

Exception Condition
CryptographicException

The key modulus size is invalid.

The valid key sizes are specified by the particular implementation of the asymmetric algorithm and are listed in the LegalKeySizes property.

The following code example demonstrates how to override the KeySize property to verify that it falls within the range identified in the local keySizes member variable. This code example is part of a larger example provided for the AsymmetricAlgorithm class.

public override int KeySize 
{
    get { return KeySizeValue; }
    set
    {
        for (int i=0; i < keySizes.Length; i++)
        {
            if (keySizes[i].SkipSize == 0) 
            {
                if (keySizes[i].MinSize == value)
                {
                    KeySizeValue = value;
                    return;
                }
            }
            else
            {
                for (int j = keySizes[i].MinSize;
                    j <= keySizes[i].MaxSize;
                    j += keySizes[i].SkipSize)
                {
                    if (j == value)
                    {
                        KeySizeValue = value;
                        return;
                    }
                }
            }
        }

        // If the key does not fall within the range identified 
        // in the keySizes member variable, throw an exception.
        throw new CryptographicException("Invalid key size.");
    }
}

.NET Framework
Available since 1.1
Windows Phone Silverlight
Available since 7.1
Return to top
Show:
© 2016 Microsoft