java实现服务端客户端使用数字信封进行加密通信代码
时间: 2023-12-31 15:05:46 浏览: 181
以下是Java实现服务端客户端使用数字信封进行加密通信的代码:
服务端代码:
```java
import java.io.*;
import java.net.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class Server {
public static void main(String[] args) throws Exception {
int port = 12345;
// 生成RSA密钥对
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
// 等待客户端连接
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("等待客户端连接...");
Socket socket = serverSocket.accept();
System.out.println("客户端已连接");
// 发送公钥给客户端
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(publicKey);
// 接收客户端发来的数字信封
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
SealedObject sealedObject = (SealedObject)in.readObject();
// 解密数字信封
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.UNWRAP_MODE, privateKey);
Key sessionKey = (Key)cipher.unwrap(sealedObject.getEncryptedKey(), "AES", Cipher.SECRET_KEY);
// 使用会话密钥进行加密通信
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, sessionKey, new IvParameterSpec(new byte[16]));
DataInputStream dis = new DataInputStream(socket.getInputStream());
while (true) {
String message = dis.readUTF();
byte[] encryptedMessage = Base64.getDecoder().decode(message);
byte[] decryptedMessage = cipher.doFinal(encryptedMessage);
System.out.println("收到消息:" + new String(decryptedMessage));
}
}
}
```
客户端代码:
```java
import java.io.*;
import java.net.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class Client {
public static void main(String[] args) throws Exception {
String host = "localhost";
int port = 12345;
// 连接服务端
Socket socket = new Socket(host, port);
System.out.println("已连接服务器");
// 接收服务端公钥
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
PublicKey publicKey = (PublicKey)in.readObject();
// 使用公钥加密会话密钥,并将数字信封发送给服务端
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.WRAP_MODE, publicKey);
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
Key sessionKey = keyGen.generateKey();
SealedObject sealedObject = new SealedObject(sessionKey, cipher);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(sealedObject);
// 使用会话密钥进行加密通信
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, sessionKey, new IvParameterSpec(new byte[16]));
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String message = br.readLine();
byte[] encryptedMessage = cipher.doFinal(message.getBytes());
String base64EncodedMessage = Base64.getEncoder().encodeToString(encryptedMessage);
dos.writeUTF(base64EncodedMessage);
dos.flush();
}
}
}
```
注意:以上代码仅供参考,实际应用中需要进行适当的安全性改进,比如使用更长的密钥、使用更安全的加密算法等。
阅读全文