public static EncryptedMessage GenerateMessage(string Sender, object message, RSACryptoServiceProvider receiver_rsa, string token) { RijndaelManaged RMCrypto = new RijndaelManaged(); RMCrypto.GenerateIV(); RMCrypto.GenerateKey(); byte[] result_message = RMCrypto.getGlobalObject <IMysteryJsonConverter>().getJson(message).getBytes(); var ms = new MemoryStream(); CryptoStream CryptStream = new CryptoStream(ms, RMCrypto.CreateEncryptor(RMCrypto.Key, RMCrypto.IV), CryptoStreamMode.Write); CryptStream.Write(result_message, 0, result_message.Length); CryptStream.FlushFinalBlock(); byte[] message_id_bytes = Guid.NewGuid().ToString().getBytes(); RSACryptoServiceProvider my_rsa = CryptStream.getGlobalObject <IMyRsaProvider>().getMySra(); EncryptedMessage answer = new EncryptedMessage { Sender = Sender, IV = receiver_rsa.Encrypt(RMCrypto.IV, false), Key = receiver_rsa.Encrypt(RMCrypto.Key, false), Message = ms.ToArray(), Signature = my_rsa.SignData(result_message, new SHA1CryptoServiceProvider()), Token = receiver_rsa.Encrypt(token.getBytes(), false), MessageID = receiver_rsa.Encrypt(message_id_bytes, false), MessageIDSignaure = my_rsa.SignData(message_id_bytes, new SHA1CryptoServiceProvider()) }; return(answer); }