[英]How to encrypt / decrypt a configuration file section with RsaProtectedConfigurationProvider
在我的配置文件中,我有一些我想加密的敏感信息以提高安全性。
這是我的代碼(按預期工作):
class Program
{
static void Main(string[] args)
{
System.Configuration.ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = @"D:\Web_S\Prep\test\test.exe.config";
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
string userNameWithoutEncryption = configuration.AppSettings.Settings["username"].Value;
EncryptAppSettings("appSettings", configuration);
}
protected static void EncryptAppSettings(string section, Configuration configuration)
{
AppSettingsSection objAppsettings = (AppSettingsSection)configuration.GetSection(section);
objAppsettings.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
objAppsettings.SectionInformation.ForceSave = true;
configuration.Save(ConfigurationSaveMode.Modified);
}
}
.配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="username" value="a2zmenu"/>
<add key="password" value="password"/>
</appSettings>
</configuration>
加密后的.config 長這樣:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="customAppSettings" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<appSettings configProtectionProvider="RsaProtectedConfigurationProvider">
<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>09+Lm23xDWWnAZFOagh3NRwp5tzad+3oedvTgoeWqunQBiAfk9UGfGxriZg6snwwANUDzOANZ+wOFUb6qa0Atf
NgSd6b4FFSKTqzkfLlk+S9GtPSAVrRaLU9
/Q2Qu7oxoSbhW7NWtengJbEZrFm+GqlLlm08w8Np/y03DMExFeA=</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>qSYRXNEKhbwNodH60c7qoWeKZ2QKVQmizPXVGCgHVZPMQ4F+XDqlZa2OyIin0kEI3j8pCjNL097RlZClgdd
gPEd61AEw6DXJc43Z98obNFHmXfK9aS67qEtO6E
T+qCWQq2ZRbfK6xZ6jlfeink35/veUmoxAmDXrkwdrbQVKv98=</CipherValue>
</CipherData>
</EncryptedData>
</appSettings>
</configuration>
我有以下問題:讓諸如此類的信息安全嗎?
<EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
在.config?
難道不能用這些信息解密嗎? 文件加密后,你能確認我可以評論這行嗎:
EncryptAppSettings("appSettings", configuration);
當我嘗試在使用此行加密文件后獲取用戶名值時:
string userNameafterEncryption = configuration.AppSettings.Settings["username"].Value;
即使我的文件現在已加密,我也會得到解密值。 我不明白為什么...
謝謝您的幫助
首先,您需要了解加密如何以及從何種配置中真正保護您。 RsaProtectedConfigurationProvider
可以存儲在兩個地方用於實際加密的私鑰。 第一個是
C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys
這是存儲機器范圍密鑰的文件夾。 默認情況下,任何用戶都可以訪問此文件夾,但您需要升級(以管理員身份運行)才能讀取此文件夾中的文件(默認情況下再次)。
第二個可能的位置是
C:\Documents and Settings\[user name]\Application Data\Microsoft\Crypto\RSA
這是用戶級位置 - 只有特定用戶才能訪問它。
默認情況下, RsaProtectedConfigurationProvider
將使用計算機級位置,這由此提供程序的UseMachineContainer
屬性控制。 默認配置在機器級配置文件(位於C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Config\\machine.config
)中定義,並且定義如下:
<add name="RsaProtectedConfigurationProvider" type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" description="Uses RsaCryptoServiceProvider to encrypt and decrypt" keyContainerName="NetFrameworkConfigurationKey" cspProviderName="" useMachineContainer="true" useOAEP="false"/>
如果您想使用用戶級位置加密您的部分,可以在自己的app.config文件中覆蓋此配置(請參閱此處 )。
現在,當您了解所有這些時,您可以做出明智的決定,是否需要加密您的部分,如果是,則使用哪個位置。
如果您使用計算機級位置(默認) - 您的應用程序應該在提升(在管理員下)運行,以加密和解密您的部分。 如果有人可以訪問您的配置文件 - 他將無法在沒有管理員權限的情況下解密它。 在某些環境(特別是公司)中運行提升可能是一個問題。
如果您使用用戶級別的位置 - 您的應用程序不需要運行提升。 只有加密部分的用戶以后才能對其進行解密。 如果有人在不同的用戶(想象一些公司域)下訪問您的計算機並竊取文件 - 他將無法解密它。
您可以為特定用戶\\機器預加密您的部分(加密部分的密鑰可以在必要時從一台機器導出到另一台機器),或者在第一次運行時請求用戶輸入敏感數據(以數據庫的密碼為例) - 然后將該數據保存到app.config和encrypt部分。
至於為什么你自動獲得解密值 - 這是因為如果可能的話,它會被動態解密。 您默認情況下以管理員身份運行(例如,禁用UAC),因此您可以訪問用於加密的密鑰,因此可以解密。 如果您在沒有管理員的情況下運行 - 當您嘗試訪問加密值時,它將拋出異常。
https://learn.microsoft.com/en-us/previous-versions/as.net/yxw286t2(v=vs.100)
創建 RSA 密鑰容器
aspnet_regiis -pc "MyKeys" –exp
添加文件配置configProtectedData keyContainerName="MyKeys"
為 RSA 密鑰容器分配權限:
aspnet_regiis -pa "MyKeys" "iis apppool\SCI"
aspnet_regiis.exe -pef "connectionStrings" H:\inetpub\wwwroot\SCI\SCICampanas -prov "MyProvider"
aspnet_regiis.exe -pdf "connectionStrings" H:\inetpub\wwwroot\SCI\SCICampanas
編輯文件配置並刪除configProtectedData
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.