找到一个net的连接加密工具,但不会用,分享下
时间:2011-02-18
来源:互联网
然后配置中就这样写:<configuration>
<appSettings>
<add key="Connectionstring" value="8C0CAF6355941F8E8F22A169DD61652B2349FF21803DE491BA6E5082906C3054B1D5CFC525F0FCC04EE66B510D2D28737998B1AFC654B7312852AEFB230BE3B9F42F6FE1F9689B21" />
复制代码这个工具原理是什么,mssql怎么会认得这个工具加密后的字符串呢?
Web.config加密之后,其它页面要如何做,请指点
附件

2011-2-18 13:54, 下载次数: 2
作者: yyb1813 发布时间: 2011-02-18
复制内容到剪贴板
public string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
byte[] bytes = Encoding.Default.GetBytes(pToEncrypt);
provider.Key = Encoding.ASCII.GetBytes(sKey);
provider.IV = Encoding.ASCII.GetBytes(sKey);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach (byte num in stream.ToArray())
{
builder.AppendFormat("{0:X2}", num);
}
builder.ToString();
return builder.ToString();
}
解密的函数(其中KEY为 WABJTAM):
public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
byte[] buffer = new byte[pToDecrypt.Length / 2];
for (int i = 0; i < (pToDecrypt.Length / 2); i++)
{
int num2 = Convert.ToInt32(pToDecrypt.Substring(i * 2, 2), 0x10);
buffer[i] = (byte) num2;
}
provider.Key = Encoding.ASCII.GetBytes(sKey);
provider.IV = Encoding.ASCII.GetBytes(sKey);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
return Encoding.Default.GetString(stream.ToArray());
}
在连接数据库之前要调用它的解密函数,来对那个加密过的字符串进行解密才可以连接数据库.代码:
加密的函数(其中KEY为 WABJTAM):public string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
byte[] bytes = Encoding.Default.GetBytes(pToEncrypt);
provider.Key = Encoding.ASCII.GetBytes(sKey);
provider.IV = Encoding.ASCII.GetBytes(sKey);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach (byte num in stream.ToArray())
{
builder.AppendFormat("{0:X2}", num);
}
builder.ToString();
return builder.ToString();
}
解密的函数(其中KEY为 WABJTAM):
public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
byte[] buffer = new byte[pToDecrypt.Length / 2];
for (int i = 0; i < (pToDecrypt.Length / 2); i++)
{
int num2 = Convert.ToInt32(pToDecrypt.Substring(i * 2, 2), 0x10);
buffer[i] = (byte) num2;
}
provider.Key = Encoding.ASCII.GetBytes(sKey);
provider.IV = Encoding.ASCII.GetBytes(sKey);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
return Encoding.Default.GetString(stream.ToArray());
}
这种加密主要是为了防止别人直接打开Web.Config文件就可以直接的看到数据库的连接信息.
作者: faeng220 发布时间: 2011-02-18
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28