C# 客户端 与 java 服务端做SSL通信
java报错:
Exception in thread "main" javax.net.ssl.SSLHandshakeException: no cipher suites in common
C#报错 :
System.Security.Authentication.AuthenticationException
java的server.jks是用的keytool生成的
C#的client.pfx也是用keytool生成并用keytool转换为pfx文件的
哪个牛人给看看怎么弄,似乎是加密的问题
java代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.KeyStore;
import java.security.Security;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManagerFactory;
import com.google.gson.Gson;
public class Server {
public static void main(String[] args) throws Exception {
// Set up key manager to do server authentication
char[] passphrase = "123456".toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(Server.class.getResourceAsStream("e:/server.jks"), passphrase);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, passphrase);
// Set up trust manager to do client authentication
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
// Set up SSL context
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
SSLServerSocketFactory sslServerSocketFactory = sslContext.getServerSocketFactory();
// Start server
ServerSocket serverSocket = sslServerSocketFactory.createServerSocket(8080);
System.out.println("Server started on port 8080");
// Gson gson = new Gson();
// String s = gson.toJson(ob);
while (true) {
// Accept client connections
SSLSocket socket = (SSLSocket) serverSocket.accept();
InetAddress address =socket.getInetAddress();
System.out.println("Client connected: " + address.getHostAddress());
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
// Receive message from client
String message = reader.readLine();
System.out.println("Message received: " + message);
// Send message back to client
writer.println("Message received: " + message);
writer.flush();
socket.close();
System.out.println("Client disconnected: " + socket.getInetAddress());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace sslsocket1
{
internal class SSLClient
{
public static void Main(string[] args)
{
int port = 8080;
// 创建 SSL 证书集合
X509CertificateCollection certCollection = new X509CertificateCollection();
certCollection.Add(new X509Certificate2("E:\\client.pfx", "123456"));
// 创建 SSL 流
TcpClient client = new TcpClient("localhost", port);
SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
try
{
// 客户端认证
sslStream.AuthenticateAsClient("localhost", certCollection, SslProtocols.Tls12, false);
Console.WriteLine("客户端已连接服务器");
}
catch (AuthenticationException e)
{
Console.WriteLine("Exception: {0}", e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
}
Console.WriteLine("Authentication failed - closing the connection.");
client.Close();
return;
}
// 向服务器发送消息
string message = "你好,服务器!";
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
sslStream.Write(messageBytes);
sslStream.Flush();
Console.WriteLine("已向服务器发送消息:" + message);
// 接收服务器的响应消息
byte[] buffer = new byte[2048];
int bytesRead;
while ((bytesRead = sslStream.Read(buffer, 0, buffer.Length)) > 0)
{
string response = Encoding.UTF8.GetString(buffer, 0, bytesRead);
Console.WriteLine("接收到服务器的响应消息:" + response);
}
// 关闭 SSL 流和客户端 socket
sslStream.Close();
client.Close();
}
private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
{
return true;
}
Console.WriteLine("SSL 证书验证失败:" + sslPolicyErrors);
return false;
}
}
}