public JsonResult DeleteRow(string r) { string rowDeleted = "false"; if (Session["userid"] != null) { if (Session["userid"].ToString().Trim() != "") { using (var ctx = new PasswordManagerEntities()) { var recordToDelete = ctx.Password.SingleOrDefault(x => x.ID.ToString() == r); if (recordToDelete != null) { ctx.Password.Remove(recordToDelete); ctx.SaveChanges(); rowDeleted = "true"; } else { rowDeleted = "false"; } } } } return(Json(new { rowisdeleted = rowDeleted })); }
public JsonResult LogInUser(string userName, string password) { PasswordManagerEntities PasswordManagerEntities = new PasswordManagerEntities(); var user = (from u in PasswordManagerEntities.User where ((u.UserName == userName)) select u).SingleOrDefault(); string authsuccess = "f"; if (user != null) { //check password using (Aes myAes = Aes.Create()) { byte[] arrayk = Encoding.ASCII.GetBytes(user.UPassHash); // Decrypt the bytes to a string. var roundtrip = DecryptStringFromBytes_Aes(user.UPasswrd, arrayk, user.UPassIVHash); string output = new string(roundtrip.Where(c => !char.IsControl(c)).ToArray()); if (output.Trim() == password.Trim()) { authsuccess = "t"; Session["userid"] = user.ID.ToString(); } else { authsuccess = "f"; } } } return(Json(new { loginsuccess = authsuccess })); }
public JsonResult GetAllStoredPasswords() { int userID = Convert.ToInt32(Session["userid"].ToString()); PasswordManagerEntities PasswordManagerEntities = new PasswordManagerEntities(); var plist = PasswordManagerEntities.Password.Where(g => g.UserID == userID).Select(x => new Models.StoredPassword { ID = x.ID, UserID = x.UserID, Website = x.Website, UserName = x.UserName, Password1 = x.Password1, PasswordHash = x.PasswordHash, PasswordIVHash = x.PasswordIVHash }).ToList(); List <DecodedPassword> decodedPasswordList = new List <DecodedPassword>(); foreach (var i in plist) { decodedPasswordList.Add(new DecodedPassword { ID = i.ID, UserID = i.UserID, Website = i.Website, UserName = i.UserName, Password1 = new string(DecodePassword(i.Password1, i.PasswordHash, i.PasswordIVHash).Where(c => !char.IsControl(c)).ToArray()), PasswordHash = i.PasswordHash, PasswordIVHash = i.PasswordIVHash }); } return(Json(decodedPasswordList, JsonRequestBehavior.AllowGet)); }
public JsonResult RegisterNewUser(string firstname, string lastname, string username, string upassword, string pHash) { //https://msdn.microsoft.com/en-us/library/system.security.cryptography.aes(v=vs.110).aspx string authsuccess = "false"; try { string original = upassword; // Create a new instance of the Aes // class. This generates a new key and initialization // vector (IV). using (Aes myAes = Aes.Create()) { byte[] arrayk = Encoding.ASCII.GetBytes(pHash); myAes.Key = arrayk; byte[] IVmy = myAes.IV; // Encrypt the string to an array of bytes. byte[] encrypted = EncryptStringToBytes_Aes(original, arrayk, IVmy); // var encryptedPwd = System.Text.Encoding.Default.GetString(encrypted); var strpwdIV = System.Text.Encoding.Default.GetString(IVmy); try { PasswordManagerEntities db = new PasswordManagerEntities(); PasswordManager.User user = new PasswordManager.User { FirstName = firstname.Trim(), LastName = lastname.Trim(), UserName = username.Trim(), UPasswrd = encrypted, UPassHash = pHash.Trim(), UPassIVHash = IVmy }; db.User.Add(user); db.SaveChanges(); authsuccess = "true"; } catch (Exception exp) { } // Decrypt the bytes to a string. //string roundtrip = DecryptStringFromBytes_Aes(encrypted,myAes.Key, myAes.IV); } } catch (Exception e) { } return(Json(new { regsuccess = authsuccess })); }
public JsonResult UpdateRow(string w, string u, string p, string r, string h) { string rowUpdated = "false"; if (Session["userid"] != null) { if (Session["userid"].ToString().Trim() != "") { string original = p; // Create a new instance of the Aes // class. This generates a new key and initialization // vector (IV). using (Aes myAes = Aes.Create()) { byte[] arrayk = Encoding.ASCII.GetBytes(h); myAes.Key = arrayk; byte[] IVmy = myAes.IV; // Encrypt the string to an array of bytes. byte[] encrypted = EncryptStringToBytes_Aes(original, arrayk, IVmy); // var encryptedPwd = System.Text.Encoding.Default.GetString(encrypted); var strpwdIV = System.Text.Encoding.Default.GetString(IVmy); try { using (PasswordManagerEntities PasswordManagerEntities = new PasswordManagerEntities()) { var ur = (from q in PasswordManagerEntities.Password where (q.ID.ToString() == r.Trim()) select q).SingleOrDefault(); ur.Website = w.Trim(); ur.UserName = u.Trim(); ur.Password1 = encrypted; ur.PasswordHash = h.Trim(); ur.PasswordIVHash = IVmy; PasswordManagerEntities.SaveChanges(); rowUpdated = "true"; } } catch (Exception exp) { } // Decrypt the bytes to a string. //string roundtrip = DecryptStringFromBytes_Aes(encrypted,myAes.Key, myAes.IV); } } } return(Json(new { rowisupdated = rowUpdated })); }
public JsonResult AddNewPassword(string w, string u, string p, string h) { string addnewpwdsuccess = "false"; try { string original = p; // Create a new instance of the Aes // class. This generates a new key and initialization // vector (IV). using (Aes myAes = Aes.Create()) { byte[] arrayk = Encoding.ASCII.GetBytes(h); myAes.Key = arrayk; byte[] IVmy = myAes.IV; // Encrypt the string to an array of bytes. byte[] encrypted = EncryptStringToBytes_Aes(original, arrayk, IVmy); // var encryptedPwd = System.Text.Encoding.Default.GetString(encrypted); var strpwdIV = System.Text.Encoding.Default.GetString(IVmy); try { PasswordManagerEntities db = new PasswordManagerEntities(); PasswordManager.Password passwd = new PasswordManager.Password { UserID = Convert.ToInt32(Session["userid"].ToString()), Website = w.Trim(), UserName = u.Trim(), Password1 = encrypted, PasswordHash = h.Trim(), PasswordIVHash = IVmy }; db.Password.Add(passwd); db.SaveChanges(); addnewpwdsuccess = "true"; } catch (Exception exp) { } // Decrypt the bytes to a string. //string roundtrip = DecryptStringFromBytes_Aes(encrypted,myAes.Key, myAes.IV); } } catch (Exception e) { } return(Json(new { addnewpasswordstatus = addnewpwdsuccess })); }