代码之家  ›  专栏  ›  技术社区  ›  Mohammad

通过网络加密字符串#

c#
  •  3
  • Mohammad  · 技术社区  · 15 年前

    程序运行得很好,但我需要对它进行加密,这就是问题所在,当我使用加密时,它似乎在每次发送数据后都会关闭连接,我需要重新运行它以接收更多的消息(我不能这样做,因为它在聊天,而且应该是连续的)

    顺便说一下,我不介意,即使它是一个弱加密,只要它不是纯文本是好的。

    这是我的程序(服务器)的一个方面:

    public static void Main()
       {
          string data;
          IPEndPoint ip = new IPEndPoint(IPAddress.Any, 9999);
    
          Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
    
          socket.Bind(ip);
          socket.Listen(10);
    
          Socket client = socket.Accept();
          IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint;
          Console.WriteLine("Connected with {0} at port {1}",newclient.Address, newclient.Port);
    
          NetworkStream ns = new NetworkStream(client);
          StreamReader sr = new StreamReader(ns);
          StreamWriter sw = new StreamWriter(ns);
    
          string welcome = "Welcome";
          sw.WriteLine(welcome);
          sw.Flush();
    
          while(true)
          {
             data = sr.ReadLine();
             Console.WriteLine(data);
             sw.WriteLine(data);
             sw.Flush();
          }
          Console.WriteLine("Disconnected from {0}", newclient.Address);
          sw.Close();
          sr.Close();
          ns.Close(); 
       }
    

    下面是我尝试用于加密的代码示例:(server) IPAddress IP2=IPAddress.Parse(IP); TcpListener TCPListen=新TcpListener(IP2,端口);

                    TCPListen.Start();
    
                TcpClient TCP = TCPListen.AcceptTcpClient();
    
    
                NetworkStream NetStream = TCP.GetStream();
    
    
    
    
                RijndaelManaged RMCrypto = new RijndaelManaged();
    
    
                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 CryptStream = new CryptoStream(NetStream,
                   RMCrypto.CreateDecryptor(Key, IV),
                   CryptoStreamMode.Read);
    
                StreamReader SReader = new StreamReader(CryptStream);
    
    
    
    
                message = SReader.ReadToEnd();
    
                textBox3.Clear();
    
                textBox3.Text = message;
    
                CryptStream.Flush();
    
                SReader.Close();
    
    
    
    
    
    
    
                NetStream.Flush();
    
                NetStream.Close();
    
                TCPListen.Stop();
                TCP.Close();
    

    3 回复  |  直到 15 年前
        1
  •  2
  •   Brian R. Bondy    15 年前

    你考虑过简单地使用 SSL 为了你的通讯(指定SslProtocols.Tls)。

    自从C#2.0以来,SSL套接字支持就一直存在于.NET中。


    如果你想保持你的代码和现在的代码基本相同,只需对加密的二进制数据使用编码,这可能会解决你的问题。例如 Convert.ToBase64String

        2
  •  1
  •   Vitor Py    15 年前

    你应该看看TLS。

    其实并不复杂,也很安全。

    http://www.codeproject.com/KB/IP/sslclasses.aspx

        3
  •  0
  •   Matt Mitchell    15 年前

    好吧,读代码应该 从未 停止写入数据,因为它有 while(true) 没有任何退出条件。