An Error has occurred while encrypting connection string

0

I am trying to decrypt connection string in my app.config using customized Config Protection Provider named "MyConfigProtectionProvider". I don't have code for this Customized Config Provider as it is developed a long back, I have only DLL file. We changed our DB Username and Password. I want to decrypt my connection string in Web.Config using this customized Config Protection Provider and change password to new password and encrypt it again

I have only app.config. So I renamed it to "web.config". Then I added CODE I as shown below in my web.config. I store key in my c drive and I need to give Key file path. "MyConfigProtectionProvider" will encrypt it using this key.

We already have encrypted connection string as shown in CODE II. So I tried decrypting it using following command in cmd as Admin

aspnet_regiis.exe -pdf "connectionStrings" "C:\Users\Tejas\Documents\Email_Notification\LatestBatchJobStatus\BatchJobStatus\Batch_Job_Status"

  -prov "MyConfigProtectionProvider"

CODE I:

<configProtectedData defaultProvider="MyProtectionConfigurationProvider">
<providers><add name="MyProtectionConfigurationProvider" type="MyProtectionConfigurationProvider,&#xD;&#xA;MyProtectionConfigurationProvider, Version=1.0.0.0,Culture=neutral,&#xD;&#xA;PublicKeyToken=#############, processorArchitecture=MSIL"keyFilePath="C:\Users\Tejas\Documents\Email_Notification\LatestBatchJobStatus\BatchJobStatus\Batch_Job_Status\ConnectionKey\ConnectionKey.txt" /></providers></configProtectedData>

CODE II:

<connectionStrings configProtectionProvider="MyProtectionConfigurationProvider">  <EncryptedData>7Zqa4I623WR..................</EncryptedData></connectionStrings>

I am getting following error "An error has occurred: 0X80070002 The System cannot find the file specified".

What is the mistake I made?

asp.net-mvc-3
encryption
web-config
app-config
aspnet-regiis.exe
asked on Stack Overflow Apr 16, 2019 by Tejas P • edited Apr 16, 2019 by Ramprasath Selvam

1 Answer

0

It sounds like it can't find the configuration file. when decrypting, see if you can catch that FileNotFound exception to see more details about it:

 private static string GetSecureConnectionString(string connStringName)
        {
            try
            {
                Log.Debug("Starting Decryption of ConnectionString Named " + connStringName);
                var sEncrypted = ConfigurationManager.ConnectionStrings[connStringName].ConnectionString;
                Log.Debug("sEncrypted = " + sEncrypted);
                _connectString = Encryption.Decrypt(sEncrypted);
                Log.Debug("Did It Work? = " + _connectString);
                return _connectString;

            }
            catch (System.IO.FileNotFoundException ex)
            {
                Log.Debug("ERROR - Unable to locate the configuration file [" + ex.FileName.ToString() + "]");
                throw;
            }
            catch (ConfigurationErrorsException ex)
            {
                Log.Debug("ERROR - Error retrieving ConnectionString", ex);
                throw;
            }
            catch (Exception ex)
            {
                Log.Debug("ERROR - Error Decrypting ConnectionString", ex);
                throw new ApplicationException("ERROR: DataAccess:GetSecureConnectionString Failed. See ErrorLog for details.");
            }
        }
answered on Stack Overflow Aug 13, 2019 by (unknown user)

User contributions licensed under CC BY-SA 3.0