/// <summary> /// url参数加密 /// </summary> /// <param name="paramValue"></param> /// <returns></returns> public static string Encrypt(string paramValue) { Triple_DES des = new Triple_DES((HttpContext.Current == null || HttpContext.Current.Session == null) ? desKey : HttpContext.Current.Session.SessionID); paramValue = des.Encrypt(paramValue); paramValue = StrUtils.ToBase64(paramValue); ////去掉最后一个“=”字符 //paramValue = paramValue.Substring(0, paramValue.Length - 1); return(paramValue); }
/// <summary> /// 加密处理登录信息并写入cookie,cookie值解密后的形式为:JobNo|pwd /// </summary> /// <param name="JobNo">登录名</param> /// <param name="pwd">密码</param> /// <param name="cookieName">cookie名</param> public static void CreateLoginCookie(string JobNo, string cookieName) { Triple_DES des = new Triple_DES(desKey); string loginInfo = JobNo; loginInfo = des.Encrypt(loginInfo); HttpCookie cookie = new HttpCookie(cookieName, loginInfo); cookie.Expires = DateTime.Now.AddDays(15); cookie.HttpOnly = true; HttpContext.Current.Response.Cookies.Add(cookie); }
/// <summary> /// Cookie数据解密 /// </summary> /// <param name="cookieValue"></param> /// <returns></returns> public static string DecryptCookie(string cookieValue) { try { ////把空格替换成“+”,字符串后面加上“=” //cookieValue = cookieValue.Replace(' ', '+') + "="; Triple_DES des = new Triple_DES(desKey); //cookieValue = StrUtils.FromBase64(cookieValue); cookieValue = des.Decrypt(cookieValue); } catch { cookieValue = ""; } return(cookieValue); }
/// <summary> /// url参数解密 /// </summary> /// <param name="paramValue"></param> /// <returns></returns> public static string Decrypt(string paramValue) { try { if (paramValue == "0") { return(paramValue); } ////把空格替换成“+”,字符串后面加上“=” //paramValue = paramValue.Replace(' ', '+') + "="; paramValue = StrUtils.FromBase64(paramValue); Triple_DES des = new Triple_DES(((HttpContext.Current == null || HttpContext.Current.Session == null) ? desKey : HttpContext.Current.Session.SessionID)); paramValue = des.Decrypt(paramValue); } catch (System.Exception ex) { HttpContext.Current.Response.Redirect(string.Format("/Login/Error?message={0}", HttpContext.Current.Server.UrlEncode(ex.Message)), true); } return(paramValue); }
public JsonResult SendMessage() { if (Session["user"] == null) { return(Json(new { status = "error", message = "User is not logged in" })); } var currentUser = (User)Session["user"]; var contact = Convert.ToInt32(Request.Form["contact"]); string socket_id = Request.Form["socket_id"]; string secretKey = Triple_DES.GetRandomKeyHex(); Conversation convo = new Conversation { SenderId = currentUser.Id, Message = Triple_DES.Encrypt(secretKey, HexConverter.StrToHex(Request.Form["message"])), ReceiverId = contact, CreatedAt = DateTime.Now, SecretKey = secretKey }; using (var db = new ChatContext()) { db.Conversations.Add(convo); db.SaveChanges(); convo.Message = Triple_DES.Decrypt(db.Conversations.FirstOrDefault(c => c.Id == convo.Id).SecretKey, convo.Message); } var conversationChannel = getConvoChannel( currentUser.Id, contact); pusher.TriggerAsync( conversationChannel, "new_message", convo, new TriggerOptions() { SocketId = socket_id }); return(Json(convo)); }
public JsonResult ConversationWithContact(int contact) { if (Session["user"] == null) { return(Json(new { status = "error", message = "User is not logged in" })); } var currentUser = (User)Session["user"]; var conversations = new List <Conversation>(); using (var db = new ChatContext()) { conversations = db.Conversations. Where(c => (c.ReceiverId == currentUser.Id && c.SenderId == contact) || (c.ReceiverId == contact && c.SenderId == currentUser.Id)) .OrderBy(c => c.CreatedAt) .ToList(); foreach (var i in conversations) { i.Message = Triple_DES.Decrypt(i.SecretKey, i.Message); } } return(Json(new { status = "success", data = conversations }, JsonRequestBehavior.AllowGet)); }