Click on thePictures for the last two years burst of good articles  

This article will discuss the encryption and decryption principle of HTTPS. Many people know RSA, think HTTPS=RSA, use RSA encryption and decryption data, but actually this is not correct.

HTTPS uses RSA to authenticate and exchange keys, and then uses the exchanged keys to encrypt and decrypt data.

Authentication is asymmetric encryption using RSA, while data transfer is symmetric encryption using the same key for both parties. So, what are symmetric and asymmetric encryption?

Symmetric and asymmetric encryption

Let’s say xiao Wang next door wants to ask Xiao Hong out, but he doesn’t want xiao Ming to know, so he wants to use symmetric encryption to pass a small note to Xiao Hong.

The data he wants to send is “Meet at 5:00 PM” (utF-8 if it’s Chinese), encrypted by moving left or right directly in the ASCII table.

His key is 3, which means that if you move 3 bits further in the ASCII table, it will become “PHW #dw#8=33#SP”, so that the average person will not know what it means if they intercept it.

But think about it. If he can intercept your data, he can also intercept your key and decrypt it.

Therefore, Wang intends to use asymmetric encryption. The characteristic of asymmetric encryption is that both parties have their own public key and private key pair, and the public key is sent to each other, but the key is not exchanged and kept by themselves.

public_key = (N, e) = (3233, 17)Copy the code

She sent xiao Ming the public key. Her own private key is:

private_key = (N, e) = (3233, 2753)Copy the code

The first letter he wants to send is t= “M”, the ASCII encoding of “M” is 77, the encryption process of 77 is calculated as follows:

T = 77 ^ e  % N = 77 ^ 17 % 3233 = 3123Copy the code

After receiving T, Xiao Hong decrypts it with her private key. The calculation is as follows:

t = T ^ e % N = 3123 ^ 2753 % 3233 = 77Copy the code

HTTPS

HTTPS connection establishment process

  • Verify the identity of the service provider, such as when I visit Google.com I am connected to Google servers

  • Prevent data hijacking, such as carriers inserting ads into HTTP pages

  • Prevent sensitive data from being stolen or tampered with

As openSSL notes, this is the only way to prevent man-in-the-middle attacks:

As shown below:


The service side

In Client Hello, the Client will inform the server of its current information, as shown in the following figure:

The Server does something in Server Hello:

  • Key exchange uses ECDHE

  • Certificate signature algorithm RSA

  • Data encryption using AES 128 GCM

  • Signature verification uses SHA256

The service then sends the client four certificates:

To see what can be inside a certificate, we can expand the first certificate, as shown below:

  • TbsCertificate (to be signed certificate) Specifies the content of the certificate to be signed

  • Certificate signature algorithm

  • Signed by CA

The authentication

Let’s take a look at the contents of tbsCertificate, as shown below:

Amazon certificate also has the above structure, we can copy the public key of Amazon certificate, as shown below:

We then decrypt the mozilla.org certificate signature using the CA’s public key in a similar way:

To manually calculate the SHA256 hash of tbsCertificate, use Wireshark to export tbsCertificate to a raw binary file:

Then use OpenSSL to calculate its hash value, as shown below:

liyinchengs-MBP:https liyincheng$ openssl dgst -sha256 ~/tbsCertificate.binSHA256(/Users/liyincheng/tbsCertificate.bin)=  5e300091593a10b944051512d39114d56909dc9a504e55cfa2e2984a883a827dCopy the code

And the fourth certificate is the root certificate, which is built into the operating system (viewed through the Mac keychain tool) :

For example, you can make a phone call or send an email to tell the server whether the signature is the same as that of the certificate calculated by yourself. If the signature is the same, the certificate has not been tampered with (for example, the public key of the certificate has not been changed to the public key of the Hacker) :

Key exchange

Using ECDHE is a more secure key exchange algorithm. As shown in the figure below, both parties exchange keys through ECDHE:

The idea of this algorithm is shown below:

Elliptic curve encryption

There are two signature algorithms for certificates: RSA and EC. As shown below, google.com is the ECC certificate used:

As shown in the figure below, there is an elliptic curve equation:

y ^ 3 = x ^ 2 + ax + b:Copy the code

To calculate the coordinates of 3G, see the following figure:

The difficulty of EC lies in the given starting point G and point K:

K = kGCopy the code

Send these two points to the other party as encrypted data, and the other party uses the private key K to decrypt the data after receiving it. The process is as follows:

M = C2 - rK = C2 - rkG = C2 - rkG = C2 - kC1Copy the code

ECC Key Exchange

The principle is simple, as shown below:

The Curve equation is specified. For example, Curve X25519 uses:

y^2 = x^3 + 486662x^2 + xCopy the code

The curve equation to be used is specified in the key exchange, as shown below:

HTTPS certificate application


There are three types of certificates:

The EV certificate will display the enterprise name of the certificate in the address bar of the browser:

But the new version of Chrome seems to have removed this, so when we open Medium’s console, we see a prompt:

As part of an experiment, Chrome temporarily shows only the lock icon in the address bar. Your SSL certificate with Extended Validation is still valid.

Alternatively, we can use OpenSSL to generate a self-signed certificate by executing the following command:

openssl req -x509 -nodes -sha256 -days 365 -newkey rsa:2048 -keyout test.com.key -out test.com.crtCopy the code

Test.com.crt is the certificate and test.com.key is the private key of the certificate, as shown below:

Then give Nginx access to both files using HTTPS, as shown in the following code:

server {        listen       443;        server_name  test.com;        ssl on;        ssl_certificate    test.com.crt;        ssl_certificate_key    test.com.key;     }Copy the code

Client Certificate

The signature verification algorithm is the same as the TLS certificate discussed above. Why do executables need to be signed? Because if they are not signed, the system will block the installation or run, as the Mac double-clicks an unsigned DMG package:

Windows will give you a warning:

And when we run a signed exe file there will be a normal prompt, like Chrome’s prompt:

Author: Li Yincheng

Source: https://zhuanlan.zhihu.com/p/75461564

The latest 2TB technology dry goods: including architect practice course, big data, Docker container, system operation and maintenance, database, Redis, MogoDB, e-book, Java foundation course, Java practice project, ELK Stack, machine learning, BAT interview intensive video, etc. To obtain all the information, simply reply to the dialog box of the wechat public account “Road of Migrant Workers and Brother Technology” with the keyword: 1024.

Being is the END being

Excellent article recommendation:


National programmer salary in April 2020!

Versatile terminal artifact! Easy to use, free!

Open individual tax App: unexpectedly to pay tax more than 20,000…

5 s! Build a Linux system out of the box in a browser

10 tips Redis uses

Ten LINUX commands you probably haven’t used

A job! On the hundreds of millions (MySQL) large table optimization….

Click [read article] to find out more

Click a look, forward to support it ↓↓ ↓