locked
C# RSA Web Config Decryption Issue RRS feed

  • Question

  • User1186649752 posted

    I tried encrypting connection string of web config from an external C# application on my machine A . It was successful .

    I exported the key .

    The code snippet I tried looks like 

      private void encryptAndWrite(String FileName)
            {
                string strEntry = FileName.Replace("\\web.config", ""); // Let the user assign to this string, for example like "C:\Users\cnandy\Desktop\Test\Websites\AccountDeduplicationWeb"
    
                Process p = new Process()
                {
                    StartInfo = new ProcessStartInfo("cmd.exe")
                    {
                        RedirectStandardInput = true,
                        RedirectStandardOutput = true,
                        UseShellExecute = false,
                        CreateNoWindow = true
                    }
                };
    
                p.Start();
    
    
                p.StandardInput.WriteLine(@"cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319");
    
    
                p.StandardInput.WriteLine("aspnet_regiis.exe -pc \"NewEncryptKey\" -exp");
             
                p.StandardInput.WriteLine("aspnet_regiis.exe -pa \"NewEncryptKey\" \"NT AUTHORITY\\NETWORK SERVICE\"");
                p.StandardInput.WriteLine("aspnet_regiis -pef \"connectionStrings\" {0} -prov \"NewEncryptProvider\"",strEntry);
    
    
                p.StandardInput.WriteLine("aspnet_regiis -px \"NewEncryptKey\" {0} -pri", KeyFileName);
    
    
                p.StandardInput.WriteLine("exit");

    as it was a physical path I used -pdf option.

    Now made the installer of this application and exported the key and the encrypted web config file to some other machine .

    When I run the application in that other machine it flags "Failed to decrypt .. Key Container Not Found " error.

    Though In my application decrypt method I have 

     string strEntry = FileName.Replace("\\web.config", ""); // Let the user assign to this string, for example like "C:\Users\cnandy\Desktop\Test\Websites\AccountDeduplicationWeb"
    
                    Process p = new Process()
                    {
                        StartInfo = new ProcessStartInfo("cmd.exe")
                        {
                            RedirectStandardInput = true,
                            RedirectStandardOutput = true,
                            UseShellExecute = false,
                            CreateNoWindow = true
                        }
                    };
    
                    p.Start();
    
                    p.StandardInput.WriteLine(@"cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319");
                    p.StandardInput.WriteLine("aspnet_regiis -pi \"NewEncryptKey\" " + KeyFileName);
                    p.StandardInput.WriteLine("aspnet_regiis -pa \"NewEncryptKey\" \"NT AUTHORITY\\NETWORK SERVICE\"");
                    p.StandardInput.WriteLine("aspnet_regiis -pdf \"connectionStrings\" " + strEntry);
    
                    p.StandardInput.WriteLine("exit");

    But if I run the commands directly from command prompt as an administrator that issue gets resolved. But it says 

    Decrypting configuration section...
    Failed to decrypt using provider 'NewEncryptProvider'. Error message from the pr
    ovider: Not enough storage is available to process this command.
     (C:\Users\abalawan\Desktop\NewStuartCN\web.config line 35)
    
    Not enough storage is available to process this command.
    
    Failed!



    What all things am I missing here ? 
    I wrote down everything I tried . Please help .

    Thursday, September 10, 2015 1:37 AM

Answers

All replies