【问题标题】:Client-Server Data Encryption and Protocol Design客户端-服务器数据加密和协议设计
【发布时间】:2009-11-13 20:42:29
【问题描述】:

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

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

服务器背后的目标包括:

  • 以编程方式授予我们 IT 部门出于安全考虑已阻止的某些功能的访问权限(例如“net.exe”)
  • 授予对我的程序的访问权限,以监控学生在计算机实验室中查看的内容。

我想包括一些简单的问题来回发送:

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

我毫不怀疑我能够做到这一点。我目前最担心的是数据安全。在我的大学里,我们确实有一些“黑客”,他们可能知道如何嗅探数据包,并能够将数据包重新发送到特定服务器,以进行恶意操作或获取有关敌人的信息或诸如此类的东西。

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

与我交谈的一位朋友说我应该使用 bit packer,我开始将他的 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

    }
}

附:我希望问题很清楚?我注意到我写的大部分问题都不清楚。 :/

【问题讨论】:

    标签: c# encryption client-server bit-packing


    【解决方案1】:

    System.Net.Security 中的 SslStream 类可能会满足您的需求

    (SslStream) 提供用于 客户端-服务器通信使用 安全套接层 (SSL) 安全 验证服务器的协议 以及可选的客户端。

    ...

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

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

    【讨论】:

    • 谢谢 :) 稍后我会仔细看看。但是真的很快,SSL 可以与 UDP 一起使用吗? (抱歉,从未听说过 ssl/udp 组合:o)
    • hmm .. 我也不确定 UDP :) 因为 SSL 应该使用公钥加密来交换共享密钥,如果对话用于休息。我想这里的解决方案(这只是一个想法,不确定它是否会工作:) 可能是为密钥交换建立 TCP 连接,然后使用密钥密码来加密单个 UDP 数据包。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 2021-11-17
    • 1970-01-01
    相关资源
    最近更新 更多