27

Duplicate of Encrypting config files for deployment .NET and Encrypting config files for deployment


What is the best approach and tools for encrypting information in web.config file?

Community
  • 1
  • 1
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188

6 Answers6

19

Here are the commands to encrypt web.config file without any programming...

For encryption

aspnet_regiis -pef "Section" "Path exluding web.config"

For Decryption

aspnet_regiis -pdf "Section" "Path exluding web.config"

From this commands you can encrypt or decrypt all the section.

Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
Jalpesh Vadgama
  • 13,653
  • 19
  • 72
  • 94
  • 1
    Don't put a trailing "\" on the end of the path either. It didn't work for me until I removed that. – user2444499 Nov 27 '13 at 01:04
  • 1
    And make sure to run with admin privileges. – marsze Mar 14 '17 at 07:57
  • Where to run this command, when I start cmd as administrator and run command it says `'aspnet_regiis' is not recognized as an internal or external command, operable program or batch file.` And when I go to the path `C:\Windows\Microsoft.NET\Framework\v4.0.30319` and open file `aspnet_regiis.exe` it auto closes. How can I run this command? – Irfan Y Oct 30 '19 at 06:28
  • You can find the aspnet_regiis.exe at `C:\Windows\Microsoft.NET\Framework64\v4.0.30319`. Execute the aspnet_regis.exe inside of the windows command prompt (cmd). You might use `"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe" -pdf "Section" "Path exluding web.config"` – Icad Aug 04 '21 at 13:01
10

I believe there are two ways of doing this:

using aspnet_regiis using DPAPI or RSA, or doing it programmatically.

The programmatic way can be handy, particularly if you also like to encrypt app.config.

From my experiences of using this, if you write a custom configuration section, you have install the DLL containing the classes for that section into the GAC. For a project I was working I basically scripted the following approach:

  • Copy config DLL to GAC.
  • Perform encryption.
  • Remove config DLL from GAC.

Chances are if you are just encrypting connection strings then this won't be a problem. You also need to be bear in mind whether you want to encrypt on a machine wide basis or to a specific user account- both options can be useful depending on your scenario. For simplicity I stuck to machine wide encryption. The links I have provided explain the merits of both approaches.

RichardOD
  • 28,883
  • 9
  • 61
  • 81
  • Yes @Richard making it machine wide makes more easy! – TheVillageIdiot Jul 03 '09 at 02:05
  • Hi RichardOD, my question is.. If doing it programmatically. When do I need to encrypt and decrypt the webconfig file? Check my post on this link --> http://stackoverflow.com/questions/25614737/encrypt-decrypt-web-config – Haminteu Sep 02 '14 at 02:08
  • 1
    What is the point of doing this? Anyone can easily decrypt the connection strings just as easily as we can encrypt it. – markthewizard1234 Aug 16 '16 at 09:42
  • 2
    @markthewizard1234 They keys used for encryption are machine-specific. So you would not be able to encrypt it unless you have access to the server. See https://www.codeproject.com/Tips/795135/Encrypt-ConnectionString-in-Web-Config (paragraph Questions & Answers) – marsze Mar 13 '17 at 11:40
  • @markthewizard1234: Anyone who has admin access to the server. – Tim Jan 28 '22 at 12:32
4

Have you tried this: http://weblogs.asp.net/scottgu/archive/2006/01/09/434893.aspx

Irwin
  • 12,551
  • 11
  • 67
  • 97
  • The blog mentions that IIS manager allows one to edit encrypted sections. But IIS 7 onwards this is not true. The runtime can decrypt the encrypted web.config but not the IIS manager – Ganesh R. Jul 02 '09 at 16:17
  • Thanks @irwin very nice resource for further studying the problem. – TheVillageIdiot Jul 03 '09 at 03:01
2

Use the aspnet_regiis tool in your framework directory:

                                        -- CONFIGURATION ENCRYPTION OPTIONS --

pe section            Encrypt the configuration section. Optional arguments:
                      [-prov provider] Use this provider to encrypt.
                      [-app virtual-path] Encrypt at this virtual path. Virtual path must begin with a forward slash.
                      If it is '/', then it refers to the root of the site. If -app is not specified, the root
                      web.config will be encrypted.
                      [-site site-name-or-ID] The site of the virtual path specified in -app. If not specified, the
                      default web site will be used.
                      [-location sub-path] Location sub path.
                      [-pkm] Encrypt/decrypt the machine.config instead of web.config.

pd section            Decrypt the configuration section. Optional arguments:
                      [-app virtual-path] Decrypt at this virtual path. Virtual path must begin with a forward slash.
                      If it is '/', then it refers to the root of the site. If -app is not specified, the root
                      web.config will be decrypted.
                      [-site site-name-or-ID] The site of the virtual path specified in -app. If not specified, the
                      default web site will be used.
                      [-location sub-path] Location sub path.
                      [-pkm] Encrypt/decrypt the machine.config instead of web.config.
x0n
  • 51,312
  • 7
  • 89
  • 111
2

You should start from here, easy to follow, well explained step-by-step guide in MSDN: How To: Encrypt Configuration Sections in ASP.NET 2.0 Using DPAPI

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
0

Use the aspnet_regiis.exe Command-Line Tool

You can also encrypt and decrypt sections in the Web.config file using the aspnet_regiis.exe command-line tool, which can be found in the %WINDOWSDIR%\Microsoft.Net\Framework\version directory.

Look here: https://web.archive.org/web/20211029043331/https://aspnet.4guysfromrolla.com/articles/021506-1.aspx.

jinsungy
  • 10,717
  • 24
  • 71
  • 79