簡體   English   中英

如何在 C# 中使用帶有 ProtectSection 方法的 RsaProtectedConfigurationProvider 創建可導出的 RSA 密鑰

[英]How can I create an exportable RSA key with RsaProtectedConfigurationProvider with ProtectSection method in C#

在以下示例中,我保護“Sleutels.config”文件的“DemoWinApp.Properties.Settings”部分。

    private static void toggleProtectionSleutelsConfig()
    {
        var fileMap = new ConfigurationFileMap(@"D:\Experimenten\ReadProtectedConfigFile\Sleutels.config");
        var configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
        var sectionGroup = configuration.GetSectionGroup("applicationSettings"); // This is the section group name, change to your needs
        var section = (ClientSettingsSection)sectionGroup.Sections.Get("DemoWinApp.Properties.Settings"); // This is the section name, change to your needs
        var setting = section.Settings.Get("SecretMessage"); // This is the setting name, change to your needs
        Console.WriteLine(setting.Value.ValueXml.InnerText);

        // Toggle beveiliging
        if (!section.SectionInformation.IsProtected)
        {
            //Protecting the specified section with the specified provider
            section.SectionInformation.ProtectSection("RSA");
        }
        else
        {
            section.SectionInformation.UnprotectSection();
        }
        section.SectionInformation.ForceSave = true;
        configuration.Save(ConfigurationSaveMode.Modified);


        Console.ReadKey();
    }

“Sleutels.config”文件的內容是:

 <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, &#xD;&#xA; System, Version=2.0.0.0, Culture=neutral, &#xD;&#xA; PublicKeyToken=b77a5c561934e089"> <section name="DemoWinApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <applicationSettings> <DemoWinApp.Properties.Settings> <setting name="SecretMessage" serializeAs="String"> <value>This is the secret message.</value> </setting> </DemoWinApp.Properties.Settings> </applicationSettings> <configProtectedData> <providers> <add name="RSA" type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0,&#xD;&#xA; Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,&#xD;&#xA; processorArchitecture=MSIL" keyContainerName="RobinsKeys" useMachineContainer="true" /> </providers> </configProtectedData> </configuration>

運行代碼后,“Sleutels.config”文件被加密,並在C:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Crypto\\RSA\\MachineKeys創建一個 RSA 密鑰容器

如果我嘗試使用命令行導出 RSA 密鑰容器:

c:\windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -pc "RobinsKeys" –exp

然后我收到錯誤消息:

Exporting RSA Keys to file...
Key not valid for use in specified state.

這意味着 RSA 密鑰容器未標記為“可導出”。 如果您要使用命令行創建密鑰容器,則有一個可選參數“-exp”可將密鑰標記為可導出。

例如:aspnet_regiis -pc "RobinsKeys" -exp

使用section.SectionInformation.ProtectSection("RSA");-exp選項是否也可用section.SectionInformation.ProtectSection("RSA"); 代碼中的方法還是作為“Sleutels.config”配置文件中RSA提供程序部分中的配置選項?

任何幫助表示贊賞!

總結一下討論,您需要先以編程方式創建RSA 加密容器,然后才能使用它來存儲 RSA 密鑰。

原因是RSAProtectedConfigurationProvider沒有使自動創建的 RSA 密鑰容器可導出的選項。

正如您在聊天中所寫,此解決方法可以通過以下示例代碼實現(我已向控制台添加了一些輸出, 此處解釋打印的 RSA 參數):

void Main()
{
   // Create the CspParameters object and set the key container
   // name used to store the RSA key pair.
   var cp = new System.Security.Cryptography.CspParameters();
   cp.Flags = System.Security.Cryptography.CspProviderFlags.UseMachineKeyStore;
   cp.KeyContainerName = "RobinsKeys";


   // Create a new instance of RSACryptoServiceProvider that accesses
   // the key container MyKeyContainerName.
   // If it is not already there, it will create a new one, which is exportable.
   var myRSA = new System.Security.Cryptography.RSACryptoServiceProvider(cp);
    
   // print it on console
   Console.WriteLine($"=== Container: {cp.KeyContainerName} ===");
   Console.WriteLine(myRSA.ToXmlString(true).Replace("><", ">\n<")); 
}

可以在這里更詳細地閱讀。 提供的鏈接還顯示了如何

  • 生成並保存密鑰對
  • 從容器中獲取密鑰
  • 從容器中刪除密鑰

獲取容器列表可能很有用,這將在單獨的問題中討論

您可以在這篇 Microsoft 文章中找到有關關鍵容器的更多信息,以及可用的aspnet_regiis參數。

一旦創建了 RSA 容器,IIS 就可以使用它。 了解用戶級機器級密鑰容器之間的區別很重要,這在本文檔中進行了描述


如果討論中缺少任何內容,請告訴我,我會更新此答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM