正则表达式匹配并替换字符串
2015-05-29 23:54
267 查看
场景:
2012年在做广州地铁协同办公项目时,客户觉得明文的数据库链接用户密码配置在web.config里面不安全;其实.NET里的web.config在IIS中有所限制,对安全性还是有保障的。但是客户既然有这样稍微“变态”的需求,那我们就考虑怎么去把它实现吧。
存在的技术难点:
(1)web.config中配置的数据库链接用户密码必须是经过特殊加密的
(2)从第一点出发,既然要加密,那是选择MD5之类的不可逆加密,还是选择AES256之类的可逆加密呢?由于在数据访问层中连接数据库进行数据交互必须是有效的明文用户和其密码,所以我们选择AES256之类的可逆加密,加密解密算法可以进一步自定义,这里就不讲解如何实现,相信online search下就很多相关文章了
好了,话不多说,代码实践见真理:
例如加密前为:eip_hr_user123,加密后为:3OHOG6W9NgpJTriw4x6JDg==
dataconfiguration.config配置文件内容:
.NET中使用正则表达式匹配并替换字符串,其实在JavaScript中也可以用这样的正则表达式,只是写法大同小异而已:
2012年在做广州地铁协同办公项目时,客户觉得明文的数据库链接用户密码配置在web.config里面不安全;其实.NET里的web.config在IIS中有所限制,对安全性还是有保障的。但是客户既然有这样稍微“变态”的需求,那我们就考虑怎么去把它实现吧。
存在的技术难点:
(1)web.config中配置的数据库链接用户密码必须是经过特殊加密的
(2)从第一点出发,既然要加密,那是选择MD5之类的不可逆加密,还是选择AES256之类的可逆加密呢?由于在数据访问层中连接数据库进行数据交互必须是有效的明文用户和其密码,所以我们选择AES256之类的可逆加密,加密解密算法可以进一步自定义,这里就不讲解如何实现,相信online search下就很多相关文章了
好了,话不多说,代码实践见真理:
例如加密前为:eip_hr_user123,加密后为:3OHOG6W9NgpJTriw4x6JDg==
dataconfiguration.config配置文件内容:
<configuration> <configSections> <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> </configSections> <dataConfiguration defaultDatabase="ORADBConnection" /> <connectionStrings> <add name="ORADBConnection" connectionString="Persist Security Info=True;User ID=eip_hr_user;Password=3OHOG6W9NgpJTriw4x6JDg==;Data Source=prd" providerName="System.Data.OracleClient" /> </connectionStrings> </configuration>
.NET中使用正则表达式匹配并替换字符串,其实在JavaScript中也可以用这样的正则表达式,只是写法大同小异而已:
internal Database oraDB { get { if (_oradb != null) return _oradb; FileConfigurationSource dataSource = new FileConfigurationSource("dataconfiguration.config"); ConnectionStringsSection csSection = (ConnectionStringsSection)dataSource.GetSection("connectionStrings"); ConnectionStringSettings csSettings = csSection.ConnectionStrings["ORADBConnection"]; if (csSettings != null) { string connectionStr = csSettings.ConnectionString; //author: Kenmu //created time: 2012-09-24 //function: 针对密码进行加密的情况,必须解密 begin string pwd; Regex r = new Regex("Password=(?<Pwd>[^;]+)", RegexOptions.IgnoreCase);//?<Pwd>为标示符,不参与匹配的;+?表示非贪婪匹配 Match m = r.Match(connectionStr); if (m.Success) { pwd = m.Groups["Pwd"].Value; //获取到密文 try { connectionStr = connectionStr.Replace(string.Format("={0}", pwd), string.Format("={0}", Cryptogram.DecryptPassword(pwd))); //对密文进行解密操作,Cryptogram.DecryptPassword为自定义的可逆解密方法 } catch { } } //function: 针对密码进行加密的情况,必须解密 end _oradb = new OracleDatabase(connectionStr); } return _oradb; } }
相关文章推荐
- c/c++与as3中的逗号表达式
- Kali实现局域网ARP欺骗和ARP攻击
- (7) 如何用Apache POI操作Excel文件-----如何用Apache POI 画一个离散图
- C#基础篇01
- 【金阳光测试】KK公益讲座第63期-5月30号(周六晚八点) 分享主题: IOS专项测试基础
- libtool: *.la was moved的问题
- 兵熊熊一个,将熊熊一窝
- Cocos2d-x游戏开发笔记(一)游戏背景音乐的添加
- 【原】EF简单使用
- 4113_exp_code
- LeetCode27:Remove Element
- java.lang.InstantiationException
- 关于C++类成员函数的重载、覆盖、隐藏与virtual关键字
- poj 2653 Pick-up sticks(判断线段相交)
- UESTC_酱神寻宝 2015 UESTC Training for Dynamic Programming<Problem O>
- 10003-Cutting sticks
- HihoCoder#1082: 然而沼跃鱼早就看穿了一切
- linux c下输入密码不回显
- VMware workstation安装linux(ubuntu)配置详解
- HihoCoder#1082: 然而沼跃鱼早就看穿了一切