KeySizes 类

定义

确定对称加密算法的一组有效密钥大小。

C#
public sealed class KeySizes
C#
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class KeySizes
继承
KeySizes
属性

示例

以下示例演示如何使用 类的成员 KeySizes

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.

构造函数

KeySizes(Int32, Int32, Int32)

使用指定的密钥值初始化 KeySizes 类的新实例。

属性

MaxSize

指定最大密钥大小。

MinSize

指定最小密钥大小。

SkipSize

指定有效密钥大小之间的间隔。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

产品 版本
.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

另请参阅