一般的项目都有一个用户表,请问在这个表中,你的账号和密码都是明文存储的么?那么怎么防止被别人看见用户的密码呢?
我见过一个项目是这样的,在用户注册时就对用户的密码进行MD5加密,这样用户表中存储的密码就是加密的信息,就算管理员也不能看到用户的密码,用户在登录时输入账号和密码,在后台把用户密码惊醒MD5摘要之后和数据库的密码就行比对,如果一致就可以登陆,但是用户忘记密码之后就没有任何人能看到密码了,只能根据用户的提示问题重新设置。
确切的说,数据库中存储的不是”加密“后的密码,而是密码的摘要信息,通过这个摘要信息是无法还原出密码的。用户登录时拿到原密码之后,按照相同的算法,再计算一遍摘要信息,和数据库中的摘要信息做一下比较,如果相同,就认为密码是正确的。
登录时,我们填写的是明文密码,但在数据库里存储的是加密后的密码,登录时这两者如何比对?
用相同的加密算法再次加密你的明文密码,然后比对数据库中的密文。相同就表示密码正确。当然这个其中可能会牵涉到碰撞等问题,但是基本可以不考虑。
可以参考下这个:
/// <summary>
/// 对密码进行MD5加密的函数(添加盐值:&%#@?,:*
/// </summary>
/// <param name="Password"></param>
/// <returns></returns>
public static string getEncryPassword(string Password)
{
string EncryedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(
Password + "&%#@?$%)@%($)#_$)*", "md5"); // Or "sha1"
return EncryedPassword;
}
/// <summary>
/// 加密
/// </summary>
/// <param name="strText"></param>
/// <returns></returns>
public static string EncryptText(String strText)
{
return Encrypt(strText, "&%#@?$%)@%($)#_$)*");
// return Encrypt(strText,DateTime.Now.ToString() );
}
/// <summary>
/// 解密
/// </summary>
/// <param name="strText"></param>
/// <returns></returns>
public static String DecryptText(String strText)
{
return Decrypt(strText, "&%#@?$%)@%($)#_$)*");
// return Decrypt(strText,DateTime.Now.ToString());
}
/// <summary>
/// 加密函数
/// </summary>
/// <param name="strText"></param>
/// <param name="strEncrKey"></param>
/// <returns></returns>
public static String Encrypt(String strText, String strEncrKey)
{
Byte[] byKey = { };
Byte[] IV = { 0x01, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception ex)
{
return ex.Message;
}
}
/// <summary>
/// 解密函数
/// </summary>
/// <param name="strText"></param>
/// <param name="sDecrKey"></param>
/// <returns></returns>
public static String Decrypt(String strText, String sDecrKey)
{
char[] stBase = strText.ToCharArray();
for (int i = 0; i < stBase.Length; i++)
{
if (stBase[i] == ' ')
{
stBase[i] = '+';
}
}
strText = "";
for (int i = 0; i < stBase.Length; i++)
{
strText += stBase[i];
}
Byte[] byKey = { };
Byte[] IV = { 0x01, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
Byte[] inputByteArray = new byte[strText.Length];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (Exception ex)
{
return ex.Message;
}
}