Decrypt using App.config

Topics: Cryptography Application Block, Data Access Application Block
Nov 28, 2008 at 6:30 PM
How do i decrypt (by code, not by Enterprise Library Configuration tool) a data access application block (.config file) using a .key or a App.config file?
The biggest problem is, I HAVE to use the 1.1 version.

I appreciate any help, tks.
Nov 28, 2008 at 11:37 PM
Edited Dec 1, 2008 at 11:55 AM
After debug the Enterprise Library Configuration Tool, I found a simple way to do it:

Fill a RuntimeConfigurationView with the App.config file, than, use a XmlDocument to load the cryptographed dataconfig file, using that RuntimeConfigurationView and a ConfigurationProtector.


if (ofDialog.ShowDialog() == DialogResult.OK)
                String filePath = ofDialog.FileName;
                tbAppPath.Text = filePath;

                RuntimeConfigurationView rtConfig = new RuntimeConfigurationView(new ConfigurationContext(filePath));

                XmlFileStorageProviderData xspd = (XmlFileStorageProviderData) rtConfig.GetStorageProviderData("dataConfiguration");

                XmlDocument doc = new XmlDocument();
                using(FileStream fs = new FileStream(xspd.Path, FileMode.Open, FileAccess.Read))
                        using (ConfigurationProtector protector = rtConfig.GetConfigurationProtector("!dataConfiguration name!"))
                            byte[] fileBytes = new byte[fs.Length];
                            fs.Read(fileBytes, 0, fileBytes.Length);
                            if (fileBytes.Length > 0)
                                fileBytes = protector.Decrypt(fileBytes);
                                doc.Load(new MemoryStream(fileBytes)); //This is the  decrypted Xml doc
                    catch (Exception ex)
Dec 2, 2008 at 8:06 AM

Thanks for the Information.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.