Export (0) Print
Expand All

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 property int KeySize {
	int get ();
	void set (int value);
}

Property Value

Type: System::Int32
The size, in bits, of the key modulus used by the asymmetric algorithm.

ExceptionCondition
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:
    property int KeySize
    {
        virtual int get() override
        {
            return KeySizeValue;
        }

        virtual void set(int value) override
        {
            for (int i = 0; i < customValidKeySizes->Length; i++)
            {
                if (customValidKeySizes[i]->SkipSize == 0)
                {
                    if (customValidKeySizes[i]->MinSize == value)
                    {
                        KeySizeValue = value;
                        return;
                    }
                }
                else
                {
                    for (int j = customValidKeySizes[i]->MinSize;
                        j <= customValidKeySizes[i]->MaxSize;
                        j += customValidKeySizes[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 gcnew CryptographicException("Invalid key size.");
        }
    }

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft