KeySizes Class

Definition

Determines the set of valid key sizes for the symmetric cryptographic algorithms.

C#
public sealed class KeySizes
C#
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class KeySizes
Inheritance
KeySizes
Attributes

Examples

The following example shows the use of members of the KeySizes class.

C#
using System;
using System.Security.Cryptography;

namespace Contoso
{
    class KeySizesMembers
    {
        [STAThread]
        static void Main(string[] args)
        {
            // Initializes a new instance of the KeySizes class with the
            // specified key values.
            int minSize = 64;
            int maxSize = 1024;
            int skipSize = 64;
            KeySizes keySizes = new KeySizes(minSize, maxSize, skipSize);

            // Show the values of the keys.
            ShowKeys(new KeySizes[1]{keySizes}, "Custom Keys");

            // Create a new symmetric algorithm and display its key values.
            Aes aes = Aes.Create();
            ShowKeys(aes.LegalKeySizes, aes.ToString());
            Console.WriteLine("aes.blocksize:" + aes.BlockSize);

            // Create a new RSA algorithm and display its key values.
            RSA rsa = RSA.Create();
            ShowKeys(rsa.LegalKeySizes, rsa.ToString());
            Console.WriteLine("RSA KeySize = " + 
                rsa.KeySize);

            Console.WriteLine("This sample completed successfully; " +
                "press Enter to exit.");
            Console.ReadLine();
        }

        // Display specified KeySize properties to the console.
        private static void ShowKeys(KeySizes[] keySizes, string objectName)
        {
            // Retrieve the first KeySizes in the array.
            KeySizes firstKeySize = keySizes[0];

            // Retrieve the minimum key size in bits.
            int minKeySize = firstKeySize.MinSize;
                
            // Retrieve the maximum key size in bits.
            int maxKeySize = firstKeySize.MaxSize;
                
            // Retrieve the interval between valid key size in bits.
            int skipKeySize = firstKeySize.SkipSize;

            Console.Write("\n KeySizes retrieved from the ");
            Console.WriteLine(objectName + " object.");
            Console.WriteLine("Minimum key size bits: " + minKeySize);
            Console.WriteLine("Maximum key size bits: " + maxKeySize);
            Console.WriteLine("Interval between key size bits: " + 
                skipKeySize);
        }
    }
}
//
// This sample produces the following output:
//
// KeySizes retrieved from the Custom Keys object.
// Minimum key size bits: 64
// Maximum key size bits: 1024
// Interval between key size bits: 64
// 
// KeySizes retrieved from the System.Security.Cryptography.Aes
// object.
// Minimum key size bits: 128
// Maximum key size bits: 256
// Interval between key size bits: 64
// aes.blocksize:128
// 
// KeySizes retrieved from the
// System.Security.Cryptography.RSA object.
// Minimum key size bits: 512
// Maximum key size bits: 16384
// Interval between key size bits: 64
// RSA KeySize = 2048
// This sample completed successfully; press Enter to exit.

Constructors

KeySizes(Int32, Int32, Int32)

Initializes a new instance of the KeySizes class with the specified key values.

Properties

MaxSize

Specifies the maximum key size.

MinSize

Specifies the minimum key size.

SkipSize

Specifies the interval between valid key sizes.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

See also