cnlan

导航

应程程序给连接数据库字符串加密...

许多朋友都遇到相同的一个问题,即数据库连接信息是敏感信息,不能让最终用户随便查看(一般在项目当中,只允许系统管理员查看).
在web开发当中,可以使用aspnet_regiis.exe命令来实现加密,但是在应用程序,则需要自己写代码来实现.
代码相当简单:

 1        /// <summary>
 2        /// 加密配置表[连接字符串]
 3        /// </summary>        

 4        public void EncryptConfiguration()
 5        {
 6            //
 7            //使用什么类型的加密
 8            //
 9            string provider = "RsaProtectedConfigurationProvider";
10            Configuration config = null;
11            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
12            //
13            // 加密连接字符串
14            //
15            ConfigurationSection section = config.ConnectionStrings;              
16            if ((section.SectionInformation.IsProtected == false&&
17                (section.ElementInformation.IsLocked == false))
18            {
19                section.SectionInformation.ProtectSection(provider);
20                section.SectionInformation.ForceSave = true;
21                config.Save(ConfigurationSaveMode.Full);
22            }

23        }
在运行程序的时候,每次都运行这个方法就OK了.希望这篇文章对大家有一定的帮助...


posted on 2008-07-04 16:19  小菜猪  阅读(730)  评论(1编辑  收藏  举报