gpt4 book ai didi

c# - 解密/加密后 user.config 中缺少条目

转载 作者:行者123 更新时间:2023-12-04 17:07:50 24 4
gpt4 key购买 nike

我喜欢将用户名和密码存储到我的 C# .net5 程序中的 user.config。我不想直接存储密码,所以我决定解密 userSettings 部分。解密后部分文件丢失。

原始用户配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System.Configuration.ConfigurationManager, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" >
<section name="FileUploader.Properties.Settings" type="System.Configuration.ClientSettingsSection, System.Configuration.ConfigurationManager, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" allowLocation="true" allowDefinition="Everywhere" allowExeDefinition="MachineToLocalUser" overrideModeDefault="Allow" restartOnExternalChanges="true" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<FileUploader.Properties.Settings>
<setting name="UserName" serializeAs="String">
<value>user</value>
</setting>
<setting name="Password" serializeAs="String">
<value>xyz</value>
</setting>
</FileUploader.Properties.Settings>
</userSettings>
</configuration>

加密后:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System.Configuration.ConfigurationManager, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" >
<section name="FileUploader.Properties.Settings" type="System.Configuration.ClientSettingsSection, System.Configuration.ConfigurationManager, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" allowLocation="true" allowDefinition="Everywhere" allowExeDefinition="MachineToLocalUser" overrideModeDefault="Allow" restartOnExternalChanges="true" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<FileUploader.Properties.Settings configProtectionProvider="DataProtectionConfigurationProvider">
<EncryptedData>
<CipherData>
<CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAA4LmAfRMCHUeURwT1gTPd8AQAAAACAAAAAAAQZgAAAAEAACAAAADnR+uFolwsH4gkWI6vd46HNUxBv7ZfrQJmHe0q/zz8jAAAAAAOgAAAAAIAACAAAABlvfcY6M7JdG2dKIXO7cUUO9ui3Nou40CjMolKyJW7zgACAAAL5H+KJFPGoyugYYFDkZsV7GjXoaO+r8/z01/OiqVYXw80dCUIKje2g0tgizNrB/rAUmSg/uDPBx1yj3TAJ5aXf+fRxJaspf1jjr+QIaSJShnpy1sEjjpbUUn9+BVCYSJH8d4Ysj2JP1wvM5mgFBlFUcCLhHO+FputK3XpihAZCXeOddjNw2xYIQu4pmenLBiW6+fRUpINmNan/exQvHOWfwXuoZvqUli1hv+29qTuh/eJuPJQoyQSKrZ1A6Ie8aG2dsgQDvnVFEkt8cChkTn/TIc2Dk1uSDrsIYj/Ah8g1T5bE3PvWAYv67vtEfKbwBrqig+3HOSltZayVWyd2Y7iDS15Qk763ipiaBM64zhs/g4koQWpH1kAkfqgW3ibYAJEsk1a/K0Dd1Q2muxo0fsk1DfNYJIhFS8eAIuABlF6NAF5AT5hYyIOWVGjaquEP/aqzepCjEwkoLgD003qMISK7W6EAugSbWCRTwcMcKxZD3tHTmNLzZk+g8C7XpaWOk0xyODi5+mVXI8zg7NIGYo34JPado8l0p0Qd0gx15PwtZNHj0k9o6rieTgWjJEqf52ng70DNySsSX3jfyW91ArslcMLqO1qpbSFuIt0LeIswSxoR/etNM+GoGUjRW7t1OKGLcV5Wi8k0QdheXhxo6Hvq2/nv9iFYJxqgwoN6v1N0kAAAAACcCtrS9KBGCutdHdYddlimN5cPb8+X/snuKEgu63fi4TE2VSei4R0WqjeC22JIFn3HqPIzWb9Kd9pPDJCWQz7</CipherValue>
</CipherData>
</EncryptedData>
</FileUploader.Properties.Settings>
</userSettings>
</configuration>

解密后:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System.Configuration.ConfigurationManager, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" >
<section name="FileUploader.Properties.Settings" type="System.Configuration.ClientSettingsSection, System.Configuration.ConfigurationManager, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" allowLocation="true" allowDefinition="Everywhere" allowExeDefinition="MachineToLocalUser" overrideModeDefault="Allow" restartOnExternalChanges="true" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<FileUploader.Properties.Settings>
<setting name="" serializeAs="String">
<value>xyz</value>
</setting>
</FileUploader.Properties.Settings>
</userSettings>
</configuration>

只有最后一个条目,缺少条目的名称。如果我切换原始文件中的顺序,我会得到“用户”作为值。

解密/加密的代码(或多或少是 msdn 示例的副本):

private void ProtectSettings()
{
// Get the application configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.PerUserRoamingAndLocal);

// Define the Rsa provider name.
string provider = "DataProtectionConfigurationProvider";

string section = "userSettings/" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".Properties.Settings";

// Get the section to protect.
ConfigurationSection connStrings = config.GetSection(section);

if (connStrings != null)
{
if (!connStrings.SectionInformation.IsProtected)
{
if (!connStrings.ElementInformation.IsLocked)
{
// Protect the section.
connStrings.SectionInformation.ProtectSection(provider);

connStrings.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);

Debug.WriteLine("Section {0} is now protected by {1}",
connStrings.SectionInformation.Name,
connStrings.SectionInformation.ProtectionProvider.Name);
}
else
Debug.WriteLine(
"Can't protect, section {0} is locked",
connStrings.SectionInformation.Name);
}
else
Debug.WriteLine(
"Section {0} is already protected by {1}",
connStrings.SectionInformation.Name,
connStrings.SectionInformation.ProtectionProvider.Name);
}
else
Debug.WriteLine("Can't get the section {0}", section);
}

private static void UnProtectSettings()
{
// Get the application configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.PerUserRoamingAndLocal);

string section = "userSettings/" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".Properties.Settings";

// Get the section to unprotect.
ConfigurationSection connStrings = config.GetSection(section);

if (connStrings != null)
{
if (connStrings.SectionInformation.IsProtected)
{
if (!connStrings.ElementInformation.IsLocked)
{
// Unprotect the section.
connStrings.SectionInformation.UnprotectSection();

connStrings.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);

Debug.WriteLine("Section {0} is now unprotected.",
connStrings.SectionInformation.Name);
}
else
Debug.WriteLine(
"Can't unprotect, section {0} is locked",
connStrings.SectionInformation.Name);
}
else
Debug.WriteLine(
"Section {0} is already unprotected.",
connStrings.SectionInformation.Name);
}
else
Debug.WriteLine("Can't get the section {0}", section);
}

最佳答案

在尝试了一段时间后,我发现问题出在 ConfigurationSaveMode.Full 选项上。

ProtectSettings()UnProtectSettings() 中,而不是

connStrings.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);

只需使用:

config.Save();

我不知道为什么该选项会导致不良行为。

关于c# - 解密/加密后 user.config 中缺少条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70169794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com