public APIMessageView UpdateTimebombStatus(TimeBomb timebomb, int status) { APIMessageView results = new APIMessageView(); try { // Retrieve bomb with id i TimeBomb defuseBomb = _context.TimeBombs.FirstOrDefault(x => x.TimeBombId == timebomb.TimeBombId); // Examine bomb's status to ensure it is armed if (defuseBomb.Status != status) { string encryptedMessage = EncryptionMethods.encrypt(EncryptionMethods.crocodile(), timebomb.KillMessage, EncryptionMethods.alphabet()); // Hash killmessage and compare it to bomb's message if (encryptedMessage.Equals(defuseBomb.KillMessage)) { System.Console.WriteLine("Success! KillMessages match!"); // If match, set bomb status to -1 defuseBomb.UpdatedAt = DateTime.Now; defuseBomb.Status = status; _context.SaveChanges(); results.TimeBomb = defuseBomb; string stringStatus = getTimebombStatus(status); System.Console.WriteLine($"Success! Timebomb set to: {stringStatus}!"); results.Success = true; results.Message = $"Success! Timebomb set to: {stringStatus}!"; } else { System.Console.WriteLine("Failure! KillMessages do not match!"); results.Success = false; results.Message = "Failure! KillMessages do not match!"; } } else { System.Console.WriteLine($"Error! Timebomb cannot set status to {status}! Status already set to {status}"); results.Success = false; results.Message = $"Error! Timebomb cannot set status to {status}! Status already set to {status}"; } } catch (Exception e) { System.Console.WriteLine("Something's f****d!"); System.Console.WriteLine(e); results.Success = false; results.Message = "An error occured! " + e; } return(results); }
public IActionResult DecryptMessage(SecretView nice) { int?userId = HttpContext.Session.GetInt32("user"); if (userId == null) { return(RedirectToAction("Index")); } try { int id = Int32.Parse(nice.MessageId); // If the message has been accessed too many times, blocks attempt DateTime then = DateTime.Now; then = then.AddHours(-1); List <Access> accesses = _context.Accesses.Where(x => x.MessageId == id).Where(x => x.CreatedAt > then).ToList(); if (accesses.Count() >= 5) { System.Console.WriteLine("Too many accesses! Blocking!"); HttpContext.Session.SetInt32("blocked", 0); } else { HttpContext.Session.SetInt32("blocked", 1); // Decrypting message System.Console.WriteLine("Decrypting message with id:" + id); Message message = _context.Messages.FirstOrDefault(x => x.MessageId == id); HttpContext.Session.SetString("cypher", nice.Cypher); string decrypt = EncryptionMethods.decrypt(message.MessageBody, nice.Cypher, EncryptionMethods.alphabet()); // System.Console.WriteLine(decrypt); HttpContext.Session.SetString("message", decrypt); // Add this attempt to access log Access thisAccess = new Access(); thisAccess.MessageId = id; thisAccess.UserId = (int)userId; thisAccess.CreatedAt = DateTime.Now; _context.Accesses.Add(thisAccess); _context.SaveChanges(); } // Returns return(RedirectToAction("ShowMessage", new { messageId = id })); } catch (Exception e) { System.Console.WriteLine("Uh oh guys, I think I broke it"); System.Console.WriteLine(e); } return(RedirectToAction("RenderPage")); }
public IActionResult NewTimeBomb([FromBody] SecretView timebomb) { APIResponseView results = new APIResponseView(); if (ModelState.IsValid) { try { TimeBomb newTimeBomb = timebomb.NewTimeBomb; newTimeBomb.CreatedAt = DateTime.Now; newTimeBomb.UpdatedAt = DateTime.Now; string encryptedMessage = EncryptionMethods.encrypt(EncryptionMethods.crocodile(), newTimeBomb.KillMessage, EncryptionMethods.alphabet()); newTimeBomb.KillMessage = encryptedMessage; _context.Add(newTimeBomb); _context.SaveChanges(); System.Console.WriteLine("Success! Adding timebomb with id " + newTimeBomb.TimeBombId); _context.SaveChanges(); results.Success = true; results.Message = "Success! Adding timebomb with id " + newTimeBomb.TimeBombId; } catch (Exception e) { System.Console.WriteLine("Something's f****d!"); System.Console.WriteLine(e); results.Success = false; results.Message = "An error occured! " + e; } } else { results.Success = false; string message = "Errant input! Model not valid!"; results.Message = message; System.Console.WriteLine(message); } return(Json(results)); }
public JsonResult NewMessageAPI([FromBody] SecretView secretView) { APIResponseView results = new APIResponseView(); if (ModelState.IsValid) { try { Message newMessage = secretView.NewMessage; newMessage.CreatedAt = DateTime.Now; newMessage.UpdatedAt = DateTime.Now; string encryptedMessage = EncryptionMethods.encrypt(newMessage.MessageBody, newMessage.Cypher, EncryptionMethods.alphabet()); newMessage.MessageBody = encryptedMessage; _context.Add(newMessage); _context.SaveChanges(); System.Console.WriteLine("Success! Adding message with id " + newMessage.MessageId); _context.SaveChanges(); results.Success = true; results.Message = "Success! Adding message with id " + newMessage.MessageId; } catch (Exception e) { System.Console.WriteLine("Something's broke!"); System.Console.WriteLine(e); results.Success = false; results.Message = "An error occured! " + e; } } else { results.Success = false; results.Message = "Errant input! Model not valid!"; System.Console.WriteLine("Errant input! Model not valid!"); } return(Json(results)); }
public string detonate(TimeBomb bomb) { try { // Decrypts timebomb message string decrypt = EncryptionMethods.decrypt(bomb.Message.MessageBody, bomb.MessageCypher, EncryptionMethods.alphabet()); System.Console.WriteLine(decrypt); // Detonates bomb by emailing message to bomb.address bomb.Status = 1; _context.SaveChanges(); return($"Success! Bomb with id {bomb.TimeBombId} detonated!"); } catch (System.Exception e) { bomb.Status = -2; _context.SaveChanges(); return($"Errant operation detonating bomb with id {bomb.TimeBombId}! Error: " + e.Message); } }
public IActionResult NewMessage(SecretView secretView) { if (ModelState.IsValid) { Message newMessage = secretView.NewMessage; newMessage.CreatedAt = DateTime.Now; newMessage.UpdatedAt = DateTime.Now; string encryptedMessage = EncryptionMethods.encrypt(newMessage.MessageBody, newMessage.Cypher, EncryptionMethods.alphabet()); newMessage.MessageBody = encryptedMessage; _context.Add(newMessage); _context.SaveChanges(); System.Console.WriteLine("Adding message with id " + newMessage.MessageId); _context.SaveChanges(); } return(RedirectToAction("RenderPage")); }
public JsonResult DecryptMessageAPI([FromBody] SecretView secretView) { APIResponseView results = new APIResponseView(); try { // Login user int login = _administrator.loginUser(secretView.User); if (login == -1) { results.Success = false; results.Message = "Errant input! Username or password incorrect!"; return(Json(results)); } else { // Check to make sure message has not been accessed too many times int id = Int32.Parse(secretView.MessageId); if (_administrator.checkMessageAccesses(id)) { System.Console.WriteLine("Decrypting message with id:" + id); Message message = _context.Messages.FirstOrDefault(x => x.MessageId == id); if (message == null) { results.Success = false; results.Message = "Errant input! Message with id " + id + " does not exist!"; return(Json(results)); } HttpContext.Session.SetString("cypher", secretView.Cypher); string decrypt = EncryptionMethods.decrypt(message.MessageBody, secretView.Cypher, EncryptionMethods.alphabet()); // System.Console.WriteLine(decrypt); results.Success = true; results.Message = decrypt; // Add this attempt to access log Access thisAccess = new Access(); thisAccess.MessageId = id; thisAccess.UserId = login; thisAccess.CreatedAt = DateTime.Now; _context.Accesses.Add(thisAccess); _context.SaveChanges(); } else { System.Console.WriteLine("Message has been accessed too many times!"); results.Success = false; results.Message = "Message has been accessed too many times!"; } } } catch (Exception e) { System.Console.WriteLine("Errant input!"); System.Console.WriteLine(e); results.Success = false; results.Message = e.ToString(); } return(Json(results)); }