ef 数据库连接字符串加密

http://www.cnblogs.com/ymnets/p/5572473.html

前言:

  这一节提供一个简单的功能,这个功能看似简单,找了一下没找到EF链接数据库串的加密帮助文档,只能自己写了,这样也更加符合自己的加密要求

  • 有时候我们发布程序为了避免程序外的SQL链接串明文暴露,需要进行一些加密手段!
  • 加密主要分几类:对称加密,非对称加密,散列算法(自己百度脑补,这里不再多说)
  • 我这里选择AES 256位的加密,主要加密速度算法快,安全性高,资源消耗低。
  • 公司一直在使用AES加密来加密一些小数据量的数据,比较方法和安全

  这是我选择加密AES的理由,当然你可以选择其他有名的加密算法,比如MD5,SHA,3DES.(注:大公司应该都是禁止自行写算法的来加解密的)

知识点:

 数据的使用跟我们登录流程基本都是一样的,获取加密链接串,然后解密使用

 所以我们需要:

  1. 加密类
  2. 加密工具
  3. EF在何处使用链接字符串

1.加密类

 AESEncryptHelper.cs

网上一抓一大把,自己搜索想要的加密类啦!

2.加密工具

加密工具这个网上抓不到,需要自己结合加密类来开发,这个不用我带领大伙来开发吧,好吧

新建一个WinFrom程序,命名Apps.EncryptHelper,引用你加密类的所在的类库,或者直接放到Apps.EncryptHelper下就可以

从工具栏拉取2个TextBox和2个Button排版好,基本页面就做完了,最后分别双击两个按钮进入事件实现代码

复制代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Apps.Common;
namespace Apps.EncryptHelper
{
    public partial class Encrypt : Form
    {
        public Encrypt()
        {
            InitializeComponent();
        }
        //加密
        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSourceText.Text))
            {
                MessageBox.Show("没数据加毛密-_-!");
                return;
            }
            else
            {
               txtResultText.Text = AESEncryptHelper.Encrypt(txtSourceText.Text);
            }
        }
        //解密
        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSourceText.Text))
            {
                MessageBox.Show("没数据解毛密-_-!");
                return;
            }
            else if (!IsBase64Formatted(txtSourceText.Text))
            {
                MessageBox.Show("别逗了,我只认识被我加过密的?");
                return;
            }
            else
            {
                txtResultText.Text = AESEncryptHelper.Decrypt(txtSourceText.Text);
            }
        }

        public static bool IsBase64Formatted(string input)
        {
            try
            {
                Convert.FromBase64String(input);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

复制代码

几十行代码,解决车房老婆问题!运行....

.

3.结合进EF

这块还是比较容易搞定的

第一:找到web.config的connectionStrings的EF链接串

第二:把修改对应Key串的Value

  <connectionStrings>
    <add name="DBContainer" connectionString="ka7ocMA8nEYPjbQYUlVwbsmTeIdxKGE+ZfXAu3/0eMhVRP+iN+9ECpY/lItoY9vfZVDA9EVgmMzH/8Z0rxRIhGPRhVMFWliBuJ9RDGtHbqRY02voyLbrZ7IiXRnXyhlLFsvgj23KXnHl8J6jxB1QNsmuUxPlqnD6HP9y5RQq2EJ//OT+uKqhVC1qUqVzdY+XR6HX/O5jGk6kJGk3Nk83qo09eBOundO7OdxQG9SXPUYNyZjhyx9YV2/1UbghuxHrxHrAuxiE4mJLqH/rusjAy8d3LS/ROiiBszSY+I400Ce4NigDwZaG679yvBKBQ5pg" providerName="System.Data.EntityClient" />
  </connectionStrings>

第三:找到EF读取串的地方

这里必须读取解密后发的字符串,所以我们再写一个方法来获取解密后的字符串ConfigPara

复制代码

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 AESEncryptHelper.Decrypt(connection);
            }
        }
    }
}

复制代码

注意修改后也是没有用的,会回档,因为这个类是根据T4生成的,所以我们必须修改T4

修改对应红框的位置!

搞破坏的,难道你现在还能看懂我的连接串?:-)

ok。实现加密,运行正常

大家赶快把他继承到系统里面!

来源:网络


智能推荐

EF

https://docs.microsoft.com/zh-cn/ef/ef6/modeling/code-first/migrations/index 迁移   Code First Migrations is the recommended way to evolve your application's database schema if you are using the Co...

