当前位置: 首页>>代码示例>>C#>>正文


C# RSACryptoServiceProvider类代码示例

本文整理汇总了C#中RSACryptoServiceProvider的典型用法代码示例。如果您正苦于以下问题:C# RSACryptoServiceProvider类的具体用法?C# RSACryptoServiceProvider怎么用?C# RSACryptoServiceProvider使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


RSACryptoServiceProvider类属于命名空间,在下文中一共展示了RSACryptoServiceProvider类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SignHash_NullArray

 public static void SignHash_NullArray()
 {
     using (var rsa = new RSACryptoServiceProvider())
     {
         Assert.Throws<ArgumentNullException>(() => rsa.SignHash(null, "SHA384"));
     }
 }
开发者ID:shiftkey-tester,项目名称:corefx,代码行数:7,代码来源:RSACryptoServiceProviderBackCompat.cs

示例2: GetRSAPrivateKey

        public static RSA GetRSAPrivateKey(this X509Certificate2 certificate)
        {
            if (certificate == null)
            {
                throw new ArgumentNullException("certificate");
            }

            if (!certificate.HasPrivateKey || !IsRSA(certificate))
            {
                return null;
            }

            using (SafeCertContextHandle certificateContext = X509Native.GetCertificateContext(certificate))
            using (SafeNCryptKeyHandle privateKeyHandle = X509Native.TryAcquireCngPrivateKey(certificateContext))
            {
                if (privateKeyHandle == null)
                {
                    if (LocalAppContextSwitches.DontReliablyClonePrivateKey)
                        return (RSA)certificate.PrivateKey;

                    // fall back to CAPI if we cannot acquire the key using CNG.
                    RSACryptoServiceProvider rsaCsp = (RSACryptoServiceProvider)certificate.PrivateKey;
                    CspParameters cspParameters = DSACertificateExtensions.CopyCspParameters(rsaCsp);
                    RSACryptoServiceProvider clone = new RSACryptoServiceProvider(cspParameters);
                    return clone;
                }

                CngKey key = CngKey.Open(privateKeyHandle, CngKeyHandleOpenOptions.None);
                return new RSACng(key);
            }
        }
开发者ID:ItsVeryWindy,项目名称:mono,代码行数:31,代码来源:RSACertificateExtensions.cs

示例3: initialize

 public void initialize()
 {
     this._hash_code = this.GetHashCode().ToString();
     this._user_cryptor = new RSACryptoServiceProvider();
     this._server_cryptor = new RSACryptoServiceProvider(2048);
     this._server_public_key = this._server_cryptor.ToXmlString(false);
 }
开发者ID:spox,项目名称:irisim,代码行数:7,代码来源:CryptManager.cs

示例4: LargeKeyImportExport

        public static void LargeKeyImportExport()
        {
            RSAParameters imported = TestData.RSA16384Params;

            using (RSA rsa = new RSACryptoServiceProvider())
            {
                try
                {
                    rsa.ImportParameters(imported);
                }
                catch (CryptographicException)
                {
                    // The key is pretty big, perhaps it was refused.
                    return;
                }

                RSAParameters exported = rsa.ExportParameters(false);

                Assert.Equal(exported.Modulus, imported.Modulus);
                Assert.Equal(exported.Exponent, imported.Exponent);
                Assert.Null(exported.D);

                exported = rsa.ExportParameters(true);

                AssertKeyEquals(ref imported, ref exported);
            }
        }
开发者ID:johnhhm,项目名称:corefx,代码行数:27,代码来源:ImportExport.cs

