在android中连接Https Socket时出错

时间:2016-05-16 10:29:52

标签: java android sockets

我在android应用程序中使用socket.io-client-java库作为socket但在应用程序中发生了一些错误,如下所示

代码: -

  KeyStore keyStore = KeyStore.getInstance("BKS");
        InputStream is = context.getResources().openRawResource(R.raw.bks);
        keyStore.load(is, "Password".toCharArray());
        is.close();


        //create a factory
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keyStore);

        //get context
        SSLContext mySSLContext = SSLContext.getInstance("TLS");

        //init context
        mySSLContext.init(
                null,
                trustManagerFactory.getTrustManagers(),
                new SecureRandom()
        );


        HostnameVerifier myHostnameVerifier = SSLSocketFactory.STRICT_HOSTNAME_VERIFIER;

        // set as an option
        IO.Options opts = new IO.Options();
        opts.sslContext = mySSLContext;
        opts.hostnameVerifier = myHostnameVerifier;
        socket = IO.socket("https://demo.in:9898", opts);
        socket.connect();

这是我的错误: -

java.io.IOException: KeyStore integrity check failed.
    at com.android.org.bouncycastle.jcajce.provider.keystore.bc.BcKeyStoreSpi.engineLoad(BcKeyStoreSpi.java:862)
    at java.security.KeyStore.load(KeyStore.java:590)
     at drawgraph.hospisoft.com.bluetootheds.SocketConnetion.ConnectSocket(SocketConnetion.java:46)
    at drawgraph.hospisoft.com.bluetootheds.MainActivity.initSocket(MainActivity.java:1095)
    at drawgraph.hospisoft.com.bluetootheds.MainActivity.onCreate(MainActivity.java:250)
    at android.app.Activity.performCreate(Activity.java:6100)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614)
    at android.app.ActivityThread.access$800(ActivityThread.java:178)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5643)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)

Github Link

我认为它的密码问题,但我多次检查BKS密码

2 个答案:

答案 0 :(得分:1)

密码错误或密钥库已损坏。

答案 1 :(得分:0)

对不起,迟到的答案,但代码下面的代码工作正常。

  try {

            String sslCertificateString= context.getResources().getString(R.string.certifiacte);
            InputStream certFile = new ByteArrayInputStream(sslCertificateString.getBytes(StandardCharsets.UTF_8));
            java.security.cert.Certificate ca = CertificateFactory.getInstance("X.509").generateCertificate(certFile);
            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
            keyStore.load(null, null);
            keyStore.setCertificateEntry("ca", ca);
            //create a factory
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init(keyStore);
            //get context
            SSLContext mySSLContext = SSLContext.getInstance("TLS");
            //init context
            mySSLContext.init(
                    null,
                    trustManagerFactory.getTrustManagers(),
                    new SecureRandom()
            );
            IO.Options opts = new IO.Options();
            opts.sslContext = mySSLContext;
            opts.secure = true;

            socket = IO.socket("https://demo.com:3000", opts); 
            socket.connect();


        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }