使用VC/JAVA/C#编程 实现简单的基于TCP或UDP的通信程序(控制台和视窗都可以对完成的socket通信进行安全性设置,4项任选2项实现(1.做用户验证(用户未注册或者密码不正确,就断开连接)2.加密信息内容3.超时机制4.黑名单机制)
时间: 2024-03-07 13:48:42 浏览: 236
以下是使用C#语言和Visual Studio开发环境实现基于TCP的通信程序并对通信进行安全性设置的代码示例,其中实现了用户验证和加密信息内容两项安全性设置。
Server端代码:
```csharp
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace SocketServer
{
class Program
{
static void Main(string[] args)
{
try
{
// 创建一个TCP/IP socket
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// 绑定IP地址和端口号
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 8888);
serverSocket.Bind(ipEndPoint);
// 开始监听
serverSocket.Listen(10);
Console.WriteLine("Server is running...");
while (true)
{
// 接收客户端连接
Socket clientSocket = serverSocket.Accept();
// 用户验证
if (!AuthenticateUser(clientSocket))
{
Console.WriteLine("User authentication failed, disconnecting...");
clientSocket.Close();
continue;
}
// 加密信息内容
string message = "Welcome to the server.";
byte[] encryptedMessage = EncryptMessage(message);
// 发送加密后的信息给客户端
clientSocket.Send(encryptedMessage);
// 关闭连接
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
// 用户验证
static bool AuthenticateUser(Socket clientSocket)
{
// 从客户端接收用户名和密码
byte[] buffer = new byte[1024];
int bytesReceived = clientSocket.Receive(buffer);
string credentials = Encoding.ASCII.GetString(buffer, 0, bytesReceived);
// 验证用户名和密码是否正确
if (credentials == "username:password")
{
return true;
}
else
{
return false;
}
}
// 加密信息内容
static byte[] EncryptMessage(string message)
{
// 使用AES算法加密信息
byte[] key = Encoding.ASCII.GetBytes("1234567890123456");
byte[] iv = Encoding.ASCII.GetBytes("1234567890123456");
byte[] messageBytes = Encoding.ASCII.GetBytes(message);
using (Aes aes = Aes.Create())
{
aes.Key = key;
aes.IV = iv;
// 创建加密器
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
// 加密信息
byte[] encryptedMessage = encryptor.TransformFinalBlock(messageBytes, 0, messageBytes.Length);
return encryptedMessage;
}
}
}
}
```
Client端代码:
```csharp
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace SocketClient
{
class Program
{
static void Main(string[] args)
{
try
{
// 创建一个TCP/IP socket
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// 连接到服务器
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 8888);
clientSocket.Connect(ipEndPoint);
// 发送用户名和密码进行验证
string credentials = "username:password";
byte[] buffer = Encoding.ASCII.GetBytes(credentials);
clientSocket.Send(buffer);
// 接收加密后的信息
buffer = new byte[1024];
int bytesReceived = clientSocket.Receive(buffer);
string encryptedMessage = Encoding.ASCII.GetString(buffer, 0, bytesReceived);
// 解密信息
string message = DecryptMessage(encryptedMessage);
Console.WriteLine(message);
// 关闭连接
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
// 解密信息内容
static string DecryptMessage(string encryptedMessage)
{
// 使用AES算法解密信息
byte[] key = Encoding.ASCII.GetBytes("1234567890123456");
byte[] iv = Encoding.ASCII.GetBytes("1234567890123456");
byte[] encryptedMessageBytes = Encoding.ASCII.GetBytes(encryptedMessage);
using (Aes aes = Aes.Create())
{
aes.Key = key;
aes.IV = iv;
// 创建解密器
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
// 解密信息
byte[] messageBytes = decryptor.TransformFinalBlock(encryptedMessageBytes, 0, encryptedMessageBytes.Length);
string message = Encoding.ASCII.GetString(messageBytes);
return message;
}
}
}
}
```
在上述代码中,我们使用了C#中的AES算法对信息进行加密和解密。同时,我们还实现了用户验证功能,当用户名和密码不正确时,会断开连接。具体实现方法是在服务器端接收客户端发送的用户名和密码,验证成功后才会进行通信。
阅读全文