0

In my .net web application I encrypted the of the web.config file.

But now I need to read this encrypted connection String from an external winforms application and display the result in a text box . The text box text would look something like this :

 <connectionStrings configProtectionProvider="CustomEncryptProvider">
        <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
            xmlns="http://www.w3.org/2001/04/xmlenc#">
            <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
            <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
                <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
                    <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
                    <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
                        <KeyName>Rsa Key</KeyName>
                    </KeyInfo>
                    <CipherData>
                        <CipherValue>Smbfcf3dHbuYZ34hLWtATU8fBqNL/CvPk24tLj9gLOizLzV88den52yhtYQ5AwXe2vVZP/GKRsWB+rcX/6ufBkz75HyVOnJHTCgLQ+JcsRX/9Td5ZzWJrEq1JBdpFzBsS9aLGLMREIILPedmFxO5+0GLIaBPzZ9/BhNcN8GXa+k=</CipherValue>
                    </CipherData>
                </EncryptedKey>
            </KeyInfo>
            <CipherData>
                <CipherValue>raoQqDzlXmMCy+3VliV6oyMoQzgIapSmBKw666WbUjLgurCh4aS+pwSMW3wULOpi+jh8BdDE/aPwvhDw9kTuComyHBsEB4xMtRFaBY1NSyrwx7dnP44x4NS+LowJ1EQiN2fAZqWDDVAljRIlq3DtZhC9YkYl4H1rEjQVvljD0pus1O8ftiqKy/yma1/rqzI+F/87GrFR1ZM8cS/ujXagtfzqME4iVdTgl/eyEPkrd5f6SGwlieeC0zJ2ErV9zIr+Af2Sc6mk2hz7/+t2x3kAzDzHU2PFfBqiLSP6o/0XAdRl43Q/Jwr72552mus7n5urlzvyND0KXKzk4Gg4bVYuo8sSQvphbFuLgHIxq+6ShDdCc9wfMzsBmGU4ayYbn/a4rI8lB5y6GzK0kQvnH0qtWQ==</CipherValue>
            </CipherData>
        </EncryptedData>
    </connectionStrings>

How to achieve the same ? Please note I only have the physical path to the web.config file .

7
  • 1
    Why are you encrypting the whole web.config? Commented Sep 2, 2015 at 18:57
  • No I am not. I am only encrypting connectionStrings . And I want to display the same . Commented Sep 2, 2015 at 19:04
  • Is your question about how to encrypt/decrypt or is it about how to locate a substring within a larger string? Commented Sep 2, 2015 at 19:04
  • I have encrypted the connectionString section already and saved the encrypted config . I want to display the encrypted connectionString back from an external winforms apllication. Commented Sep 2, 2015 at 19:07
  • OK, so then are you asking how to decrypt the string that you've already encrypted or are you asking how to find it within the web.config file from the external winforms application? Or both? Commented Sep 2, 2015 at 19:11

1 Answer 1

1

You can load a config file using System.Configuration.ConfigurationManager like this:

var config = ConfigurationManager.OpenExeConfiguration(/* path to config file */);

The connection strings will automatically be decrypted, and will be available in the ConnectionStrings property of the Configuration object. In your case, the connection string is called "LocalSqlServer":

Console.WriteLine(config.ConnectionStrings.ConnectionStrings["LocalSqlServer"]);
> data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

EDIT

If, as you've indicated, you actually want the whole content of the connectionStrings xml element, you can do this with an XmlReader:

using (var reader = XmlReader.Create(/* path to config file */))
{
    if (reader.ReadToDescendant("connectionStrings"))
        Console.WriteLine(reader.ReadOuterXml());
}
 > <connectionStrings configProtectionProvider="CustomEncryptProvider">
        <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" etc...
Sign up to request clarification or add additional context in comments.

6 Comments

but I want to show the encrypted connection string not the auto - decrypted one.
Do you mean you want to show "raoQqDzlXmMCy+3VliV6o..."?
The whole content of the encrypted connection String section
Thanks that helped a lot . But would it be possible to update connection String using xmlWriter also ? Just a wild guess though :)
You'd be best off using an XmlDocument if you want to change the content, but yes, it can be done.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.