Предполагается, что код C# .NET Framework 4.5, над которым я работаю, позволяет мне передавать текст через зашифрованный поток в другую программу. Я создал две простые программы, чтобы продемонстрировать мою проблему. EncryptionTestA — это сервер, и он должен запускаться первым. EncryptionTestB является клиентом и должен запускаться вторым. После подключения EncryptionTestB передает текст «hello world» другой программе, передавая его через CryptoStream. По крайней мере, в теории.
Что на самом деле происходит, ничего. Я подтвердил это, наблюдая за передачей данных с помощью Wireshark на внутреннем интерфейсе. Этот код не передает абсолютно никаких данных в его нынешнем виде. Единственный способ заставить его отправить «привет, мир» - это закрыть StreamWriter на стороне клиента. Проблема в том, что он также закрывает базовое TCP-соединение, чего я не хочу делать.
Итак, мой вопрос: как мне сбросить StreamWriter/CryptoStream, не закрывая базовое TCP-соединение?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections;
using System.Threading;
using System.Security;
using System.Security.Cryptography;
using System.Net;
using System.Net.Sockets;
namespace EncryptionTestA
{
class Program
{
static void Main(string[] args)
{
TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 1892);
listener.Start();
TcpClient client = listener.AcceptTcpClient();
NetworkStream ns = client.GetStream();
Rijndael aes = RijndaelManaged.Create();
byte[] key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
byte[] iv = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
CryptoStream cs = new CryptoStream(ns, aes.CreateDecryptor(key, iv), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
String test = sr.ReadLine();
Console.Read();
sr.Close();
cs.Close();
ns.Close();
client.Close();
listener.Stop();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections;
using System.Threading;
using System.Security;
using System.Security.Cryptography;
using System.Net;
using System.Net.Sockets;
namespace EncryptionTestB
{
class Program
{
static void Main(string[] args)
{
TcpClient client = new TcpClient();
client.Connect(IPAddress.Parse("127.0.0.1"), 1892);
NetworkStream ns = client.GetStream();
Rijndael aes = RijndaelManaged.Create();
byte[] key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
byte[] iv = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
CryptoStream cs = new CryptoStream(ns, aes.CreateEncryptor(key, iv), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs);
sw.WriteLine("hello world");
sw.Flush();
//sw.Close();
Console.Read();
sw.Close();
cs.Close();
ns.Close();
client.Close();
}
}
}