gpt4 book ai didi

java rsa加密SSLSocket、android转java问题

转载 作者:行者123 更新时间:2023-12-01 06:04:29 25 4
gpt4 key购买 nike

我需要从 Android 手机通过 SSLSocket(不是 http/https)连接服务器。我首先生成带有私有(private)/公共(public)对的 jks keystore ,然后生成仅包含客户端公钥的 jks。然后我首先尝试从 java 到 java 设置 SSLSocketFactory,并且我让它工作了。Android 不接受我的 jks keystore ,因此我将其转换为 Android 端的 bks。但是,当我测试它时,服务器端抛出

javax.net.ssl.SSLHandshakeException: Received fatal alert: certificate_unknown

android端抛出一个

java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

这是测试服务器的代码:

System.setProperty("javax.net.ssl.keyStore","edkey.jks");
System.setProperty("javax.net.ssl.keyStorePassword","password");
ServerSocketFactory ssocketFactory = SSLServerSocketFactory.getDefault();
ServerSocket ssocket = ssocketFactory.createServerSocket(port);
System.out.println(ansiPurple("Starting"));
socket = ssocket.accept();
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
....

这里是工作java客户端的代码:

System.setProperty("javax.net.ssl.trustStore","edkey_public.jks");
System.setProperty("javax.net.ssl.trustStorePassword","password");
SSLSocketFactory f = (SSLSocketFactory)SSLSocketFactory.getDefault();
socket = (SSLSocket)f.createSocket(host, port);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
....

以下是无法运行的 Android 应用程序的代码:

InputStream ki = a.getResources().openRawResource(a.getResources().getIdentifier("raw/edkey_public", "raw", a.getPackageName()));
KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
KeyStore ks = KeyStore.getInstance("BKS");
ks.load(ki,"password".toCharArray());
kmfactory.init(ks, "password".toCharArray());
ki.close();
KeyManager[] keymanagers = kmfactory.getKeyManagers();
TrustManagerFactory tmf=TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ks);
TrustManager[] tms = tmf.getTrustManagers();
SSLContext sslContext=SSLContext.getInstance("TLSv1.2");
sslContext.init(keymanagers, tms, new SecureRandom());
SSLSocketFactory f=sslContext.getSocketFactory();
Socket socket = (SSLSocket)f.createSocket(host, port);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
....

有人知道问题出在哪里吗?

最佳答案

也许是因为在 Android 应用程序中你说

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

应该是

KeyStore ks = KeyStore.getInstance("JKS");

关于java rsa加密SSLSocket、android转java问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47794146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com