使用spring.net+nibernate时数据库连接往往是放在一个 <db:provider ... />字段中的
不能使用aspnet_regiis直接加密
解决办法是引入一个专门负责键值对应的配置节
<section name="databaseSettings" type="System.Configuration.NameValueSectionHandler" />
在这个配置节中声明重要的连接参数
<databaseSettings> <add key="db.datasource" value=".\SQLEXPRESS; Integrated Security=true; AttachDbFilename=|DataDirectory|northwnd.mdf; User Instance=true;"/> <add key="db.user" value="springqa"/> <add key="db.password" value="springqa"/> <add key="db.database" value="Northwind"/> </databaseSettings>
<!-- Property placeholder configurer for database settings --> <object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core"> <property name="ConfigSections" value="databaseSettings"/> </object>
最后,把 <db:provider ... />字段中的内容改换成 以通配符的形式声明
<db:provider id="SqlServerDbProvider" provider="System.Data.SqlClient" connectionstring="Data Source=${db.datasource};database=${db.database};uid=${db.user};pwd=${db.password};"> </db:provider>
这样加密的时候只要加密
<databaseSettings> <add key="db.datasource" value=".\SQLEXPRESS; Integrated Security=true; AttachDbFilename=|DataDirectory|northwnd.mdf; User Instance=true;"/> <add key="db.user" value="springqa"/> <add key="db.password" value="springqa"/> <add key="db.database" value="Northwind"/> </databaseSettings>这段就行了,加密后的效果
<databaseSettings 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>PtULk77o93K0Wf66BLm+6ljtChqAha1+fHeVpFboLUBzOGk9256pvpVxyjAbxnxWGBp6s6haCwZS+gofCXfCzycEIxiXCaxTHUhFiUagfutCEHlTg/TwqO0s6B3qvzSZG/rMdAZc4vGt8yOv+XiZHlG/FZoH1LUaZUM+MmRXcoY=</CipherValue> </CipherData> </EncryptedKey> </KeyInfo> <CipherData> <CipherValue>4F0up6tbkZ1cOxtCuKmLdAtjBFUFLKsGgkzeibWHVdmSgupgfM7cNXHY/c3Emm6z1v99pqJyEMO3DoCfZiEgdka3qldxNwC+KTFfZDvfYiNuHnFIUY3VjuPiWmeQ69DLF5454u3V3GctPYAVmE0lFc+PYYyEH4Tn4YO4VtiwhWkHRFcEvt7mk4Y+4/aw37d8rrJV9Sx6Z1uJ4isKbAy2DXxJ82QRkilMbl/B6r0+1f4xK3OGEkVL8PRL3B/BOGzXOtOwJckxSyKL8LBbmw8P0J+JoTnuwhO50jMjqROOWg2GVFZkTvwMLg==</CipherValue> </CipherData> </EncryptedData> </databaseSettings>