【问题标题】:Do I need private certificate doing https in Android java我需要在 Android java 中做 https 的私有证书吗
【发布时间】:2012-02-22 01:57:04
【问题描述】:

我需要从与我的 Android 应用程序进行 https 通信的服务器中检索数据。 我像这样动态获取服务器证书:

KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
store.load(null);
URLConnection uc = new URL("https://duckduckgo.com/").openConnection();
uc.connect();
Certificate certs[] = ((HttpsURLConnection) uc).getServerCertificates();
for (int i = 0; i < certs.length; i++) store.setCertificateEntry("cert_"+ i, certs[i]);
((HttpsURLConnection) uc).disconnect();

我有一个自定义的 HttpClient:

public class MyHttpClient extends DefaultHttpClient {
    final KeyStore store;

    public MyHttpClient(KeyStore store) {
        this.store = store;
    }

    @Override
    protected ClientConnectionManager createClientConnectionManager() {
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory
                .getSocketFactory(), 80));
        registry.register(new Scheme("https", newSslSocketFactory(), 443));
        return new SingleClientConnManager(getParams(), registry);
    }

    private SSLSocketFactory newSslSocketFactory() {
        try {
            return new SSLSocketFactory(store);
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }
}

并像这样从服务器检索数据:

HttpEntity responseEntity;
URI url = new URI("https://duckduckgo.com/?q=android+java");
HttpGet httpGet = new HttpGet(url );
HttpClient client = new MyHttpClient(store);
httpGet.addHeader("Content-Type", "text/xml");
HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000);
HttpResponse getResponse = client.execute(httpGet);

我发送和接收的所有内容都加密了吗?

我在想也许我必须向服务器提供我的公钥?

【问题讨论】:

  • 没有“私人证书”之类的东西。获取服务器证书并将它们存储到信任库中将不起作用,除非它们已经被信任库信任,即存在于信任库中,这是循环的。不清楚你在问什么。

标签: java android https certificate private


【解决方案1】:

我在想也许我必须向服务器提供我的公钥?

您可能指的是Public Key Cryptography,如果是这种情况,那么是的,另一方需要您的公钥才能(为您)加密响应。

但要回答您的问题:是的,您发送和接收的所有内容(在此上下文中)都是加密的。在duckduckgo.com 的情况下,您正在与已验证其 SSL 证书的公共网络服务器通信,因此一切都应该没问题。

另请参阅this 答案,简要了解证书颁发机构和 SSL 的实际工作原理。

【讨论】:

    【解决方案2】:

    简短回答:不,您不需要证书就可以安全地与服务器通信。

    长答案:证书的全部意义在于客户端和服务器可以就私有加密密钥达成一致,而无需其他人知道该密钥是什么。服务器证书将由一些主要的证书颁发机构(Verisign 等)颁发。您可以检查权威机构以确保他们确实已为相关域名颁发了证书。一旦您根据授权验证了证书,您就可以使用证书中的公共加密密钥,并且只有证书的所有者才能解密您加密的任何内容。在 SSL 的情况下,您使用证书上的公钥来加密会话加密密钥,您将使用该密钥与服务器进行安全通信。

    在做这些事情时不要忽略在使用之前验证证书!!!

    【讨论】:

    • 我打算省略验证,因为我自己将完全控制服务器。通过动态获取证书,我不需要在手机的密钥库中预加载证书。证书的唯一要点(如我所见)是确保加密
    • 客户端不生成会话密钥;不加密;并且不发送。它通过密钥协议协议进行协商。
    猜你喜欢
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2016-11-02
    相关资源
    最近更新 更多