62,041
社区成员
发帖
与我相关
我的任务
分享
#region MD5加密解密
/// <summary>
/// MD5加密
/// </summary>
/// <param name="password">需要加密的字符串</param>
/// <returns>MD5加密后的字符串</returns>
public static string Md5Encrypt(string password)
{
//把字符串放到byte数组中
byte[] bytIn = Encoding.Default.GetBytes(password);
//建立加密对象的密钥和偏移量
byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定义偏移量
byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定义密钥
//实例DES加密类
DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider() { Key = iv, IV = key };
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
//实例MemoryStream流加密密文件
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
/// <summary>
/// MD5解密
/// </summary>
/// <param name="password">需要解密的字符串</param>
/// <returns>MD5解密后的字符串</returns>
public static string Md5Decrypt(string password)
{
//将解密字符串转换成字节数组
byte[] bytIn = Convert.FromBase64String(password);
//给出解密的密钥和偏移量,密钥和偏移量必须与加密时的密钥和偏移量相同
byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定义偏移量
byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定义密钥
DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider() { Key = iv, IV = key };
//实例流进行解密
MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader strd = new StreamReader(cs, Encoding.Default);
return strd.ReadToEnd();
}
#endregion
protected void txtLogin_Click(object sender, EventArgs e)
{
string strUserName = this.txtLoginName.Text.Trim();
string strPassword = this.txtPassward.Text.Trim();
if (string.IsNullOrEmpty(strUserName) || string.IsNullOrEmpty(strPassword))
{
this.spanError.InnerText = "用户名或密码不能为空!";
return;
}
string strMDPassword = SecurityHelper.SetMD5(strPassword);
Employee entity = new Employee(string.Format(" IDPlus ={0} and LoginPassword='{1}' and DelStatus=0", strUserName, strMDPassword), false);
if (!entity.HaveData)
{
this.spanError.InnerText = "用户名或密码不正确!";
return;
}
HttpCookie userCookie = this.Request.Cookies["WNNUserInfo"];
if (userCookie != null)
{
this.Response.Cookies.Remove("WNNUserInfo");
}
userCookie = new HttpCookie("WNNUserInfo");
userCookie.Values["UserName"] = strUserName;
userCookie.Values["Password"] = strMDPassword;
userCookie.Values["UserIDPlus"] = entity.Idplus.ToString();
userCookie.Expires = DateTime.Now.AddDays(1);
this.Response.AppendCookie(userCookie);
login.aspx里只有这些有用的了,其他都是登录日志了 protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
HttpCookie userCookie = this.Request.Cookies["WNNUserInfo"];
if (userCookie != null)
{
this.txtLoginName.Text = userCookie["UserName"] == null ? "" : userCookie["UserName"].ToString();
if (userCookie["Password"] != null)
{
this.txtPassward.Text = SecurityHelper.DEMD5(userCookie["Password"].ToString());
}
}
}
}
我看了代码是这个,有没有人能解出来