日期:2014-05-18  浏览次数:20862 次

我想为数据库后台密码加密,但是不知道怎么加??
是数据库表的属性加密,MD5和DES都行,但是我不知道怎么加,给了例子行不???
谢谢

------解决方案--------------------
如果只是简单的加密,最好采用对称的,用MD5你怎么解密。加密语句msdn上写的很清楚,照着copy就可以了
------解决方案--------------------
你可以在数据库表里面保存一个加密股则,在存储过程里面读取该规则,然后把加了密的数据进行解密,再存入到数据库中
------解决方案--------------------
MD5只适用于局限的领域.
如用户登录.
当用户注册时,密码是于MD5加密保存到数据库,该算法不可逆,当用户登录时,将提交的密码用MD5加密,与数据库保存的对比,一样就表示密码正确,不一致则错误.

所以楼主要将加密的要求说一下.

可逆算法一般都采用BASE64
------解决方案--------------------
using System.Security.Cryptography;
引用 System.Web
MD5
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("admin","MD5")
不过这个是不可逆的,如果需要判断只能MD5加密后进行比较

DES
private void button2_Click(object sender, EventArgs e)
{
richTextBox1.AppendText(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("admin", "MD5"));
}

private void button3_Click(object sender, EventArgs e)
{
richTextBox1.AppendText(Encode("aaa"));
}

public static string Encode(string data)
{

byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes("VavicApp"); //注意了,是8个字符,64位 
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes("VavicApp");
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length); 
}

public static string Decode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes("VavicApp");
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes("VavicApp");
byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(data);
}
catch
{
return null;
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}

private void button4_Click(object sender, EventArgs e)
{
richTextBox1.AppendText(Decode(richTextBox1.Text.Trim()));
}
------解决方案--------------------
给你一个我曾经用过的加解密方法吧,存入前加密,读取后解密就好,代码不用改动,只需要调用就好
#region 密码加密
/// 根据密钥和需要加密的字符串2个参数加密 
/// </summary> 
/// <param name= "sKey "> 密钥 </param> 
/// <param name= "theEncryptString "> 要加密的字符串 </param> 
/// <returns> 已加密的字符串 </returns> 
public string EncryptTheString(string sKey, string theEncryptString)
{
string sourceStr = theEncryptString;
DESCryptoServiceProvider des = new DESCryptoServiceProvider();