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. 远程服务器使用phantomjs报错:phantomjs unexpectedly exited. Status code was: 127

    原因是因为缺少phantomjs运行的依赖包 Ubuntu尝试: sudo apt-get install libfontconfig centos尝试: 用wget安装phantomjs依赖的lib ...

  2. python获取日期加减之后的日期

    python语言中的datetime模块可以利用其中的方法获取不同的日期,比如获取当前日期.明天.昨天.上个月.下个月和明年.下面利用几个实例说明这些日期的获取方法,操作如下:     第一步,利用d ...

  3. 面试时怎样回答:你对原生ajax的理解

    很多人跟我一样用习惯了jq封装好的$.ajax,但是面试时,原生ajax是很多面试官喜欢问的问题,今天再查资料,打算好好整理一下自己理解的原生ajax. 首先,jq的ajax:一般我常用的参数就是这些 ...

  4. java 中 “==” 和 equals 的区别

    转自https://www.cnblogs.com/www123----/p/7857298.html 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new Strin ...

  5. mybatis批量提交

    之前在做项目时,使用mybatis,批量执行sql,这里简单写下步骤 在配置数据库连接时,加入一个参数,例如 jdbc:mysql://127.0.0.1:3307/mvs-report?allowM ...

  6. BZOJ_4516_[Sdoi2016]生成魔咒_后缀数组+ST表+splay

    BZOJ_4516_[Sdoi2016]生成魔咒_后缀数组+ST表+splay Description 魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示.例如可以将魔咒字符 1.2 拼凑起来形成一个魔 ...

  7. BZOJ_2600_[Ioi2011]ricehub_二分答案

    BZOJ_2600_[Ioi2011]ricehub_二分答案 Description 乡间有一条笔直而长的路称为“米道”.沿着这条米道上 R 块稻田,每块稻田的坐标均 为一个 1 到 L 之间(含 ...

  8. BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈

    BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao ...

  9. 蚂蚁通讯框架SOFABolt之私有通讯协议设计

    前言 SOFABolt 是蚂蚁金融服务集团开发的一套基于 Netty 实现的网络通信框架. 为了让 Java 程序员能将更多的精力放在基于网络通信的业务逻辑实现上,而不是过多的纠结于网络底层 NIO ...

  10. 使用BeetleX的TcpBenchmark工具进行百万设备模拟测试

    其实TCP测试的工具有很多,那BeetleX工具所提供的特点又是什么呢?如果你需数十万的请求或模拟上百万的设备连接,那这个工具相信可以满足你的需要!工具是基于BeetleX的基础功能扩展,支持多IP绑 ...