如何实现简单的安全套接字连接

How to implement an easy secure socket connection

我正在用 C# 编写一个简单的客户端-服务器游戏。我的原型适用于套接字编程。我使用 socket class 和 SocketAsyncEventArgs 来异步我的代码。现在对于我喜欢的 alpha 版本(必须)创建安全连接。但我没有任何关于安全和 SSL 和 TLS 的背景知识。

为此我找到了 class:sslstream。但是 .Net 文档对我这个初学者来说是无法理解的。另一个问题是 SocketAsyncEventArgs 不适用于 TCPClient Class.

这样的流

所以谁能指导我如何使用套接字实现简单的安全连接?有两个要求:
1- 它应该是快速低延迟的,我很欣赏它支持非阻塞(异步)模式。
2- 它应该支持非常小的消息。我的游戏命令平均为 10 个字节。我不喜欢加密命令变成128字节!

因为我的服务器不是public,只有我的客户连接到它,所以我不喜欢注册和购买证书。

请提供分步回答。首先如何创建一些密钥(Public- 私有...我不知道)其次如何验证客户端,第三如何安全通信。

衷心感谢您阅读此问题直至结束,之前感谢您的回答。此致!

Google是你的朋友:
https://www.codeguru.com/columns/dotnet/using-secure-sockets-in-.net.html

(*) 强烈不建议使用自签名证书。不要在生产中使用自签名证书。

对于像我这样不熟悉密码学的人来说,在 Microsoft 文档中创建安全方案是一个很好的起点。 .NET Framework Cryptography Model

Creating a cryptographic scheme is not a trivial task! (Source)

但本教程是关于实现基本安全系统的基础material。

编辑:微软已经删除了关于创建加密方案的页面,所以我为你粘贴了一个google chached版本:

Creating a Cryptographic Scheme

The cryptographic components of the .NET Framework can be combined to create different schemes to encrypt and decrypt data.

A simple cryptographic scheme for encrypting and decrypting data might specify the following steps:

  • Each party generates a public/private key pair.

  • The parties exchange their public keys.

  • Each party generates a secret key for TripleDES encryption, for example, and encrypts the newly created key using the other's public key.

  • Each party sends the data to the other and combines the other's secret key with its own, in a particular order, to create a new secret key.

  • The parties then initiate a conversation using symmetric encryption.

Creating a cryptographic scheme is not a trivial task.