2009-11-13 142 views
0

我在写一个客户端 - 服务器应用程序以用于计算机实验室并充当服务(不作为服务运行)。我有一个控制台应用程序使用控制台的HWND对象调用本地函数“ShowWindow”/ SW_HIDE - 这给了它我想要的东西。服务器/客户端正在工作,我发送了消息“Hello world!”从客户端到服务器很多次,我很高兴。 (我使用UDP作为套接字协议,因为IT部门需要一种无连接方式。)客户端 - 服务器数据加密和协议设计

我的问题在于客户端服务器之间通信的“协议”。

服务器背后的目标包括以下几点:

  • 给接入,编程,到我们的IT部门已经阻止了某些安全性的能力(例如,“NET.EXE”)
  • 访问我的程序以监视学生在计算机实验室中查看的内容。

有些事情,我想包括被发送来回简单的问题:

  • “REQUSER”的命令将返回用户名和全名(由“网用户”允许前)
  • “REQPROCS”命令将返回当前正在用户名下运行的进程列表。

我毫不怀疑我能够做到这一点。我目前所熟悉的是数据安全。在我的大学里,我们确实有一些“黑客”,他们可能知道如何封包嗅探,并能够将数据包重新发送到特定的服务器,以便做恶意的事情或获得关于敌人或其他事物的信息。

我的想法是提供所有发送数据的加密方案,并在接收时解码。

我曾经交谈的一位朋友说我应该用一个打包器,然后我开始将他的BitPacker类从C++移植到C#中,这让我感到困惑,并且来看看Stackoverflow的想法。

namespace Atlantis.Net.Sockets 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.Diagnostics; 
    using System.Linq; 
    using System.Net; 
    using System.Net.Sockets; 
    using System.Text; 
    using System.Windows.Forms; 

    public class UdpServer : System.Net.Sockets.UdpClient 
    { 

     #region Constructor(s) 

     public UdpServer(Int32 port) 
      : base(port) 
     { 
     } 

     public UdpServer(IPEndPoint endPoint) 
      : base(endPoint) 
     { 
     } 

     #endregion 

     #region Properties 

     private Int32 m_Backlog = 32; 
     /// <summary> 
     ///  Sets how many active connections the socket can support 
     /// </summary> 
     public Int32 Backlog 
     { 
      private get 
      { 
       return m_Backlog; 
      } 
      set 
      { 
       m_Backlog = value; 
      } 
     } 

     private Boolean m_IsInitialized = false; 
     /// <summary> 
     ///  Gets a value indicating whether the server has been initialized 
     /// </summary> 
     public Boolean IsInitialized 
     { 
      get 
      { 
       return m_IsInitialized; 
      } 
      private set 
      { 
       m_IsInitialized = value; 
      } 
     } 

     private Int32 m_Port = 1337; 
     /// <summary> 
     ///  Sets the port number for listening for incoming connections 
     /// </summary> 
     public Int32 Port 
     { 
      private get 
      { 
       return m_Port; 
      } 
      set 
      { 
       m_Port = value; 
      } 
     } 

     private Encoding m_Encoding = Encoding.ASCII; 
     /// <summary> 
     ///  Gets or sets the text encoding for data being transferred to/from the server 
     /// </summary> 
     public Encoding Encoding 
     { 
      get 
      { 
       return m_Encoding; 
      } 
      set 
      { 
       m_Encoding = value; 
      } 
     } 

     #endregion 

     #region Events 

     public event EventHandler<UdpReceiveEventArgs> DataReceive; 

     #endregion 

     #region Methods 

     protected virtual void OnDataRecieve(String data, object state) 
     { 
      if (DataReceive != null) 
      { 
       DataReceive(this, new UdpReceiveEventArgs(data, ((UdpState)state))); 
      } 
     } 

     private void DataReceiveCallback(IAsyncResult ar) 
     { 
      UdpClient u = (UdpClient)((UdpState)ar.AsyncState).host; 
      IPEndPoint e = (IPEndPoint)((UdpState)ar.AsyncState).endPoint; 

      Byte[] data = u.EndReceive(ar, ref e); 

      OnDataRecieve(Encoding.GetString(data), ((UdpState)ar.AsyncState)); 

      UdpState state = new UdpState(); 
      state.endPoint = new IPEndPoint(IPAddress.Any, Port); 
      state.host = u; 
      u.BeginReceive(new AsyncCallback(DataReceiveCallback), ((UdpState)ar.AsyncState)); 
     } 

     /// <summary> 
     ///  . 
     /// </summary> 
     public void Initialize() 
     { 
      if (IsInitialized) 
      { 
       return; 
      } 
      //Debug.WriteLine(String.Format("Local address and port : {0}", Client.RemoteEndPoint.ToString())); 

      UdpState state = new UdpState(); 
      state.endPoint = new IPEndPoint(IPAddress.Any, Port); 
      state.host = this; 
      BeginReceive(new AsyncCallback(DataReceiveCallback), state); 

      IsInitialized = true; 
     } 

     #endregion 

    } 
} 

P.S.我希望问题清楚吗?我注意到我写的大部分问题都不清楚。 :/

回答

1
从System.Net.Security

SslStream类可能会做你需要什么

(SslStream)提供用于 客户端 - 服务器通信流使用 安全套接字层(SSL)安全 协议来认证服务器 和可选的客户端。

...

SSL协议有助于提供 机密性和完整性检查 使用的 SslStream传输的消息。当在客户端和服务器之间传递敏感的 信息时,使用由SslStream提供的诸如 的SSL连接应该是 。使用SslStream有助于 防止任何人阅读和 篡改信息,同时它是 在网络中传输

更多细节和例子在这里:SslStream Class

+0

谢谢:)我会仔细看看在一点。但真正快速,SSL可以用于UDP? (对不起,从未听说过ssl/udp组合:o) – Zack 2009-11-13 21:54:07

+0

嗯..我也不确定关于UDP :)因为SSL应该使用公共密钥加密来交换共享密钥,如果对话用于休息。我想这里的解决方案(再次,这只是一个想法,不知道它是否会工作:)可能是建立一个TCP连接的密钥交换,然后使用密钥密码来加密单个UDP数据包。 – 2009-11-13 22:39:28

相关问题