1、加密解密方法

using System;
using System.Security.Cryptography;
using System.Text;
namespace DBUtility
{
/// <summary>
/// DES加密/解密类。
/// </summary>
public class DESEncrypt
{
public DESEncrypt()
{
}

#region ========加密========

/// <summary>
/// 加密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Encrypt(string Text)
{
return Encrypt(Text, "xxxxxxx");
}
/// <summary>
/// 加密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Encrypt(string Text,string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray=Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms=new System.IO.MemoryStream();
CryptoStream cs=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret=new StringBuilder();
foreach( byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}",b);
}
return ret.ToString();
}

#endregion

#region ========解密========

/// <summary>
/// 解密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Decrypt(string Text)
{
return Decrypt(Text, "xxxxxxx");
}
/// <summary>
/// 解密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Decrypt(string Text,string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
int len;
len=Text.Length/2;
byte[] inputByteArray = new byte[len];
int x,i;
for(x=0;x<len;x++)
{
i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
inputByteArray[x]=(byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms=new System.IO.MemoryStream();
CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}

#endregion

}
}

2、用的同种方法加密了链接字符串

web.config文件里

<connectionStrings>
<add name="DBEntities" connectionString="metadata=res://*/Entities.DBEntities.csdl|res://*/Entities.DBEntities.ssdl|res://*/Entities.DBEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=dbtest;persist security info=True;user id=user;password=123456;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

只需要将connectionString里面的进行加密就行。并且,需要将&quot;改成“,链接字符串才能链接数据库,否则会报错。

然后,加密后的字符串替换connectionString里的内容即可,如

3、修改EF实体生成文件

需要先定义一个属性,获取web.config里的加密字符串

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Apps.Common
{
public class ConfigPara
{
public static string EFDBConnection {
get {
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["DBContainer"].ConnectionString;
return DESEncrypt.Decrypt(connection);
}
}
}
}

然后,修改 DBEntities.Context.cs

最后,修改 DBEntities.Context.tt

这样就可以了,去运行吧。

EF实体实现链接字符串加密的更多相关文章

  1. C# 数据库链接字符串加密工具

    有些项目尤其是WinForm或者是WPF项目,针对一些工具形式的小项目,不想软件流出去之后,懂程序的的拿到手之后一看配置文件就知道了我们数据库的用户名和密码,如果外网能访问的话,那就麻烦大了.所以这里 ...

  2. 如何重写EF DBContext 获取链接字符串的方法

    public partial class byvarDBFirst: DbContext { //使用自定义连接串 private static string GetEFConnctionString ...

  3. ASP.NET MVC5+EF6+EasyUI 后台管理系统(62)-EF链接串加密

    系列目录 前言: 这一节提供一个简单的功能,这个功能看似简单,找了一下没找到EF链接数据库串的加密帮助文档,只能自己写了,这样也更加符合自己的加密要求 有时候我们发布程序为了避免程序外的SQL链接串明 ...

  4. EF删除,查询,Linq查询,Lambda查询,修改链接字符串

    (1)//删除操作 public bool delete() { try { a_context = new AEntities(); b1 = new Table_1(); //删除只需要写主键就行 ...

  5. 构建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x注入的后台管理系统(62)-EF链接串加密

    前言: 这一节提供一个简单的功能,这个功能看似简单,找了一下没找到EF链接数据库串的加密帮助文档,只能自己写了,这样也更加符合自己的加密要求 有时候我们发布程序为了避免程序外的SQL链接串明文暴露,需 ...

  6. 【EF 1】EF实体框架 原理+实例

    一.知识回顾 到目前为止,自己学到的链接数据库操作已经经历了几个阶段,分别是:学生信息管理和(第一次)机房收费时的直接连接数据库操作表格,然后是机房个人重构中应用的操作实体,在其中还利用了一个很重要的 ...

  7. EF实体框架之CodeFirst一

    对于SQL Server.MySql.Oracle等这些传统的数据库,基本都是关系型数据库,都是体现实体与实体之间的联系,在以前开发时,可能先根据需求设计数据库,然后在写Model和业务逻辑,对于Mo ...

  8. EF实体框架之CodeFirst四

    在EF实体框架之CodeFirst二中也提到数据库里面一般包括表.列.约束.主外键.级联操作.实体关系(E-R图).存储过程.视图.锁.事务.数据库结构更新等.前面几篇博客把表.存储过程.视图这些算是 ...

  9. [转载]C#字符串加密和解密

    using System.Security.Cryptography; using System.IO; //默认密钥向量 private static byte[] Keys = { 0x12, 0 ...

随机推荐

  1. MVC项目中怎么浏览html页面

    public class HomeController : Controller { public ActionResult Index() { //return View(); //return R ...

  2. 【状态表示】Bzoj1096 [SCOI2008] 着色方案

    Description 有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木 ...

  3. 【链表】Bzoj1098[POI2007]办公楼biu

    Description FGD开办了一家电话公司.他雇用了N个职员,给了每个职员一部手机.每个职员的手机里都存储有一些同事的电话号码.由于FGD的公司规模不断扩大,旧的办公楼已经显得十分狭窄,FGD决 ...

  4. mysql事务隔离级别和MVCC

    一.三种问题: 脏读(Drity Read):事务A更新记录但未提交,事务B查询出A未提交记录. 不可重复读(Non-repeatable read):在一个事务的两次查询之中数据不一致,这可能是两次 ...

  5. 从零开始学 Web 之 CSS(三)链接伪类、背景、行高、盒子模型、浮动

    大家好,这里是「 Daotin的梦呓 」从零开始学 Web 系列教程.此文首发于「 Daotin的梦呓 」公众号,欢迎大家订阅关注.在这里我会从 Web 前端零基础开始,一步步学习 Web 相关的知识 ...

  6. Vue学习小结(二)

    接上一批,小结(二). 三.导航内容(含左侧导航及顶部面包屑导航) 其实导航条主要根据element-ui的教程进行编写,官网:http://element-ui.cn/#/zh-CN/compone ...

  7. 微信小程序 组件通信相关知识整理

    1.自定义组件间通信与事件 https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/events.htm ...

  8. 微服务(入门一):netcore安装部署consul

    环境准备  vs开发环境:vs2017 consul版本: 1.4.4 netcore版本:2.1 安裝Consul  1.从官网下载consul到本地,选择系统对应的版本进行下载到本地,下载地址:h ...

  9. 安全研究 | Jenkins 任意文件读取漏洞分析

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由云鼎实验室 发表于云+社区专栏 一.漏洞背景 漏洞编号:CVE-2018-1999002 漏洞等级:高危 Jenkins 7 月 18 ...

  10. solr的认识、linux下安装、java下使用(含下载资源)

    目录 一.solr的大概认识 二.solr安装 三.solr的深度认识 四.solr的使用 (1)由于我们用到中文,所以需要中文分析器,这里我用IK Analyzer 2012FF_hf1 (2)同时 ...