Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 密钥库加载_Java_Ssl_Keystore - Fatal编程技术网

Java 密钥库加载

Java 密钥库加载,java,ssl,keystore,Java,Ssl,Keystore,我想知道以下内容在java中的等价物是什么: -Djavax.net.ssl.trustStore=keystore.jks -Djavax.net.ssl.keyStore=keystore.jks -Djavax.net.debug=ssl -Djavax.net.ssl.keyStorePassword=test JavaFile 我希望加载密钥库,而不是从命令行将其作为参数发送。我一直与: private TcpLink createSSL() { KeyStore k

我想知道以下内容在java中的等价物是什么:

-Djavax.net.ssl.trustStore=keystore.jks -Djavax.net.ssl.keyStore=keystore.jks
-Djavax.net.debug=ssl -Djavax.net.ssl.keyStorePassword=test JavaFile
我希望加载密钥库,而不是从命令行将其作为参数发送。我一直与:

private TcpLink createSSL() {
        KeyStore keyStore = null;
        TrustManagerFactory tmf = null;
        SSLContext ctx = null;
        SSLSocket socket = null;
        TcpLink smscLink = null;

        try {
            keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
            LOGGER.info("Got keystore");
            keyStore.load(new FileInputStream("/root/keystore.jks"), "test".toCharArray());
            LOGGER.info("Loaded keystore");
            tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            tmf.init(keyStore);
            LOGGER.info("Inited keystore");
            ctx = SSLContext.getInstance("TLSv1");
            ctx.init(null, tmf.getTrustManagers(), null);
            SSLSocketFactory factory = ctx.getSocketFactory();
            socket = (SSLSocket)factory.createSocket("100.100.201.189", 8807);
            LOGGER.info("Got socket");
            smscLink = new TcpLink(socket);

            return smscLink;

        } catch (KeyStoreException e) {
            LOGGER.error("Key store exception : " + e);
        } catch (NoSuchAlgorithmException e) {
            LOGGER.error("NoSuchAlgorithmException : " + e);
        } catch (CertificateException e) {
            LOGGER.error("CertificateException : " + e);
        } catch (FileNotFoundException e) {
            LOGGER.error("FileNotFoundException : " + e);
        } catch (IOException e) {
            LOGGER.error("FileNotFoundException : " + e);
        } catch (KeyManagementException e) {
            LOGGER.error("KeyManagementException : " + e);
        } catch (Exception e) {
            LOGGER.error("Exception : " + e);
        }
        return null;
    }
但我得到:

javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.checkEOF(SSLSocketImpl.java:1293)
        at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:65)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
欢迎任何意见


Thx

您可以通过以下方式设置系统属性:

System.setProperty("javax.net.ssl.keyStore", '/path/to/keystore.jks');
System.setProperty("javax.net.ssl.keyStorePassword", 'your-password-here');

它们将在系统范围内使用(在这个JVM实例中),因此您可能希望在初始化时使用它们。

它使用以下代码工作:

KeyManagerFactory kmf =  
    KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(this.getCertificateContent(), "test".toCharArray());
kmf.init(keyStore, "test".toCharArray());

TrustManagerFactory tmf =
    TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
SSLContext ctx = SSLContext.getInstance("TLSv1");
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
SSLSocketFactory factory = ctx.getSocketFactory();
socket = (SSLSocket)factory.createSocket("100.125.100.1", 8775);

如果需要,这里有一个API可以轻松创建SSLSocket和SSLServerSocket:

它不需要任何其他罐子。。。。只需获取文件并像以下那样使用它们:

SSLSocket s = SSLSocketKeystoreFactory.getSocketWithCert(ip, port, Main.class.getResourceAsStream("/mykey.jks"), "password")
或:


这更易于使用:)

由于以下原因导致:sun.security.validator.validator异常:PKIX路径生成失败:sun.security.provider.certpath.SunCertPathBuilderException:无法在sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:323)找到请求目标的有效证书路径在sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:217)上使用这些changes@Sergiu这是由信任库问题引起的。您的信任库不信任服务器证书。在您的情况下使用密钥库没有任何意义,只需使用
ctx.init(null,tmf.getTrustManagers(),null)
SSLServerSocket s = SSLServerSocketKeystoreFactory.getSocketWithCert(port, Main.class.getResourceAsStream("/mykey.jks"), "password")