當前位置: 首頁>>代碼示例>>C#>>正文


C# KeySizes類代碼示例

本文整理匯總了C#中System.Security.Cryptography.KeySizes的典型用法代碼示例。如果您正苦於以下問題:C# KeySizes類的具體用法?C# KeySizes怎麽用?C# KeySizes使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


KeySizes類屬於System.Security.Cryptography命名空間,在下文中一共展示了KeySizes類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
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.
            SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
            ShowKeys(rijn.LegalKeySizes, rijn.ToString());
            Console.WriteLine("rijn.blocksize:" + rijn.BlockSize);

            // Create a new RSA algorithm and display its key values.
            RSACryptoServiceProvider rsaCSP = 
                new RSACryptoServiceProvider(384);
            ShowKeys(rsaCSP.LegalKeySizes, rsaCSP.ToString());
            Console.WriteLine("RSACryptoServiceProvider KeySize = " + 
                rsaCSP.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);
        }
    }
}
//
開發者ID:.NET開發者,項目名稱:System.Security.Cryptography,代碼行數:63,代碼來源:KeySizes

輸出:

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.RijndaelManaged
object.
Minimum key size bits: 128
Maximum key size bits: 256
Interval between key size bits: 64
rijn.blocksize:128

KeySizes retrieved from the
System.Security.Cryptography.RSACryptoServiceProvider object.
Minimum key size bits: 384
Maximum key size bits: 16384
Interval between key size bits: 8
RSACryptoServiceProvider KeySize = 384
This sample completed successfully; press Enter to exit.


注:本文中的System.Security.Cryptography.KeySizes類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。