• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# RSACryptoServiceProvider类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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


示例16: ApiInterop_OldToNew_Positional

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


示例17: ToPublicRSAParameters

 public static RSAParameters ToPublicRSAParameters(this string publicKeyXml)
 {
     using (var rsa = new RSACryptoServiceProvider())
     {
         rsa.FromXmlString(publicKeyXml);
         return rsa.ExportParameters(includePrivateParameters: false);
     }
 }
开发者ID:pkmnfrk,项目名称:ServiceStack,代码行数:8,代码来源:CryptUtils.cs


示例18: ToPublicKeyXml

 public static string ToPublicKeyXml(this RSAParameters publicKey)
 {
     using (var rsa = new RSACryptoServiceProvider())
     {
         rsa.ImportParameters(publicKey);
         return rsa.ToXmlString(includePrivateParameters: false);
     }
 }
开发者ID:pkmnfrk,项目名称:ServiceStack,代码行数:8,代码来源:CryptUtils.cs


示例19: FromPrivateRSAParameters

 public static string FromPrivateRSAParameters(this RSAParameters privateKey)
 {
     using (var rsa = new RSACryptoServiceProvider())
     {
         rsa.ImportParameters(privateKey);
         return rsa.ToXmlString(includePrivateParameters: true);
     }
 }
开发者ID:pkmnfrk,项目名称:ServiceStack,代码行数:8,代码来源:CryptUtils.cs


示例20: signSamlElement

    /// <summary>
    /// Sign the XML String of SamlResponse
    /// </summary>
    /// <param name="xmlString"></param>
    /// <returns>Digital signed SamlReponse string</returns>
    public static String signSamlElement(String xmlString)
    {
        // Create a new CspParameters object to specify a key container.
         CspParameters cspParams = new CspParameters();

         cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
         //    cspParams.KeyContainerName = "GOOGLE";
        //     cspParams.ProviderName = "HF";

         string pb = "<RSAKeyValue><Modulus>wTI341fDKEG9mV9VDFRj/XKf5nZxfadISavENRbwPlKZBipYAi6zgVNPJ7nhSH4qdXqphOreXFFmwsg8JzxHLJRJ8yjIfiG3ORuRaHO0dpTslSQ4wz5qVroj4avI3m5pL6jFgtaWJkWlr7uzq4xrdKwu+wZiOaNNCFjqUo18ycE=</Modulus><Exponent>AQAB</Exponent><P>86FAPMQJey3PrI+PBPnMgn8xzR3qy/WBjUihKn+Fb9tP7GipWD9oi3tkdR/KZfBcvSoacDQqxMg8Y+aY90glGQ==</P><Q>ywFrg+GccDYsFwOZJsgzC8FXBQf9jalFbfuRdjrrH0Cd2JqHXC/nrpc7YB3qOORaWSuxWorGdN3+o42qszX26Q==</Q><DP>L3udDnrSsjxCfopYQIsDDegGZ8jN60SFJGkkaCkEc8GVuSjI4JczJAQ/lwhEJUwMdx3Om1G/iCzSgFIAPCnGeQ==</DP><DQ>HX0nUREE2IgF/5HWPXv3bk23hlOS0XE1VLSmfLYyUWfhhgVshEexL/tn9J5j17/UH//o0241ReS5iKibk0zTgQ==</DQ><InverseQ>IC++K/C2NT5w01BYp5dcB1sXmWH32oFB1bmgcAkwK2VbQm9a9Xt1YdXtMVUEkxln7Inciny8oEfwdDiUjc82KQ==</InverseQ><D>byvGnTvTQUcTIz6IYh/tqdpbyPI/PF8Wac49iY85j6NYCwQywI6/HJwj4GhGCsEPDasYATRl4Bm3WD6A3tMA4NUw/RYfdutL2vDXjYXZMETWnABeeTdPK9haPw/NrcvhWRkGqNyeHG1soqmrF/x/0Xh5EYTv4KtrIJrPpKFajAE=</D></RSAKeyValue>";
        // Create a new RSA signing key and save it in the container.
         RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider();

            /*  RSACryptoServiceProvider rsaKey;
         if (System.Web.HttpContext.Current == null) // WinForm
             rsaKey = new RSACryptoServiceProvider();
         else // WebForm - Uses Machine store for keys
             rsaKey = new RSACryptoServiceProvider(cspParams);
        */

         rsaKey.FromXmlString(pb);

          /*  X509Certificate2 cert = new X509Certificate2(@"C:\certificate.pfx", "");
        RSACryptoServiceProvider rsaKey = cert.PrivateKey as RSACryptoServiceProvider;*/

           // Create a new XML document
        XmlDocument xmlDoc = new XmlDocument();

        // Load an XML String into the XmlDocument object
        xmlDoc = Util.createXmlDom(xmlString);

        // Sign the XML document
        SignXml(xmlDoc, rsaKey);

        // convert XmlDocument to String
           /*     MemoryStream stream = new MemoryStream();

        XmlTextWriter writer = new XmlTextWriter(stream, null);

        writer.Formatting = Formatting.Indented;

        xmlDoc.Save(writer);

        StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);

        stream.Position = 0;

        string res = sr.ReadToEnd();

        sr.Close();

        stream.Close();

        return res;
        * */

        return xmlDoc.OuterXml;
    }
开发者ID:brooklynb7,项目名称:CGA,代码行数:63,代码来源:XmlDigitalSigner.cs



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# RSAParameters类代码示例发布时间:2022-05-24
下一篇:
C# RMS类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap