2009-11-13 141 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

相關問題