Failed to decrypt using provider 'DataProtectionConfigurationProvider' in app.config
<p>I used following method to encrypt <code>connectionstrings</code> section of my <code>app.config</code> in my <code>WinForms</code> project(I'm using Code First EF in my project):</p>
<pre><code>public static void EncryptConfig(string exeConfigName)
{
    var config = ConfigurationManager.OpenExeConfiguration(exeConfigName);
    var section = config.GetSection(&quot;connectionStrings&quot;) as ConnectionStringsSection;
    if (section != null || !section.SectionInformation.IsProtected)
    {
       section.SectionInformation.ProtectSection(&quot;DataProtectionConfigurationProvider&quot;);
       config.Save();
    }
}
</code></pre>
<p>and I use following method to decrypt the <code>connectionstrings</code> section, too:</p>
<pre><code>public static void DecryptConfig(string exeConfigName)
{
    var config = ConfigurationManager.OpenExeConfiguration(exeConfigName);
    var section = config.GetSection(&quot;connectionStrings&quot;) as ConnectionStringsSection;
    if (section != null &amp;&amp; section.SectionInformation.IsProtected)
        section.SectionInformation.UnprotectSection();
}
</code></pre>
<p>this method works in my machine, but when I deploy my application to another machine I get following exception:</p>
<blockquote>
<p>System.Configuration.ConfigurationErrorsException: Failed to decrypt using provider 'DataProtectionConfigurationProvider'. Error message from the provider: Key not valid for use in specified state. (Exception from HRESULT: 0x8009000B) (D:\l4test\Level4UI.exe.config line 82) ---&gt; System.Runtime.InteropServices.COMException: Key not valid for use in specified state. (Exception from HRESULT: 0x8009000B)</p>
<p>at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)</p>
<p>at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)</p>
<p>at System.Configuration.DpapiProtectedConfigurationProvider.DecryptText(String encText)</p>
<p>at System.Configuration.DpapiProtectedConfigurationProvider.Decrypt(XmlNode encryptedNode)</p>
<p>at System.Configuration.ProtectedConfigurationSection.DecryptSection(String encryptedXml, ProtectedConfigurationProvider provider)</p>
<p>at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.DecryptSection(String encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)</p>
<p>at System.Configuration.Internal.DelegatingConfigHost.DecryptSection(String encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)</p>
<p>at System.Configuration.Internal.DelegatingConfigHost.DecryptSection(String encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)</p>
<p>at System.Configuration.BaseConfigurationRecord.CallHostDecryptSection(String encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfig)</p>
<p>at System.Configuration.BaseConfigurationRecord.DecryptConfigSection(ConfigXmlReader reader, ProtectedConfigurationProvider protectionProvider)</p>
<p>--- End of inner exception stack trace ---</p>
<p>at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)</p>
<p>at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object&amp; result, Object&amp; resultRuntimeObject)</p>
<p>at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object&amp; result, Object&amp; resultRuntimeObject)</p>
<p>at System.Configuration.Configuration.GetSection(String sectionName)</p>
<p>at IASCo.Infrastructure.Common.Utilities.Configuration.ConfigurationEncryption.DecryptConfig(string exeConfigName)</p>
</blockquote>
<p>In <a href="https://stackoverflow.com/questions/788459/error-after-encryptingweb-config">this thread</a>, <a href="https://stackoverflow.com/users/91814/jeremy">Jeremy</a> said:</p>
<blockquote>
<p>You need to publish with the section decrypted. The key that is used to encrypt/decrypt is machine specific.</p>
</blockquote>
<p>My app will be installed on a network share and run from there but there will be more than one person who may access the app from their work stations, How can I specify a single key to decrypt the <code>connectionString</code> section that will work from all the machines used to access the app.</p>
<p>I'm looking for a way to do this job(encrypt in my machine and decrypt in user's machines) using c#.</p>