示例5: EncryptRSA

    /// <summary>
    ///     A string extension method that encrypts the string.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <param name="key">The key.</param>
    /// <returns>The encrypted string.</returns>
    /// <example>
    ///     <code>
    ///           using Microsoft.VisualStudio.TestTools.UnitTesting;
    ///           using Z.ExtensionMethods;
    ///           
    ///           namespace ExtensionMethods.Examples
    ///           {
    ///               [TestClass]
    ///               public class System_String_EncryptRSA
    ///               {
    ///                   [TestMethod]
    ///                   public void EncryptRSA()
    ///                   {
    ///                       // Type
    ///                       string @this = &quot;Fizz&quot;;
    ///           
    ///                       // Examples
    ///                       string value = @this.EncryptRSA(&quot;Buzz&quot;); // return Encrypted string;
    ///           
    ///                       // Unit Test
    ///                       Assert.AreEqual(&quot;Fizz&quot;, value.DecryptRSA(&quot;Buzz&quot;));
    ///                   }
    ///               }
    ///           }
    ///     </code>
    /// </example>
    public static string EncryptRSA(this string @this, string key) {
        var cspp = new CspParameters {KeyContainerName = key};
        var rsa = new RSACryptoServiceProvider(cspp) {PersistKeyInCsp = true};
        byte[] bytes = rsa.Encrypt(Encoding.UTF8.GetBytes(@this), true);

        return BitConverter.ToString(bytes);
    }
开发者ID:Nucs,项目名称:nlib,代码行数:39,代码来源:String.EncryptRSA.cs

示例6: DecryptSavedAnswerUnusualExponent

        public static void DecryptSavedAnswerUnusualExponent()
        {
            byte[] cipherBytes =
            {
                0x55, 0x64, 0x05, 0xF7, 0xBF, 0x99, 0xD8, 0x07,
                0xD0, 0xAC, 0x1B, 0x1B, 0x60, 0x92, 0x57, 0x95,
                0x5D, 0xA4, 0x5B, 0x55, 0x0E, 0x12, 0x90, 0x24,
                0x86, 0x35, 0xEE, 0x6D, 0xB3, 0x46, 0x3A, 0xB0,
                0x3D, 0x67, 0xCF, 0xB3, 0xFA, 0x61, 0xBB, 0x90,
                0x6D, 0x6D, 0xF8, 0x90, 0x5D, 0x67, 0xD1, 0x8F,
                0x99, 0x6C, 0x31, 0xA2, 0x2C, 0x8E, 0x99, 0x7E,
                0x75, 0xC5, 0x26, 0x71, 0xD1, 0xB0, 0xA5, 0x41,
                0x67, 0x19, 0xF7, 0x40, 0x04, 0xBE, 0xB2, 0xC0,
                0x97, 0xFB, 0xF6, 0xD4, 0xEF, 0x48, 0x5B, 0x93,
                0x81, 0xF8, 0xE1, 0x6A, 0x0E, 0xA0, 0x74, 0x6B,
                0x99, 0xC6, 0x23, 0xF5, 0x02, 0xDE, 0x47, 0x49,
                0x1E, 0x9D, 0xAE, 0x55, 0x20, 0xB5, 0xDE, 0xA0,
                0x04, 0x32, 0x37, 0x4B, 0x24, 0xE4, 0x64, 0x1B,
                0x1B, 0x4B, 0xC0, 0xC7, 0x30, 0x08, 0xA6, 0xAE,
                0x50, 0x86, 0x08, 0x34, 0x70, 0xE5, 0xB0, 0x3B,
            };

            byte[] output;

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportParameters(TestData.UnusualExponentParameters);
                output = rsa.Decrypt(cipherBytes, true);
            }

            Assert.Equal(TestData.HelloBytes, output);
        }
开发者ID:AustinWise,项目名称:corefx,代码行数:32,代码来源:EncryptDecrypt.cs

示例7: PaddedExport

        public static void PaddedExport()
        {
            // OpenSSL's numeric type for the storage of RSA key parts disregards zero-valued
            // prefix bytes.
            //
            // The .NET 4.5 RSACryptoServiceProvider type verifies that all of the D breakdown
            // values (P, DP, Q, DQ, InverseQ) are exactly half the size of D (which is itself
            // the same size as Modulus).
            //
            // These two things, in combination, suggest that we ensure that all .NET
            // implementations of RSA export their keys to the fixed array size suggested by their
            // KeySize property.
            RSAParameters diminishedDPParamaters = TestData.DiminishedDPParamaters;
            RSAParameters exported;

            using (RSA rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportParameters(diminishedDPParamaters);
                exported = rsa.ExportParameters(true);
            }

            // DP is the most likely to fail, the rest just otherwise ensure that Export
            // isn't losing data.
            AssertKeyEquals(ref diminishedDPParamaters, ref exported);
        }
