java ssl连接失败_Java服务器和Android客户端之间的SSL连接失败

我正在尝试在Java主机和android客户端之间设置双向身份验证SSL连接.不知道为什么它没有连接.以下是Android客户端应用和Java服务器的代码.

客户代码:

private SSLContext createSSLContext(final Context cont){

SSLContext ssl_cont = null;

try {

Log.d(TAG, "TrustStore - Initializing");

KeyStore trustStore = KeyStore.getInstance("BKS");

TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());

InputStream trustStoreStream = cont.getResources().openRawResource(R.raw.myclienttruststore);

trustStore.load(trustStoreStream, "client".toCharArray());

trustManagerFactory.init(trustStore);

Log.d(TAG, "TrustStore - Initialized");

// Setup keystore

Log.d(TAG, "KeyStore - Initializing");

KeyStore keyStore = KeyStore.getInstance("BKS");

KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

InputStream keyStoreStream = cont.getResources().openRawResource(R.raw.myclient);

keyStore.load(keyStoreStream, "client".toCharArray());

keyManagerFactory.init(keyStore, "client".toCharArray());

Log.d(TAG, "KeyStore - Initialized");

ssl_cont = SSLContext.getInstance("TLS");

ssl_cont.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);

} catch (Exception e) {

// TODO Auto-generated catch block

alertbox("SSLClient", "ERROR: " + e.getMessage());

Log.d(TAG, "ERROR: " + e.getMessage());

}

return ssl_cont;

}

OnClickListener onConnClick = new OnClickListener() {

public void onClick(View arg0) {

// TODO Auto-generated method stub

try {

// Setup the SSL context to use the truststore and keystore

Log.d(TAG, "Started..");

SSLContext ssl_context = createSSLContext(cont);

Log.d(TAG,"here 1...");

SSLSocketFactory socketFactory = (SSLSocketFactory) ssl_context.getSocketFactory();

Log.d(TAG,"here 2...");

socket = (SSLSocket) socketFactory.createSocket(ipadd.getText().toString().trim(), Integer.parseInt(port.getText().toString().trim()));

Log.d(TAG,"here 3...");

dataOut = new DataOutputStream(socket.getOutputStream());

dataIn = new DataInputStream(socket.getInputStream());

dataOut.writeUTF("Hello !!");

msgin.setText("Connected");

Log.d(TAG, "Completed..");

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

msgin.setText("Not connected");

alertbox("Main", "ERROR: " + e.getMessage());

Log.d(TAG, "ERROR: " + e.getMessage());

}

}

};

服务器代码:

try {

mySSLServerFac = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();

mySSLServerSocket = (SSLServerSocket) mySSLServerFac.createServerSocket(9999);

System.out.println("Listening on 9999

");

mySSLSocket = (SSLSocket) mySSLServerSocket.accept();

DataInputStream input = new DataInputStream(mySSLSocket.getInputStream());

DataOutputStream output = new DataOutputStream(mySSLSocket.getOutputStream());

do{

System.out.println("Remote IP Address : " + mySSLSocket.getInetAddress());

msg = input.readUTF().toString();

System.out.println(msg);

java.util.Scanner sc = new java.util.Scanner(System.in);

output.writeUTF(sc.nextLine());

}while(msg != "exit");

System.out.println(msg);

} catch (Exception e) {

e.printStackTrace();

}

我在服务器上遇到“没有共同的密码套件”错误.由于我不在SSL连接设置中.如果您发现错误或主要问题,请允许我提供帮助.

这是我创建证书和信任库所遵循的link.我创建的Truststore和kestore是here

我正在使用Android 2.2和BKSProvider 1.46,请让我知道哪里出问题了.我必须尽快结束这个项目.

提前致谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值