java 通过TCP\UDP 协议实现多人聊天,点对点,文件传送-----分服务器端和客户端 在此内容上添加发送消息有AES加密的代码详情
时间: 2024-04-01 07:35:08 浏览: 121
运用 java 语言 实现 TCP/udp 聊天程序
好的,您想了解关于Java通过TCP\UDP协议实现多人聊天、点对点、文件传送,并在此基础上添加发送消息有AES加密的代码详情,我可以为您提供一些基本的信息。
首先,TCP和UDP是两种不同的传输协议,分别对应于面向连接的可靠传输和面向无连接的不可靠传输。在使用这两种协议进行多人聊天、点对点、文件传送时,需要根据具体情况选择合适的协议。
下面以TCP协议为例,介绍如何实现多人聊天、点对点、文件传送,并在此基础上添加发送消息有AES加密的代码详情:
1. 服务器端
服务器端需要监听客户端的请求,建立连接,并处理接收到的消息。具体代码如下:
```java
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class TCPServer {
private static final String AES_ALGORITHM = "AES";
private static final String CHARSET_NAME = "UTF-8";
private static final int AES_KEY_SIZE = 128; // 128位密钥
private SecretKey secretKey;
public TCPServer() throws Exception {
// 生成AES密钥
KeyGenerator keyGen = KeyGenerator.getInstance(AES_ALGORITHM);
keyGen.init(AES_KEY_SIZE);
secretKey = keyGen.generateKey();
}
public void start(int port) throws IOException {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server started at port " + port);
while (true) {
Socket socket = serverSocket.accept();
new Thread(() -> {
try {
handleConnection(socket);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}
private void handleConnection(Socket socket) throws Exception {
System.out.println("Client connected: " + socket.getRemoteSocketAddress());
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
String message = new String(buffer, 0, len, CHARSET_NAME);
System.out.println("Received message from client: " + message);
String decryptedMessage = decryptMessage(message);
System.out.println("Decrypted message: " + decryptedMessage);
String response = "Server response: " + decryptedMessage;
String encryptedResponse = encryptMessage(response);
out.write(encryptedResponse.getBytes(CHARSET_NAME));
out.flush();
}
socket.close();
System.out.println("Client disconnected: " + socket.getRemoteSocketAddress());
}
private String encryptMessage(String message) throws Exception {
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedData = cipher.doFinal(message.getBytes(CHARSET_NAME));
return new String(encryptedData, CHARSET_NAME);
}
private String decryptMessage(String encryptedMessage) throws Exception {
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedData = cipher.doFinal(encryptedMessage.getBytes(CHARSET_NAME));
return new String(decryptedData, CHARSET_NAME);
}
public static void main(String[] args) throws Exception {
TCPServer server = new TCPServer();
server.start(12345);
}
}
```
在代码中,我们首先生成一个128位的AES密钥,并使用ServerSocket类监听客户端的请求。当有客户端连接时,我们开启一个新的线程来处理连接,并在该线程中读取客户端发送的消息,进行解密后给出响应。在解密和加密消息时,我们使用相同的AES密钥。
2. 客户端
客户端需要连接服务器,并发送消息。具体代码如下:
```java
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class TCPClient {
private static final String AES_ALGORITHM = "AES";
private static final String CHARSET_NAME = "UTF-8";
private static final int AES_KEY_SIZE = 128; // 128位密钥
private SecretKey secretKey;
public TCPClient() throws Exception {
// 生成AES密钥
KeyGenerator keyGen = KeyGenerator.getInstance(AES_ALGORITHM);
keyGen.init(AES_KEY_SIZE);
secretKey = keyGen.generateKey();
}
public void connect(String host, int port) throws IOException {
Socket socket = new Socket(host, port);
System.out.println("Connected to server: " + socket.getRemoteSocketAddress());
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
byte[] buffer = new byte[1024];
while (true) {
String message = "Hello, server!";
String encryptedMessage = encryptMessage(message);
out.write(encryptedMessage.getBytes(CHARSET_NAME));
out.flush();
int len = in.read(buffer);
String response = new String(buffer, 0, len, CHARSET_NAME);
String decryptedResponse = decryptMessage(response);
System.out.println("Received response from server: " + decryptedResponse);
Thread.sleep(1000);
}
}
private String encryptMessage(String message) throws Exception {
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedData = cipher.doFinal(message.getBytes(CHARSET_NAME));
return new String(encryptedData, CHARSET_NAME);
}
private String decryptMessage(String encryptedMessage) throws Exception {
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedData = cipher.doFinal(encryptedMessage.getBytes(CHARSET_NAME));
return new String(decryptedData, CHARSET_NAME);
}
public static void main(String[] args) throws Exception {
TCPClient client = new TCPClient();
client.connect("localhost", 12345);
}
}
```
在代码中,我们首先生成一个128位的AES密钥,并使用Socket类连接服务器。连接成功后,我们进入一个循环中,每隔一秒钟向服务器发送一条消息,并读取服务器的响应。在发送和接收消息时,我们使用相同的AES密钥。
以上代码只是简单示例,实际应用中还需要考虑一些细节问题,例如如何处理并发连接、如何发送和接收文件等等。同时,需要注意密钥的保密性,避免密钥被攻击者获取导致数据泄露。
阅读全文