问题描述
我想创建一个应用程序使用 SSLSocket
:客户端向服务器发送一个 String
,服务器将大写 String
并发送回客户端以供显示。
SSLServer
public class SSLServer {
public static void main(String args [])throws Exception
{
try {
// Creaet a SSLServersocket
SSLServerSocketFactory factory =(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
SSLServerSocket sslserversocket =(SSLServerSocket)factory.createServerSocket(1234);
//Tạo1đốitượngSockettừserversocketđlắngnghevàchấpnậnkếtnốitừclient
SSLSocket sslsocket =(SSLSocket)sslserversocket.accept();
// Tao cac luong de nhan va gui du lieu cua client
DataInputStream is = new DataInputStream(sslsocket.getInputStream());
PrintStream os = new PrintStream(sslsocket.getOutputStream());
while(true)// khi dang ket noi voi client
{
// Doc du lieu den
String input = is.readUTF();
String ketqua = input.toUpperCase();
// Du lieu tra ve
os.println(ketqua);
}
}
catch(IOException e)
{
System.out.print(e);
}
}
}
SSLClient
公共类SSLClient {
public static void main(String args [])
{
try
{
// Mo 1客户端套接字服务器voi so cong va dia chi xac dinh
SSLSocketFactory factory =(SSLSocketFactory)SSLSocketFactory.getDefault();
SSLSocket sslsocket =(SSLSocket)factory.createSocket(127.0.0.1,1234);
// Tao luong nhan va gui du lieu len server
DataOutputStream os = new DataOutputStream(sslsocket.getOutputStream());
DataInputStream = new DataInputStream(sslsocket.getInputStream());
// Gui du lieu len server
String str =helloworld;
os.writeBytes(str);
// Nhan du lieu da qua xu li tu server ve
String responseStr;
if((responseStr = is.readUTF())!= null)
{
System.out.println(responseStr);
}
os.close();
is.close();
sslsocket.close();
}
catch(UnknownHostException e)
{
System.out.println(e.getMessage());
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
运行时 SSLServer
。它显示以下错误:
javax.net.ssl.SSLException:没有可用的证书或密钥对应SSL的
密码套件已启用
我有搜索并做某些方法但是..你能帮助我吗?这将生成证书:
$ c> keytool -genkey -keystore yourKEYSTORE -keyalg RSA
输入yourPASSWORD,然后使用ssl启动服务器调试信息(将yourKEYSTORE放入SSLServer.class目录):
java -Djavax.net.ssl.keyStore = yourKEYSTORE -Djavax .net.ssl.keyStorePassword = yourPASSWORD -Djava.protocol.handler.pkgs = com.sun.net.ssl.internal.www.protocol -Djavax.net.debug = ssl SSLServer
比启动客户端(将yourKEYSTORE放入SSLClient.class目录):
java -Djavax.net.ssl.trustStore = yourKEYSTORE -Djavax.net。 ssl.trustStorePassword = yourPASSWORD SSLClient
I want to create an app use SSLSocket
: client send a String
to server and server will uppercase that String
and send back to client for display.
SSLServer
public class SSLServer {
public static void main(String args[]) throws Exception
{
try{
//Creaet a SSLServersocket
SSLServerSocketFactory factory=(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket sslserversocket=(SSLServerSocket) factory.createServerSocket(1234);
//Tạo 1 đối tượng Socket từ serversocket để lắng nghe và chấp nhận kết nối từ client
SSLSocket sslsocket=(SSLSocket) sslserversocket.accept();
//Tao cac luong de nhan va gui du lieu cua client
DataInputStream is=new DataInputStream(sslsocket.getInputStream());
PrintStream os=new PrintStream(sslsocket.getOutputStream());
while(true) //khi dang ket noi voi client
{
//Doc du lieu den
String input=is.readUTF();
String ketqua=input.toUpperCase();
//Du lieu tra ve
os.println(ketqua);
}
}
catch(IOException e)
{
System.out.print(e);
}
}
}
SSLClient
public class SSLClient {
public static void main(String args[])
{
try
{
//Mo 1 client socket den server voi so cong va dia chi xac dinh
SSLSocketFactory factory=(SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket=(SSLSocket) factory.createSocket("127.0.0.1",1234);
//Tao luong nhan va gui du lieu len server
DataOutputStream os=new DataOutputStream(sslsocket.getOutputStream());
DataInputStream is=new DataInputStream(sslsocket.getInputStream());
//Gui du lieu len server
String str="helloworld";
os.writeBytes(str);
//Nhan du lieu da qua xu li tu server ve
String responseStr;
if((responseStr=is.readUTF())!=null)
{
System.out.println(responseStr);
}
os.close();
is.close();
sslsocket.close();
}
catch(UnknownHostException e)
{
System.out.println(e.getMessage());
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
When run SSLServer
. It displays this error:
javax.net.ssl.SSLException: No available certificate or key corresponds
to the SSL cipher suites which are enabled
I have search and do some ways but.. Can you help me.
This will generate certificate:
keytool -genkey -keystore yourKEYSTORE -keyalg RSA
Enter yourPASSWORD and than start your server with ssl debug information(put yourKEYSTORE into directory with SSLServer.class):
java -Djavax.net.ssl.keyStore=yourKEYSTORE -Djavax.net.ssl.keyStorePassword=yourPASSWORD -Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol -Djavax.net.debug=ssl SSLServer
Than start your client(put yourKEYSTORE into directory with SSLClient.class):
java -Djavax.net.ssl.trustStore=yourKEYSTORE -Djavax.net.ssl.trustStorePassword=yourPASSWORD SSLClient
这篇关于使用SSLSocket Java创建应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!