1
    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(this.getClass().getResourceAsStream("keystore.jks"), "haslo1".toCharArray());
    TrustManagerFactory trustManagerFactory =
    TrustManagerFactory.getInstance("PKIX", "SunJSSE");
    trustManagerFactory.init(keyStore);
    X509TrustManager x509TrustManager = null;
    for (TrustManager trustManager : trustManagerFactory.getTrustManagers()) {
    if (trustManager instanceof X509TrustManager) {
    x509TrustManager = (X509TrustManager) trustManager;
    break;
    }
    }
        if (x509TrustManager == null) {
            throw new NullPointerException();
        }
        SSLContext sslContext = SSLContext.getInstance("TLS");

      sslContext.init( KeyManagerFactory.getInstance( "RSA" ).getKeyManagers(),
       new TrustManager[]{x509TrustManager}, null);

我有这段代码,但在 sslContext.init 线上我得到以下异常: java.security.NoSuchAlgorithmException: RSA KeyManagerFactory not available

有任何想法吗?

4

2 回答 2

5

代替

KeyManagerFactory.getInstance("RSA")

KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm())
于 2013-01-23T12:26:03.530 回答
5

KeyManagerFactory算法与RSA. Sun JSSE 提供程序文档(或提供程序的文档,如果您使用不同的文档)中记录了可以使用的内容。

这里唯一可用的选项是SunX509PKIX。一般来说,最好使用KeyManagerFactory.getDefaultAlgorithm(). 您也可以对TrustManagerFactory, 而不是硬编码执行此操作PKIX(此处列出了 Sun JRE 的默认值。)

于 2013-01-23T14:12:26.780 回答