3

Is there any way I can achieve this encrytption and decryption of connection string from my C# code.

like we do

aspnet_regiis -pe "connectionStrings"

aspnet_regiis -pd "connectionStrings"

Thanks

BreakHead
  • 10,480
  • 36
  • 112
  • 165
  • 2
    Which C# code *where*? Ultimately, it comes down to ***what attack vector are you trying to protect***. You need to state clearly the attack vector to know whether encryption would actually add any security, vs just cause inconvenience. For example, the attack vector on aspnet_regiis is that it protects the files from inspection outside of your application, by somebody with a copy of the files. It doesn't protect the data from anything that has access to the running app itself. – Marc Gravell Jul 29 '11 at 07:51
  • The reason is if connection string have any password it not be shown in a clear text – BreakHead Jul 29 '11 at 07:57
  • @Christian K, how can I accept an answer which is not useful for me? – BreakHead Jul 29 '11 at 08:10
  • @BreakHead, you probably shouldn't. However - and I don't really bother to look through all your open questions - keep in mind sometimes the right answer is the one that shows that the question was not good/answerable to start with - may that be satisfying for the questioner or not. – Christian.K Jul 29 '11 at 08:26
  • Duplicates: http://stackoverflow.com/search?q=[c%23]encrypt+web.config – oleksii Jul 29 '11 at 10:13
  • How are you currently approaching your design and using the connection strings in your application? Are they in the web.config or in the code behind? Are they being dynamically determined based upon operating environment? Do you have a single class setting the values? Are you using a Dataset that has it hardcoded? I could go on, but as @Marc-Gravell said, we need more information to help you. – McArthey Aug 03 '11 at 02:05

1 Answers1

0
static public void ProtectSection()
{
    // Get the current configuration file.
    System.Configuration.Configuration config =
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    // Get the section.
    UrlsSection section = (UrlsSection)config.GetSection("connectionStrings");

    // Protect (encrypt)the section.
    section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");

    // Save the encrypted section.
    section.SectionInformation.ForceSave = true;

    config.Save(ConfigurationSaveMode.Full);

    // Display decrypted configuration 
    // section. Note, the system
    // uses the Rsa provider to decrypt
    // the section transparently.
    string sectionXml =
    section.SectionInformation.GetRawXml();

    Console.WriteLine("Decrypted section:");
    Console.WriteLine(sectionXml);
}
KnightHawk
  • 336
  • 3
  • 5