.Net App.Configの暗号化

Pocket

作った.Netのバッチプログラムを開発PCから本番機へデプロイして、ユーザさんは
データベースのパスワードやメールのアカウントなどは暗号化してほしいとの要望があったため、
調査を行いました。Web上にていろんなところで記事もありましたが、直接使えるサンプルはありませんでした。
ここでそのまま使えるサンプルを公開します。どうぞご参考ください。

下記のソースはApp.configに対して暗号化し、そのままで、修正する必要はありません(Web.configならファイル名を変更してください)。
App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DatabaseServer" value="127.0.0.1"></add>
<add key="DatabaseName" value="master"></add>
<add key="DatabaseUserId" value="sa"></add>

</appSettings>
<connectionStrings>
<add name="MailPassword" connectionString="2135643y6"></add>
<add name="DatabasePassword" connectionString="asdgfqtre35yt"></add>
</connectionStrings>
</configuration>

1.開発マシン
——————————————————
1). App.Configファイル暗号化/暗号化解除用のキーを作成 (GenerateExportKeyFile.bat)

set currentPath=%~dp0
IF %currentPath:~-1%==\ SET currentPath=%currentPath:~0,-1%

set KeyName=MySecurityKey
set ProviderName=NeclSecurityProvider
set WebConfigFileFolder=%currentPath%
set EncryptSection=connectionStrings
set KeyXmlOutputPath=%currentPath%

for /F “usebackq tokens=1,2 delims==” %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if ‘.%%i.’==’.LocalDateTime.’ set ldt=%%j
set ldt=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%_%ldt:~82%%ldt:~102%%ldt:~12,2%

set timestamp=%ldt%

CD C:\Windows\Microsoft.NET\Framework\v2.0.50727
C:

aspnet_regiis -pc “%KeyName%” -exp
aspnet_regiis -px “%KeyName%” “%KeyXmlOutputPath%\%KeyName%.xml” -pri

このバッチを実施すれば、MySecurityKey.xmlファイルが出力されます。
このファイルは運用マシンや他の開発マシンにインポートすれば、
そのPCでも暗号化したApp.Configファイルが使えます。

2). App.Configファイルを暗号化 (GenerateExportKeyFile.bat)
まず、下記セクションをApp.Configファイルに追加します。
<configProtectedData>
<providers>
<add keyContainerName=”MySecurityKey”
useMachineContainer=”true”
description=”Uses RsaCryptoServiceProvider to encrypt and decrypt”
name=”NeclSecurityProvider” type=”System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” />
</providers>
</configProtectedData>

次は、以下のバッチを実行します。

set currentPath=%~dp0
IF %currentPath:~-1%==\ SET currentPath=%currentPath:~0,-1%

set KeyName=NeclSecurityKey
set ProviderName=NeclSecurityProvider
set WebConfigFileFolder=%currentPath%
set EncryptSection=connectionStrings
set KeyXmlOutputPath=%currentPath%

for /F “usebackq tokens=1,2 delims==” %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if ‘.%%i.’==’.LocalDateTime.’ set ldt=%%j
set ldt=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%_%ldt:~82%%ldt:~102%%ldt:~12,2%

set timestamp=%ldt%

Copy %currentPath%\App.Config %currentPath%\App.Config.Backup.Dev
Move %currentPath%\App.Config %currentPath%\Web.Config

CD C:\Windows\Microsoft.NET\Framework\v2.0.50727
C:

aspnet_regiis -pef “%EncryptSection%” “%WebConfigFileFolder%” -prov %ProviderName%

Move %currentPath%\Web.Config %currentPath%\App.Config

このバッチを実施すると、暗号化したApp.Configが生成されます。
connectionStrings Sectionは以下のように変更されます。

<connectionStrings configProtectionProvider=”MySecurityProvider”>
<EncryptedData Type=”http://www.w3.org/2001/04/xmlenc#Element”
xmlns=”http://www.w3.org/2001/04/xmlenc#”>
<EncryptionMethod Algorithm=”http://www.w3.org/2001/04/xmlenc#tripledes-cbc” />
<KeyInfo xmlns=”http://www.w3.org/2000/09/xmldsig#”>
<EncryptedKey xmlns=”http://www.w3.org/2001/04/xmlenc#”>
<EncryptionMethod Algorithm=”http://www.w3.org/2001/04/xmlenc#rsa-1_5″ />
<KeyInfo xmlns=”http://www.w3.org/2000/09/xmldsig#”>
<KeyName>Rsa Key</KeyName>
</KeyInfo>
<CipherData>
<CipherValue>YdPPtC9zXvlBH5APGvhAqzIY5w0LtyX9H4zDuMb0MnwTipLr+0JcJ2ZfnHbN5/FAaP7TzLTvl0oxcJY65DSGemyjyJ4qZgpOuC0mk3hBa4+8ouPXTH6kR8uLpJwA33ue0genvc11+oQ2SqCpDPdD4eUI5+UR5kfaDeXHmN3atOQ=</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>paGnSbomqKH3eSbtEtoUkgOBR5YuqEaRM7IeWereVNNyk4r+mEAw4ndES1vv8SQDRL/wZSIxK/GjuIZPjMudl2WiLAhoP4+ToYn9i0C2oy3HgAHNKtggqnl0xn9JK52ZLLYDGYSr2Iw1ispwHymOpAzKUnqftlW6xKOHSEW9crOpm4k3awV00FFeA+kAq378XaR++DCrn634hn/LmNJFpwnvVckDkYr4D5rvrprsrSdcrjpe+m1vFA==</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
——————————————————

2. 運用マシン
——————————————————
1). キーをデプロイ DeployMyKey.bat

set currentPath=%~dp0
IF %currentPath:~-1%==\ SET currentPath=%currentPath:~0,-1%

echo %currentPath%

set KeyName=MySecurityKey
set KeyXmlPath=%currentPath%

CD C:\Windows\Microsoft.NET\Framework\v2.0.50727
C:

aspnet_regiis -pi “%KeyName%” “%KeyXmlPath%\%KeyName%.xml”
aspnet_regiis -pa “%KeyName%” “NT Authority\Network Service”
——————————————————

Exeファイルと暗号化したApp.Configファイルを運用サーバにコピーして、
確認しましょう。

3. Decrypt(DecryptConfig.bat)

set currentPath=%~dp0
IF %currentPath:~-1%==\ SET currentPath=%currentPath:~0,-1%

set KeyName=MySecurityKey
set ProviderName=MySecurityProvider
set WebConfigFileFolder=%currentPath%
set EncryptSection=connectionStrings
set KeyXmlOutputPath=%currentPath%

for /F “usebackq tokens=1,2 delims==” %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if ‘.%%i.’==’.LocalDateTime.’ set ldt=%%j
set ldt=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%_%ldt:~82%%ldt:~102%%ldt:~12,2%

set timestamp=%ldt%

Copy %currentPath%\App.Config %currentPath%\App.Config.Backup.Prod
Move %currentPath%\App.Config %currentPath%\Web.Config

CD C:\Windows\Microsoft.NET\Framework\v2.0.50727
C:

aspnet_regiis -pdf “%EncryptSection%” “%WebConfigFileFolder%”
Move %currentPath%\Web.Config %currentPath%\App.Config