我目前正在尝试在我的程序中实现SSL连接。但是当我现在尝试启动它时,会在堆栈跟踪之上抛出NoSuchAlgorithmException。
这里是完整的堆栈跟踪:
java.net.SocketException: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: sun.security.ssl.SSLContextImpl$DefaultSSLContext)
at javax.net.ssl.DefaultSSLServerSocketFactory.throwException(SSLServerSocketFactory.java:159)
at javax.net.ssl.DefaultSSLServerSocketFactory.createServerSocket(SSLServerSocketFactory.java:171)
at net.dreamcode.im.server.IMServer.startServer(IMServer.java:155)
at net.dreamcode.im.server.IMServer.main(IMServer.java:66)
Caused by: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: sun.security.ssl.SSLContextImpl$DefaultSSLContext)
at java.security.Provider$Service.newInstance(Provider.java:1262)
at sun.security.jca.GetInstance.getInstance(GetInstance.java:236)
at sun.security.jca.GetInstance.getInstance(GetInstance.java:164)
at javax.net.ssl.SSLContext.getInstance(SSLContext.java:156)
at javax.net.ssl.SSLContext.getDefault(SSLContext.java:97)
at javax.net.ssl.SSLServerSocketFactory.getDefault(SSLServerSocketFactory.java:113)
at net.dreamcode.im.server.IMServer.startServer(IMServer.java:153)
... 1 more
Caused by: java.io.EOFException
at java.io.DataInputStream.readInt(DataInputStream.java:392)
at sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:645)
at sun.security.provider.JavaKeyStore$JKS.engineLoad(JavaKeyStore.java:55)
at java.security.KeyStore.load(KeyStore.java:1214)
at sun.security.ssl.SSLContextImpl$DefaultSSLContext.getDefaultKeyManager(SSLContextImpl.java:610)
at sun.security.ssl.SSLContextImpl$DefaultSSLContext.<init>(SSLContextImpl.java:495)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at java.lang.Class.newInstance(Class.java:374)
at java.security.Provider$Service.newInstance(Provider.java:1238)
... 7 more
所以,如果我们调查它,我们会看到这个EOFException - 这个含义很清楚。如果我打开密钥库文件,它是空的。但我不知道为什么。 以下是我尝试生成密钥库和证书的方法:
Scanner scr = new Scanner(System.in);
System.out.println("**** IMPORTANT ****");
System.out.println("You don't have a certificate for using SSL yet.");
System.out.println("We will create it together now!");
System.out.println();
System.out.print("Name of your organisation: ");
String ssl_o = scr.nextLine();
String ssl_ou = ssl_o;
System.out.print("Your name: ");
String ssl_cn = scr.nextLine();
System.out.print("Country code [de/en/fr]: ");
String ssl_c = scr.nextLine();
System.out.print("City: ");
String ssl_l = scr.nextLine();
System.out.print("State: ");
String ssl_st = scr.nextLine();
System.out.print("Password: ");
String pass = new String(scr.nextLine());
String execString = "keytool -genkey -keyalg RSA -alias dreamim -keystore " + KEYSTORE + " -storepass " + pass + " -validity 360 -dname \"cn=" + ssl_cn + ", o=" + ssl_o + ", ou=" + ssl_ou + ", c=" + ssl_c + ", l=" + ssl_l + ", st=" + ssl_st + "\" -keypass " + pass;
try
{
Runtime.getRuntime().exec(execString);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
然后,在尝试创建SSLServerSocket时,我只是使用它:
SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket sslserversocket = (SSLServerSocket) sslserversocketfactory.createServerSocket(port);
我没有通过传递密钥库参数来执行JAR文件,但我在创建套接字之前使用了这段代码:
System.setProperty("javax.net.ssl.keyStore", KEYSTORE);
System.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword);
我不知道自己做错了什么。我真的很感激任何帮助。谢谢!