开发者ID:johnhhm,项目名称:corefx,代码行数:25,代码来源:ImportExport.cs

示例8: ExportAutoKey

        public static void ExportAutoKey()
        {
            RSAParameters privateParams;
            RSAParameters publicParams;
            int keySize;

            using (RSA rsa = new RSACryptoServiceProvider())
            {
                keySize = rsa.KeySize;

                // We've not done anything with this instance yet, but it should automatically
                // create the key, because we'll now asked about it.
                privateParams = rsa.ExportParameters(true);
                publicParams = rsa.ExportParameters(false);

                // It shouldn't be changing things when it generated the key.
                Assert.Equal(keySize, rsa.KeySize);
            }

            Assert.Null(publicParams.D);
            Assert.NotNull(privateParams.D);

            ValidateParameters(ref publicParams);
            ValidateParameters(ref privateParams);

            Assert.Equal(privateParams.Modulus, publicParams.Modulus);
            Assert.Equal(privateParams.Exponent, publicParams.Exponent);
        }
开发者ID:johnhhm,项目名称:corefx,代码行数:28,代码来源:ImportExport.cs

示例9: DecryptSavedAnswer

        public static void DecryptSavedAnswer()
        {
            byte[] cipherBytes =
            {
                0x35, 0x6F, 0x8F, 0x2C, 0x4D, 0x1A, 0xAC, 0x6D,
                0xE7, 0x52, 0xA5, 0xDF, 0x26, 0x54, 0xA6, 0x34,
                0xF5, 0xBB, 0x14, 0x26, 0x1C, 0xE4, 0xDC, 0xA2,
                0xD8, 0x4D, 0x8F, 0x1C, 0x55, 0xD4, 0xC7, 0xA7,
                0xF2, 0x3C, 0x99, 0x77, 0x9F, 0xE4, 0xB7, 0x34,
                0xA6, 0x28, 0xB2, 0xC4, 0xFB, 0x6F, 0x85, 0xCA,
                0x19, 0x21, 0xCA, 0xC1, 0xA7, 0x8D, 0xAE, 0x95,
                0xAB, 0x9B, 0xA9, 0x88, 0x5B, 0x44, 0xC6, 0x9B,
                0x44, 0x26, 0x71, 0x5D, 0x02, 0x3F, 0x43, 0x42,
                0xEF, 0x4E, 0xEE, 0x09, 0x87, 0xEF, 0xCD, 0xCF,
                0xF9, 0x88, 0x99, 0xE8, 0x49, 0xF7, 0x8F, 0x9B,
                0x59, 0x68, 0x20, 0xF3, 0xA7, 0xB2, 0x94, 0xA4,
                0x23, 0x70, 0x83, 0xD9, 0xAC, 0xE7, 0x5E, 0xEE,
                0xE9, 0x7B, 0xE4, 0x4F, 0x73, 0x2E, 0x9B, 0xD8,
                0x2A, 0x75, 0xFB, 0x6C, 0xB9, 0x39, 0x6D, 0x72,
                0x8A, 0x9C, 0xCD, 0x58, 0x1A, 0x27, 0x79, 0x97,
            };

            byte[] output;

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportParameters(TestData.RSA1024Params);
                output = rsa.Decrypt(cipherBytes, true);
            }

            Assert.Equal(TestData.HelloBytes, output);
        }
开发者ID:AustinWise,项目名称:corefx,代码行数:32,代码来源:EncryptDecrypt.cs

示例10: CreateKey_LegacyProvider_RoundtripBlob

        public static void CreateKey_LegacyProvider_RoundtripBlob()
        {
            const int KeySize = 512;

            CspParameters cspParameters = new CspParameters(PROV_RSA_FULL);
            byte[] blob;

            using (var rsa = new RSACryptoServiceProvider(KeySize, cspParameters))
            {
                CspKeyContainerInfo containerInfo = rsa.CspKeyContainerInfo;
                Assert.Equal(PROV_RSA_FULL, containerInfo.ProviderType);
                Assert.Equal(KeySize, rsa.KeySize);

                blob = rsa.ExportCspBlob(true);
            }

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportCspBlob(blob);

                CspKeyContainerInfo containerInfo = rsa.CspKeyContainerInfo;

                // The provider information is not persisted in the blob
                Assert.Equal(PROV_RSA_AES, containerInfo.ProviderType);
                Assert.Equal(KeySize, rsa.KeySize);
            }
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:27,代码来源:RSACryptoServiceProviderTests.cs

