I have someone's public key as a string:
<RSAKeyValue>
<Modulus>publickeyhere</Modulus>
<Exponent>AAAA</Exponent>
</RSAKeyValue>
I also have an app.config file and I'm trying to encrypt the appSettings section of it using the public key. I'm doing it like this
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;
}
}
How can I tell it that to do the encryption, it should use the public key and not one from the current machine it's running on?