HTTPS - 网络请求和响应是否被加密?

3

我正在从安卓应用程序中向HTTPS服务器进行webservice调用。下面是代码片段,借助它我能够成功进行webservice调用并获取响应。

我的问题是,在向HTTPS服务器进行调用之前,我们需要执行任何附加步骤来加密数据吗? 因为从安卓分析器中,我可以看到所有的Web请求都以明文格式呈现。据我所知,请求在进行HTTPS调用之前会被加密。

enter image description here

     public static WebServiceResp makeWebServiceCall(String XML, String urlPath) throws IOException{
    //Code to make a web service HTTP request
    String responseString = "";
    String outputString = "";
    String wsURL = urlPath;
    URL url = new URL(wsURL);
    URLConnection connection = url.openConnection();
    HttpsURLConnection httpConn = (HttpsURLConnection)connection;
    ByteArrayOutputStream bout = new ByteArrayOutputStream();


    //System.out.println(XML);

    byte[] buffer = new byte[XML.length()];
    buffer = XML.getBytes();
    bout.write(buffer);
    byte[] b = bout.toByteArray();
    // Set the appropriate HTTP parameters.
    httpConn.setRequestProperty("Content-Length",
                                String.valueOf(b.length));
    httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
    httpConn.setRequestMethod("POST");
    httpConn.setRequestProperty("Cache-Control", "no-cache");

    httpConn.setDoOutput(true);
    httpConn.setDoInput(true);


    OutputStream out = httpConn.getOutputStream();
    //Write the content of the request to the outputstream of the HTTP Connection.
    out.write(b);
    out.close();
    //Ready with sending the request.

    //Check the status
    int status = httpConn.getResponseCode();
    Log.d(TAG, "makeWebServiceCall: "+"Processing Status: "+status);


    BufferedReader in;
    if (status <= 200) {
        //Read the response.
        Log.d(TAG, "makeWebServiceCall: Getting Input Stream");
        InputStreamReader isr =
                new InputStreamReader(httpConn.getInputStream());
        in = new BufferedReader(isr);
    }else{
        //Read the response.
        Log.d(TAG, "makeWebServiceCall: Getting Error Stream");
        InputStreamReader isr =
                new InputStreamReader(httpConn.getErrorStream());
        in = new BufferedReader(isr);
    }

    //Write the SOAP message response to a String.
    while ((responseString = in.readLine()) != null) {
        outputString = outputString + responseString;
    }
        Log.d(TAG, "makeWebServiceCall: WebServiceResponse " + outputString);
        //Parse the String output to a org.w3c.dom.Document and be able to reach every node with the org.w3c.dom API.
        Document document = Utils.parseXmlFile(outputString);
        //NodeList nodeLst = document.getElementsByTagName("GetWeatherResult");
        // String weatherResult = nodeLst.item(0).getTextContent();
        //System.out.println("Weather: " + weatherResult);

        //Write the SOAP message formatted to the console.

    WebServiceResp webServiceResp = new WebServiceResp();
    webServiceResp.setDocument(document);
    webServiceResp.setStatus(status);
    return webServiceResp;

}
4个回答

3

不需要。如果你将数据发送到一个https网站,加密是协议的一部分。你不需要做任何额外的工作。


但是从Android Studio可以看到我的应用程序发出的所有请求,以纯XML格式呈现?这是Android Studio能够在移动设备连接调试模式时解密的内容吗? - Maddy
1
在Android Studio中的哪里?你在看什么?直到它被发送到操作系统的套接字子系统深处的HTTP堆栈中,它才会被加密。 - Gabe Sechan
添加了解密后的网页请求的图像,可以以纯文本形式查看。 - Maddy
3
是的,这是在加密之前/解密之后的堆栈较高层次上进行的。如果您在路由器上嗅探流量,它将会是被加密的。 - Gabe Sechan

1
不。您看到的加密是在网络层上进行的。发起https调用的客户端会看到发送和接收的内容。这就是https的工作原理。
当您查看Chrome浏览器的网络选项卡时,您可以看到发送和接收的内容。现在这不是一个安全问题,https更多地是关于您采取措施使得任何人都无法窃听您的数据。
现在,如果您仍然想要额外的安全级别,您可以使用证书固定。

https://www.owasp.org/index.php/Certificate_and_Public_Key_Pinning

https://medium.com/@appmattus/android-security-ssl-pinning-1db8acb6621e

如何从MainActivity添加到network_security_config

因此,在这种技术中,您基本上说您期望的证书哈希值应具有此内容。然后,即使某人在系统上使用受信任的代理和受信任的CA,为给定域生成有效证书后,连接也将无法建立。


1

HTTPS对您的应用程序是透明的,所有的魔术都发生在传输层之间(因此它被称为“传输层安全”),您可以想象旧时代的加密电报,将军们用明文告诉电报员消息,而电报员则以加密形式发送它们(可能使用某种代码本),任何没有相同代码本的人都不能轻松解密消息,而任何使用电报的人都不关心代码本(甚至不知道它的存在,除了传输层两侧的电报员)。


1

加密/解密由操作系统提供的内置网络客户端模块完成,因此您无需担心。

您可以使用一些客户端工具查看纯文本,因为它们清楚地知道自己正在发送/接收什么。例如,Chrome开发者工具。(实际上,它们也不关心加密/解密)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接