EF

EF 延迟加载与贪婪加载 Db first, code first, model first 实体关系(one-to-one, one-to-many, many-to-many) 延迟加载与贪婪加载 EF查询默认会延迟加载 EF对于集合类型的导航属性会延迟加载 本质:IQueryable拥有3个成员,Expression,Type,Provider IQueryable与IEnumberable...

EF

   public class Article     {        [Key]        public int ID { get; set; }        ...

C#连接数据库 —— (一)实体框架(EF)的创建 (Linq To EF)

Linq 的语法使用示例如下文章(Lambda表达式): C#连接数据库 —— (二)Linq To EF 的 lambda表达式使用 ,增删查改、判断、分组、排序 实体框架(EF)的创建 选择数据模型项目,并取名添加: 选择 EF设计器,下一步: 连接数据库,创建数据模型: 点出新建连接数据库的过程: 选择版本: 选择建立数据模型的数据表: 实体框架模型已经建立完成,但可...

猜你喜欢

EF框架连接Oracle数据库问题

最近遇到一个问题,使用EF框架操作Oracle数据库,使用过程中发现一些问题,希望大牛们帮我解惑吧。 第一:使用EF连接Oracle无法使用modelfirst建立表格。     我看了下报的错误,全是T4模板中的错误,尝试着修改T4模板,也依然存在其他问题。     第二:因为oracle没有自增的设置,只能设置触发器。在这样...

ORM系列之二:EF(2) 数据库连接

ORM系列之二:EF(2) 数据库连接 目录 1.前言 2.Code First默认连接 3.Code First指定数据库 4.自定义连接   前言     在介绍EF的Code First模式时候,我们没有修改任何配置,运行之后自动在LocalDb创建一个新的数据库,并且创建对应的物理表,这个是如何实现的呢?    其实在我们安装EF时,就会自动检测当前系统包含的数据库实例...

西门子S7-300PLC,我们可以使用模块FB41 来实现PID 控制

经过学习西门子S7-300PLC,我们可以使用模块FB41 来实现PID 控制,FB41 就相当 于我们常规仪表里的控制器,既然是PID 控制器就应该能够设定P、I、D 参数。即:比例度、 积分时间、微分时间。常规仪表的面板上可以更改PID 参数,又有手动/自动切换按钮等。 今天我们要做的就是使用S7-300PLC 的FB41 来代替常规仪表,如何使用FB41 来实现 PID 控制的呢?? FB4...

根据dpr动态设置font-size

Dpr为逻辑像素和物理像素的像素比 1:1代表1px=1pt 1:2代表1px=2pt 1:3代表1px=3pt dpr2-3时会将逻辑像素缩小2-3倍然后解析为物理像素呈现 以下图为例     Dpr:1下应设置的逻辑像素   Dpr:2下应设置的逻辑像素   Dpr:3下应该设置的逻辑像素 查看dpr...

kali linux2019.4设置中文以及伪造win10桌面

说明:我使用的是kali-linux-2019-vm-64的版本 一、先添加源,我这里添加的是阿里云的源: 1.打开终端输入:vi /etc/apt/sources.list deb http://mirrors.aliyun.com/kali kali-rolling main non-free contrib deb-src http://mirrors.aliyun.com/kali kal...

问答精选

Selecting multiple items on an actionable list bootstrap, flask-wtf

I am trying to add a delete button to a searchable list of emails like this I was wondering if anyone knows a way to highlight multiple items on a list like this and then send a list of the names to p...

After upgrading eclipse, aptana, pydev, Debugger not working

I upgraded eclipse from 3.6 to 4.2 and Aptana from 2 to 3 with pydev Now I cannot get the debugger to work. I get the following errors: If you're encountering this error, check to make sure you don't ...

Configuring Org-Gcal to have subheadings within Spacemacs

this is a bit of a complicated case, so I will try to be as brief as possible. I would like to use sub headings (level 2+) in org-gcal within Spacemacs. However, the sync currently only allows for lev...

States Drop Down Missing (Magento)

I am currently running Magento version 1.7.0.2. States are not available in the Default State dropdown in admin area. I have only option to choose asterisk (*) with no States listed. I have selected U...

Pygame display 2D numpy array

I've created a 2D numpy array, 20x20, that has a random value of either 0, 1, or 2. What I want is for each of these values to have a corresponding colour value, and for pygame to display a grid of th...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答