public static JsonCapsula GetJsonDecoded(Context context, byte[] message, long senderId) { byte[] aesBinKey = context.Contacts .Where(u => u.PublicId == senderId) .Select(u => u.ReceiveAesKey) .SingleOrDefault(); AESPassword key = new AESPassword(aesBinKey); byte[] decrypted = key.Decrypt(message); MemoryStream stream = new MemoryStream(decrypted); JsonTypes type = (JsonTypes)BinaryEncoder.ReadInt(stream); string jsonText = TextEncoder.ReadString(stream); byte[] attechment = null; int isAttechment = BinaryEncoder.ReadInt(stream); if (isAttechment == 1) { attechment = BinaryEncoder.ReceiveBytes(stream); } IJType jmessage; switch (type) { case JsonTypes.ALARM: jmessage = JsonConvert.DeserializeObject <JAlarm>(jsonText); break; case JsonTypes.CONTACT: jmessage = JsonConvert.DeserializeObject <JContact>(jsonText); break; case JsonTypes.MESSAGES: jmessage = JsonConvert.DeserializeObject <JMessage>(jsonText); break; case JsonTypes.MESSAGES_THREAD: jmessage = JsonConvert.DeserializeObject <JMessageThread>(jsonText); break; default: throw new Exception("Unknown JsonType."); } return(new JsonCapsula() { Attechment = attechment, Message = jmessage }); }
private void UntrustContact() { int recepientId = BinaryEncoder.ReadInt(stream); using (Context context = new Context(config)) { var key = context.UsersKeys .Where(u => u.RecepientId == recepientId) .Where(u => u.SenderId == connectionInfo.UserId) .SingleOrDefault(); if (key != null) { key.Trusted = false; context.SaveChanges(); } } Log("Untrust contact done."); }
private void TrustContact() { int recepientId = BinaryEncoder.ReadInt(stream); using (Context context = new Context(config)) { var key = context.UsersKeys .Where(u => u.RecepientId == recepientId) .Where(u => u.SenderId == connectionInfo.UserId) .SingleOrDefault(); if (BinaryEncoder.ReadInt(stream) == 0) { Log("No new key will be received."); key.Trusted = true; } else { Log("Receiving new key."); var aesKey = BinaryEncoder.ReceiveBytes(stream); if (key != null) { Log("Updating denied!"); //key.AesKey = aesKey; key.Trusted = true; } else { key = new UsersKeys() { RecepientId = recepientId, SenderId = connectionInfo.UserId, AesKey = aesKey, Trusted = true }; context.Add(key); } } context.SaveChanges(); } Log("Trust contact done."); }