首页 > 解决方案 > 如何使用提供的 RSA 公钥加密 App.config 文件的一部分

问题描述

我有某人的公钥作为字符串:

<RSAKeyValue>
  <Modulus>publickeyhere</Modulus>
  <Exponent>AAAA</Exponent>
</RSAKeyValue>

我还有一个 app.config 文件,我正在尝试使用公钥加密它的 appSettings 部分。我正在这样做

var publicKeyXml = @"<RSAKeyValue><Modulus>publickeyhere</Modulus><Exponent>AAAA</Exponent></RSAKeyValue>";

var map = new ExeConfigurationFileMap
{
    ExeConfigFilename = "app.config"
};

var config = ConfigurationManager
    .OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

using (var rsa = new RSACryptoServiceProvider())
{
    try
    {
        rsa.FromXmlString(publicKeyXml);

        // Now use the public key to encrypt how?
        config.AppSettings.SectionInformation
            .ProtectSection("RSAProtectedConfigurationProvider");
        config.SaveAs($"app.encrypted.config");
    }
    finally
    {
        rsa.PersistKeyInCsp = false;
    }
}

我怎么能告诉它要进行加密,它应该使用公钥而不是它正在运行的当前机器上的公钥?

标签: c#.netrsaapp-config

解决方案


推荐阅读