来源:https://www.cnblogs.com/xszjk/articles/EFConfig.html
来源:https://blog.csdn.net/qdluo/article/details/81391503
特此记录下
找到模型数据
打开
修改为
注意:
sb是我自己写的类(最近有点恼火)
定义如下方法
public static string ConnectionString
{
get
{
string _connectionString = ConfigurationManager.ConnectionStrings["kkkEntities"].ConnectionString;
string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
if (ConStringEncrypt == "true")
{
sb Sb = new sb();
_connectionString = Sb.Decrypt(_connectionString);
}
return _connectionString;
}
}
ConfigurationManager.AppSettings[“ConStringEncrypt”];
这句的作用是,连接字符串是否加密。机智的做法!!
可以在配置文件中定义
<appSettings>
<!--是否使用加密的数据库连接字符串-->
<add key="ConStringEncrypt" value="true"/>
</appSettings>
由于我用的EF框架 提供一个没加密的字符串
string Mysql = string.Format("metadata=res://*/KeyouModel.csdl|res://*/KeyouModel.ssdl|res://*/KeyouModel.msl;provider=MySql.Data.MySqlClient;provider connection string=\"; server = {0}; user id = {1}; password = {2}; persistsecurityinfo = True; database = {3}\";", IP, UserName, UserPwd, BaseName);
关于加密
来源:https://www.cnblogs.com/wifi/articles/2482350.html
public class sb
{
static string encryptKey = "Cnmd"; //定义密钥 四个字符 随意写
#region 加密字符串
/// <summary> /// 加密字符串
/// </summary>
/// <param name="str">要加密的字符串</param>
/// <returns>加密后的字符串</returns>
public string Encrypt(string str)
{
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider(); //实例化加/解密类对象
byte[] key = Encoding.Unicode.GetBytes(encryptKey); //定义字节数组,用来存储密钥
byte[] data = Encoding.Unicode.GetBytes(str);//定义字节数组,用来存储要加密的字符串
MemoryStream MStream = new MemoryStream(); //实例化内存流对象
//使用内存流实例化加密流对象
CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
CStream.Write(data, 0, data.Length); //向加密流中写入数据
CStream.FlushFinalBlock(); //释放加密流
return Convert.ToBase64String(MStream.ToArray());//返回加密后的字符串
}
#endregion
#region 解密字符串
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="str">要解密的字符串</param>
/// <returns>解密后的字符串</returns>
public string Decrypt(string str)
{
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider(); //实例化加/解密类对象
byte[] key = Encoding.Unicode.GetBytes(encryptKey); //定义字节数组,用来存储密钥
byte[] data = Convert.FromBase64String(str);//定义字节数组,用来存储要解密的字符串
MemoryStream MStream = new MemoryStream(); //实例化内存流对象
//使用内存流实例化解密流对象
CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
CStream.Write(data, 0, data.Length); //向解密流中写入数据
CStream.FlushFinalBlock(); //释放解密流
return Encoding.Unicode.GetString(MStream.ToArray()); //返回解密后的字符串
}
#endregion
//模型需要解密的字符串 与加解密无关
public static string ConnectionString
{
get
{
string _connectionString = ConfigurationManager.ConnectionStrings["keyouEntities"].ConnectionString;
string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
if (ConStringEncrypt == "true")
{
sb Sb = new sb();
_connectionString = Sb.Decrypt(_connectionString);
}
return _connectionString;
}
}
}
加密连接字符串的时候要注意,"“是双引号,加密时要将其改为” "才可以,不然会报出data source关键字的错误!