【发布时间】:2022-01-13 12:36:53
【问题描述】:
我想使用 DpapiProtectedConfigurationProvider 生成密码文本。 我在互联网上看到的所有代码都是在 app.config 中执行的。我知道这是最初构建的原因。但我有不同的用法。我有界面,用户必须在文本框中输入文本,然后单击按钮,我需要使用 DpapiProtectedConfigurationProvider 生成 cpher 文本。如何做到这一点?
目前我正在使用以下代码在 app.config 中生成它。但这不是我想要的
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//Configuration config = ConfigurationManager.OpenExeConfiguration(exefilePath);
ConfigurationSection section = config.GetSection(sectionKey);
if (section != null)
{
if (section.ElementInformation.IsLocked)
{
Console.WriteLine("Section: {0} is locked", sectionKey);
}
else
{
if (!section.SectionInformation.IsProtected)
{
//%windir%\system32\Microsoft\Protect\S-1-5-18
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
section.SectionInformation.ForceSave = true;
Console.WriteLine("Encrypting: {0} {1}", section.SectionInformation.Name, section.SectionInformation.SectionName);
}
else
{ // display values for current config application name value pairs
//
section.SectionInformation.UnprotectSection();
section.SectionInformation.ForceSave = true;
Console.WriteLine("Decrypting: {0} {1}", section.SectionInformation.Name, section.SectionInformation.SectionName);
}
}
}
else
{
Console.WriteLine("Section: {0} is null", sectionKey);
}
//
config.Save(ConfigurationSaveMode.Full);
Console.WriteLine("Saving file: {0}", config.FilePath);
我该怎么做?
【问题讨论】:
标签: c# encryption