安全HTTP
HTTPS是最流行的HTTP安全形式。
HTTPS方案的URL以https://
開頭
使用HTTPS時,所有的HTTP請求和響應數據在發送到網絡之前,都要進行加密。HTTPS在HTTP傳輸層下面提供了一個傳輸級的密碼安全層(可使用SSL或TLS)

數字加密
對稱密鑰加密技術
編碼和解碼使用的密鑰值一樣(密鑰k)
發送端和接收端共享相同的密鑰k才能進行通信。
缺點:發送者和接收者在互相對話前,一定要有一個共享的保密密鑰。
公開密鑰加密技術
使用兩個非對稱密鑰:一個對主機報文編碼,另一個對主機報文解碼。
編碼密鑰是公開的(所以叫公開密鑰加密),解碼密鑰是保密的,只有接收端才能對報文進行解碼。
RSA算法
公開密鑰非對稱加密系統的關鍵:
確保有人擁有下面所有的線索,也無法計算出保密的私有密鑰
- 公開密鑰(公開的,所有人都可獲得)
- 一小片攔截下來的密文
- 一條報文和與之相關的密文
RSA算法就是一個滿足所有這些條件的流行的公開密鑰加密系統。
數字簽名
數字簽名(digital signing):用加密系統對報文進行簽名,說明是誰編寫的報文,以證明報文未被篡改過。
數字簽名是附加在報文上的特殊加密校驗碼。
好處:
- 證明是作者編寫了這條報文
- 防止報文被篡改
通過非對稱公開密鑰技術產生。只有所有者知道其私有密鑰,可將私有密鑰作為指紋使用。
數字證書
數字證書(certs)中包含由某個受信任組織擔保的用戶或公司的相關信息。
上圖就是由DigiCert組織簽發給Github的數字證書
上圖是12306在Chrome瀏覽器上瀏覽,可看出雖然使用了HTTPS,但是是由不受信任的簽發者簽名的數字證書
用證書對服務器進行認證
通過HTTPS建立一個安全Web事務之后,瀏覽器自動獲取所連接服務器的數字證書。
HTTPS 細節介紹
HTTPS概述
HTTPS就是安全的傳輸層上發送的HTTP。在安全層對報文進行加密。
HTTP安全層是通過SSL或TLS
實現的。
HTTP的URL由
http://
起始,默認端口80,HTTPS的默認端口是443。
建立安全傳輸
HTTP:客戶端建立到服務器80端口的TCP連接,發送一條請求報文,接收一條響應報文,關閉連接。
HTTPS:客戶端打開一條道服務器443端口的連接,建立TCP連接,客戶端和服務器初始化SSL層,溝通加密參數,交換密鑰。SSL初始化完成后,客戶端就可將請求報文加密后發送給安全層。
SSL握手
發送加密的HTTP報文之前,客戶端和服務器要進行一次SSL握手。
下面是我用Java代碼實現的打開https://www.github.com和12306購票網站https://kyfw.12306.cn/otn/leftTicket/init的Demo代碼:
import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
* Created by JohnTsai on 16/2/23.
*/
public class HTTPSDemo {
/**
* 必須先進行SSL握手,再打開HttpsURLConnection
* http://stackoverflow.com/questions/9568100/exception-when-printing-the-server-certificate-details
*/
public static void openHttpsURL(String urlString) {
TrustManager[] trustManagers = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
};
try {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustManagers, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
} catch (NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
}
URL url = null;
try {
url = new URL(urlString);
//建立HTTPS connection
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.connect();
if(connection.getResponseCode()==200){
System.out.println(urlString+"打開成功");
}
printHttpsConnCert(connection);
printHttpsConn(connection);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void printHttpsConnCert(HttpsURLConnection conn) {
if (conn == null) return;
try {
//連接所使用的密碼程序
String cipherSuite = conn.getCipherSuite();
//服務器端的證書鏈
Certificate[] serverCertificates = conn.getServerCertificates();
System.out.println("密碼程序:" + cipherSuite);
for (Certificate certificate : serverCertificates) {
String type = certificate.getType();
PublicKey publicKey = certificate.getPublicKey();
System.out.println("證書類型:" + type + "\n" + "公鑰算法:" + publicKey.getAlgorithm()
+ "\n" + "公鑰:" + publicKey.getFormat());
}
} catch (SSLPeerUnverifiedException e) {
e.printStackTrace();
}
}
public static void printHttpsConn(HttpsURLConnection conn) {
if (conn == null) return;
try {
BufferedReader br =
new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String output = "";
while (br.readLine() != null) {
output += br.readLine();
}
br.close();
System.out.println(conn.getURL().getHost() + "的內容\n" + output);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
openHttpsURL("https://www.github.com");
openHttpsURL("https://kyfw.12306.cn/otn/leftTicket/init");
}
}
運行結果: