﻿# 對 ASP.NET 應用程式中 web.config 敏感資料加解密最簡單的方式

### 加密 web.config 的連接字串 (.NET Framework 2.0)

```batch
cd c:\inetpub\wwwroot   (你的應用程式目錄)
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pef connectionStrings . -prov DataProtectionConfigurationProvider
```

### 解密 web.config 的連接字串 (.NET Framework 2.0)

```batch
cd c:\inetpub\wwwroot   (你的應用程式目錄)
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pdf connectionStrings .
```

### 加密/解密 web.config 連接字串 (.NET Framework 4.0+)

```powershell
C:\windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -pef connectionStrings C:\inetpub\wwwroot -prov DataProtectionConfigurationProvider
```

```powershell
C:\windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -pdf connectionStrings C:\inetpub\wwwroot
```

### 加密/解密 web.config 應用程式設定 (.NET Framework 4.0+)

```powershell
C:\windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -pef appSettings C:\inetpub\wwwroot -prov DataProtectionConfigurationProvider
```

```powershell
C:\windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -pdf appSettings C:\inetpub\wwwroot
```

### 相關連結

-   [Encrypting Configuration Information Using Protected Configuration](https://learn.microsoft.com/en-us/previous-versions/aspnet/53tyfkaw\(v=vs.100\))
    -   [Overview of Protected Configuration](https://learn.microsoft.com/en-us/previous-versions/aspnet/hh8x3tas%28v%3dvs.100%29)
    -   [Specifying a Protected Configuration Provider](https://learn.microsoft.com/en-us/previous-versions/aspnet/68ze1hb2%28v%3dvs.100%29)
    -   [Encrypting and Decrypting Configuration Sections](https://learn.microsoft.com/en-us/previous-versions/aspnet/zhhddkxy%28v%3dvs.100%29)
    -   [Importing and Exporting Protected Configuration RSA Key Containers](https://learn.microsoft.com/en-us/previous-versions/aspnet/yxw286t2%28v%3dvs.100%29)
    -   [Understanding Machine-Level and User-Level RSA Key Containers](https://learn.microsoft.com/en-us/previous-versions/aspnet/f5cs0acs%28v%3dvs.100%29)
    -   [Walkthrough: Encrypting Configuration Information Using Protected Configuration](https://learn.microsoft.com/en-us/previous-versions/aspnet/dtkwfdky%28v%3dvs.100%29)
    -   [Walkthrough: Creating and Exporting an RSA Key Container](https://learn.microsoft.com/en-us/previous-versions/aspnet/2w117ede%28v%3dvs.100%29)
    -   [Implementing a Protected Configuration Provider](https://learn.microsoft.com/en-us/previous-versions/aspnet/wfc2t3az%28v%3dvs.100%29)
-   [如何對組態區段加密](http://renjin.blogspot.com/2008/07/how-to-encrypt-configuration-sections.html)
-   [Securing ASP.NET Web Sites](https://learn.microsoft.com/en-us/previous-versions/aspnet/91f66yxt%28v%3dvs.100%29)
