public static byte[] GetJsonEncoded(Context context, IJType message, long receiverId, byte[] attechment = null) { byte[] aesBinKey = context.Contacts .Where(u => u.PublicId == receiverId) .Select(u => u.SendAesKey) .SingleOrDefault(); AESPassword key = new AESPassword(aesBinKey); MemoryStream stream = new MemoryStream(); BinaryEncoder.SendInt(stream, (int)message.GetJsonType()); string json = JsonConvert.SerializeObject(message); TextEncoder.SendString(stream, json); if (attechment == null) { BinaryEncoder.SendInt(stream, 0); } else { BinaryEncoder.SendInt(stream, 1); BinaryEncoder.SendBytes(stream, attechment); } byte[] notEncrypted = stream.ToArray(); return(key.Encrypt(notEncrypted)); }
static void AesTrial() { using (Context context = new Context(config)) { byte[] aesBinKey = context.Contacts .Where(u => u.PublicId == connection.UserId) .Select(u => u.ReceiveAesKey) .SingleOrDefault(); AESPassword key = new AESPassword(aesBinKey); String testStr = "Testy tisty teristy\n"; for (int i = 0; i != 3; i++) { testStr += testStr; } WriteLine($"Encrypting string: {testStr}"); byte[] data = Encoding.UTF8.GetBytes(testStr); byte[] encrypted = key.Encrypt(data); WriteLine(Encoding.UTF8.GetString(key.Decrypt(encrypted))); } }
public static byte[] GetJsonEncoded(Context context, IJType message, long receiverId) { byte[] aesBinKey = context.Contacts .Where(u => u.PublicId == receiverId) .Select(u => u.SendAesKey) .SingleOrDefault(); AESPassword key = new AESPassword(aesBinKey); MemoryStream stream = new MemoryStream(); stream.WriteByte((byte)message.GetJsonType()); string json = JsonConvert.SerializeObject(message); StreamWriter writer = new StreamWriter(stream, Encoding.UTF8); writer.Write(json); writer.Close(); byte[] notEncrypted = stream.ToArray(); return(key.Encrypt(notEncrypted)); }