示例11: ExportImportPublicOnly

        public static void ExportImportPublicOnly()
        {
            byte[] expectedExport = ByteUtils.HexToByteArray(
                "0602000000a40000525341310004000001000100e19a01644b82962a224781d1f60c2cc373b"
                + "798df541343f63c638f45fa96e11049c8d9e88bd56483ec3c2d56e9460d2b1140191841761c1523840221b0e"
                + "b6401dc4d09c54bf75cea25d9e191572fb2ec92c3559b35b3ef3fa695171bb1fddeb469792e49f0d17c769d0"
                + "a37f6a4a6584af39878eb21f9ba9eae8be9c39eac6ae0");

            byte[] exported;

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportParameters(TestData.CspTestKey);

                exported = rsa.ExportCspBlob(includePrivateParameters: false);
            }

            Assert.Equal(expectedExport, exported);

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportCspBlob(exported);

                byte[] exported2 = rsa.ExportCspBlob(includePrivateParameters: false);

                Assert.Equal(exported, exported2);

                Assert.Throws<CryptographicException>(() => rsa.ExportCspBlob(includePrivateParameters: true));
            }
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:30,代码来源:ImportExportCspBlob.cs

示例12: GenerateKey

        private static void GenerateKey(Func<RSA, int> getSize)
        {
            int keySize;

            using (var rsa = new RSACryptoServiceProvider())
            {
                keySize = getSize(rsa);
            }

            using (var rsa = new RSACryptoServiceProvider(keySize))
            {
                Assert.Equal(keySize, rsa.KeySize);

                // Some providers may generate the key in the constructor, but
                // all of them should have generated it before answering ExportParameters.
                RSAParameters keyParameters = rsa.ExportParameters(false);
                ImportExport.ValidateParameters(ref keyParameters);

                // KeySize should still be what we set it to originally.
                Assert.Equal(keySize, rsa.KeySize);

                // KeySize describes the size of the modulus in bits
                // So, 8 * the number of bytes in the modulus should be the same value.
                Assert.Equal(keySize, keyParameters.Modulus.Length * 8);
            }
        }
开发者ID:AustinWise,项目名称:corefx,代码行数:26,代码来源:KeyGeneration.cs

示例13: DefaultKeySize

 public static void DefaultKeySize()
 {
     using (var rsa = new RSACryptoServiceProvider())
     {
         Assert.Equal(1024, rsa.KeySize);
     }
 }
开发者ID:ESgarbi,项目名称:corefx,代码行数:7,代码来源:RSACryptoServiceProviderTests.cs

示例14: LargeKeyCryptRoundtrip

        public static void LargeKeyCryptRoundtrip()
        {
            byte[] output;

            using (var rsa = new RSACryptoServiceProvider())
            {
                try
                {
                    rsa.ImportParameters(TestData.RSA16384Params);
                }
                catch (CryptographicException)
                {
                    // The key is pretty big, perhaps it was refused.
                    return;
                }

                byte[] crypt = rsa.Encrypt(TestData.HelloBytes, true);

                Assert.Equal(rsa.KeySize, crypt.Length * 8);

                output = rsa.Decrypt(crypt, true);
            }

            Assert.Equal(TestData.HelloBytes, output);
        }
开发者ID:johnhhm,项目名称:corefx,代码行数:25,代码来源:EncryptDecrypt.cs

示例15: ApiInterop_NewToOld

 public static void ApiInterop_NewToOld()
 {
     using (var rsa = new RSACryptoServiceProvider())
     {
         byte[] newSignature = rsa.SignData(s_dataToSign, HashAlgorithmName.SHA384, RSASignaturePadding.Pkcs1);
         Assert.True(rsa.VerifyData(s_dataToSign, "SHA384", newSignature));
     }
 }
开发者ID:shiftkey-tester,项目名称:corefx,代码行数:8,代码来源:RSACryptoServiceProviderBackCompat.cs


注:本文中的RSACryptoServiceProvider类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。