Пример #1
0
        void ShowCreateRSAKeyPage()
        {
            Console.Clear();
            Console.WriteLine("----- C# RSA 操作示例 -----");
            Console.Write("RSA密钥位数:");
            int          rsaKeySize = int.Parse(Console.ReadLine());
            RSASecretKey rsaKey     = GenerateRSASecretKey(rsaKeySize);

            _generatedRSAKey.Add(rsaKey);
            DisplayRSAKey(rsaKey);
            Console.ReadKey();
        }
Пример #2
0
 void DisplayRSAKeyInXml(RSASecretKey key, bool showPublicKey = true, bool showPrivateKey = true)
 {
     if (showPrivateKey)
     {
         Write(ConsoleColor.Green, "PrivateKey:");
         Console.WriteLine(FormatXml(RSAKeyConverter.ToXmlPrivateKey(key.PrivateKey)));
     }
     if (showPublicKey)
     {
         Write(ConsoleColor.Green, "PublicKey:");
         Console.WriteLine(FormatXml(RSAKeyConverter.ToXmlPublicKey(key.PublicKey)));
     }
 }
Пример #3
0
 void DisplayRSAKey(RSASecretKey key, bool showPublicKey = true, bool showPrivateKey = true)
 {
     if (showPrivateKey)
     {
         Write(ConsoleColor.Green, "PrivateKey:");
         Console.WriteLine(key.PrivateKey);
     }
     if (showPublicKey)
     {
         Write(ConsoleColor.Green, "PublicKey:");
         Console.WriteLine(key.PublicKey);
     }
 }
Пример #4
0
        /// <summary>
        /// generate RSA secret key
        /// </summary>
        /// <param name="keySize">the size of the key,must from 384 bits to 16384 bits in increments of 8 </param>
        /// <returns></returns>
        RSASecretKey GenerateRSASecretKey(int keySize)
        {
            if (keySize % 8 != 0 || keySize < 384 || keySize > 16384)
            {
                throw new ArgumentOutOfRangeException(nameof(keySize), "The range of KeySize must from 384 bits to 16384 bits in increments of 8");
            }
            RSASecretKey rsaKey = new RSASecretKey();

            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keySize))
            {
                rsaKey.PrivateKey = rsa.ToXmlString(true);
                rsaKey.PublicKey  = rsa.ToXmlString(false);
            }
            rsaKey.PrivateKey = RSAKeyConverter.FromXmlPrivateKey(rsaKey.PrivateKey);
            rsaKey.PublicKey  = RSAKeyConverter.FromXmlPublicKey(rsaKey.PublicKey);
            return(rsaKey);
        }