Asymmetric encryption and decryption

1. Types of encryption algorithms

1 Key A
key, generally a string or number, is passed to the encryption or decryption algorithm during encryption or decryption, so that the algorithm can correctly encrypt the plaintext or decrypt the ciphertext.
2 Classification of encryption algorithms
There are two encryption algorithms in the world: symmetric cryptography and asymmetric cryptography.
In other words, you need to have two things when you want to perform encryption and decryption operations: secret keys and encryption and decryption algorithms.
2.1. Symmetric encryption
The characteristic of symmetric encryption algorithm is that the key used for encryption and the key used for decryption are the same. In other words, both encryption and decryption use the same key. Therefore, if the symmetric encryption algorithm is to ensure security, the key must be kept secret, so that it can only be known to the person who uses it and cannot be made public.
2.2. Asymmetric encryption
In the asymmetric encryption algorithm, there are two types of keys: public key and private key. Among them, the public key is public and does not need to be kept secret. The private key is held by the individual and must be kept properly and kept secret. Encryption and decryption use two different keys, which is why it gets its name. It is estimated that everyone has heard of RSA, which is a common and widely used asymmetric encryption algorithm.

2. Asymmetric encryption method
1 Principles of using public key and private key
① Each public key corresponds to a private key.
②In the key pair, what everyone knows is the public key. If you don't tell everyone, only you know it is the private key. ③If one of the keys is used to encrypt data, only the corresponding key can be decrypted.
④ If one of the keys can be used to decrypt the data, the data must be encrypted by the corresponding key.

The main application of asymmetric key cryptography is public key encryption and public key authentication.
2 Public key encryption and decryption
The purpose of encryption is to prevent a third party from seeing the communication content of the current two communication users.

2.1 Encryption
A (client) wants to send a piece of text to B (server), but does not want others to see it, so he wants to use asymmetric encryption to encrypt this piece of text. Of course, B needs to have a pair of public and private keys:
① B sends him
② A encrypts the text with the public key given to him by B, and then sends it to B
③ B uses his private key to decrypt the message sent by A. It should be emphasized that as long as B’s private key is not Leaked, this letter is safe, even if it falls into the hands of others, it cannot be decrypted.
Through these steps, B can successfully receive the information sent by A, and at the same time achieve the purpose of confidentiality.

2.2 Decryption
If B wants to return a message to A, it is much simpler:
① B encrypts the reply message with his own private key, and then transmits it to A
② A uses the public key that B gave him before to decrypt the message.

3. Public key authentication
The communication process described in 2 Public Key Encryption and Decryption seems simple, but think about this question: In process 2, how did A reply to him in the process of transmission, is there any modification? This involves the concept of digital signatures.

3.1 Digital signature (digital signature) The
official definition given by Microsoft: "digital signature" refers to an electronic security mark that can be added to a file. Use it to verify the issuer of the file and help verify whether the file has changed since it was digitally signed.

3.1.1 Principles of Digital Signature
To achieve this goal, it is generally to do a hash calculation on the information to obtain a hash value. Note that this process is irreversible, which means that the original information content cannot be obtained through the hash value. When sending the information, encrypt the hash value as a signature and send it out with the information. After receiving the information, the receiver will recalculate the hash value of the information and compare it with the hash value attached to the information (after decryption). If they are consistent, the content of the information has not been modified, because the hash calculation can guarantee Different content will definitely get different hash values, so as long as the content is modified, the hash value calculated based on the information content will change. Of course, malicious people can also modify the content of the information and also modify the hash value so that they can match. In order to prevent this, the hash value is generally encrypted (that is, signed) and then sent with the information.

3.1.2 How to use digital signatures
The following is an example to illustrate this process: When
B replies to A, it uses a digital signature method.
1. B first uses the hash function to generate a digest of the letter.
2. B uses its own private key. , Encrypt the digest, and generate a digital signature (signature)
3. B attaches the signature to the message to be replied, and sends it to A.
4. After A receives B’s message, remove the digital signature and pass it B’s public key decrypts to obtain the digest information of the letter.
5. A uses the hash function specified by B on the information sent by B, and compares the result with the digest obtained in the previous step. If the two are consistent, it means that B sent The information received has not been modified.

3.2
Does the issue of Digital Certificate end like this? Far from it, just imagine, although A has determined that the information that B returned to him has not been modified, how can I be sure that B is the one that returned the information to him? What if a malicious C secretly replaces the public key of B saved by A with his own, and sends a message to A in the name of B?
To solve this problem, A only needs to be able to determine whether the public key he holds is B's, which requires the use of digital certificates.
The digital certificate is used to verify the identity of the user to which the public key belongs. In daily life, if we want to verify a person's identity, the usual way is to check his ID. We trust the credibility of the ID card issuing agency, that is, the government agency, so as long as we verify that a person's ID is not forged, we believe that the person's identity is consistent with what is described on the ID card.
A digital certificate is the identity card of a person or organization in the online world, and its issuing authority is a certificate authority (CA). The CA signs the user's identity information (mainly the user name and the user's public key) with its private key, and the signature and the user's identity information together form a certificate.

3.2.1 The composition of a digital certificate
 The issuing authority of the certificate (Issuer)
points out which authority issued the certificate, that is, it points out which certificate authority (CA) issued the certificate (just creating the certificate, not referring to the certificate authority). The user of the certificate).
 The validity period of the certificate (Valid from, Valid to)
is the validity time of the certificate, or the period of use of the certificate. After the expiration date, the certificate will become invalid and cannot be used.
 Public key (Public key)
This we introduced when we introduced the public key cryptosystem. The public key is used to encrypt and decrypt messages and is a long string of numbers.
 Certificate owner (Subject)
Who is the certificate issued to, or the owner of the certificate, usually the name of a person or a company, the name of an organization, the URL of the company's website, etc.
 Signature algorithm
Refers to the encryption algorithm used for the digital signature of this digital certificate, so that the public key in the certificate of the certificate issuing authority can be used to decrypt the fingerprint according to this algorithm. The result of fingerprint encryption is digital signature
 Fingerprint and fingerprint algorithm (Thumbprint, Thumbprint algorithm)
are used to ensure the integrity of the certificate, that is, to ensure that the certificate has not been modified. The principle is that when issuing a certificate, the issuer calculates the hash value (fingerprint) of the entire certificate according to the fingerprint algorithm (a hash algorithm) and puts it together with the certificate. When the user opens the certificate, he also calculates the certificate's value according to the fingerprint algorithm. The hash value (fingerprint), if it matches the initial value, means that the certificate has not been modified, because after the content of the certificate is modified, the hash value (fingerprint) calculated based on the content of the certificate will change.

3.2.2 The generation process of digital certificate

1. Use the identity information of the digital certificate applicant to generate a digital signature

2. Combine the identity information of the certificate applicant and the digital signature to form a digital certificate

3.2.3 Principle
of digital certificate After having a digital certificate, A and want to communicate with B, they can obtain B's public key through B's digital certificate, so as to verify whether the public key in their hands is B or not. The process is as follows:
1. When B replies to A, he attaches his own digital certificate to the information.
2. After A receives B’s reply, he will take out the attached digital certificate and read the issuing authority in the certificate ( Issuer), and then look up the public key of the certificate issuing authority from the list of trusted certificate authorities in the operating system. If it is not found, it means that the certificate authority is untrusted. The information sent by B is of course unsafe.
3. Use the public key of the certificate authority obtained in the previous step to extract the digital certificate, and obtain the user information and digital signature that may be B.
4. A uses the encryption algorithm specified in the certificate to hash the user information that may be B
5. Compare the encrypted result with the digital signature decoded in the certificate. If they are the same, it means that the user information is indeed B, that is to say, the public key contained in the user information is indeed B.
This verifies B The authenticity of the identity.
The following figure shows this process vividly:

3.2.4 Nested verification of the public key of the certificate issuing center (CA)
There is an interesting problem here. User A uses the public key of the certificate authority to verify the digital certificate of user B, but if A and B use the certificate authority ( CA) What if it is different? Since the certificate authority can issue a certificate to the public key of the certificate authority through another higher-level certificate authority, a nested loop of public key certificates is formed, and the end of the loop is the root certificate authority. There are few root certificate authorities, and their public keys can be issued in a secure way, such as copying via USB or handing over written documents in person. With such dependence, A must start from the bottom of the tree structure of B's ​​CA, and query from the bottom CA to the upper CA until it finds a common trusted CA.
The whole process is shown in the figure below:

3.2.5 Certificate issuing authority
3.2.5.1 Who can become a certificate issuing authority
At this point, you may be thinking, can't we issue certificates ourselves? Must spend money to apply?
Of course not. We can also set up a certificate issuing agency, but we need to pass some security certifications, etc., which is just a bit troublesome. In addition, if the digital certificate is only to be used within the company, the company can generate a certificate for itself, and set this certificate as the certificate of the certificate issuing authority trusted by the operating system on all the company’s machines (see this sentence carefully. Around the mouth), so that the certificates issued by the company in the future can be verified on all machines within the company (when issuing certificates, set the Issuer (issuer) of these certificates to the Subject( of the certificate of our own certificate issuing agency) Subject) on it). But this is only limited to internal applications, because only our company’s own machine has set up a so-called certificate issuing authority that trusts us, and other machines do not trust our certificate issuing authority in advance, so on other machines, we issue The certificate cannot pass the security verification.

We can register a company to issue certificates to others, but it is obvious that our own company that specializes in issuing certificates will not be recognized by those international authorities. How do people know if you are a shit leather company? Therefore, in its operating system, Microsoft (or other operating system providers) will not trust our certificate issuing authority. When an application is checking the legality of a certificate, one can see that the issuing authority of the certificate is not owned by the operating system. A trusted publishing organization will throw an error message. That is to say, the certificate of our certificate issuing organization will not be pre-installed in the windows operating system, and our issuing organization will not be trusted.

3.2.5.2 Harm of untrusted certificate issuing authority
Why is it important for a certificate issuing authority to be untrusted? Let us give an example. Suppose we start a shit company to issue certificates for others, and I have a leg with Microsoft, and Microsoft has set me as a trusted certificate issuing authority in their operating system. Now if there is a small company called Hisunsray who spends 10 yuan to ask me to apply for a certificate for their company, and the company grows slowly, the scope of application of the certificate becomes wider and wider. Then there was a profiteer company Baidu who wanted to impersonate hisunsray, so he gave me ¥10000 and asked me to issue a certificate for them, but the name of the certificate (Subject) should write hisunsray. If I did this for ¥10000, I really gave the certificate. They, then they can use this certificate to impersonate hisunsray in the future.
If it is an excellent certificate issuing organization, for example, if you want to apply for a certificate named hisunsray, it will ask you to provide a lot of information to prove that you can represent the company hisunsray, which means he will go back to verify your identity. The certificate issuing authority is responsible for the certificate issued by it.

3.2.6 How to view digital certificates
Our operating system will pre-install the certificates of some certificate issuing organizations. We can add and delete certificates through the certificate manager. The following describes how to find them.

3.2.6.1 windows
start menu -> run, enter certmgr.msc, press enter

3.2.6.2Mac
open the keychain, select the system root certificate in the keychain, the certificate in the category, you can see the recipient certificate issuing authority list as shown in the figure below:

Of course, the above content only involves the most basic principles of asymmetric encryption methods. It must be a lot more complicated in actual use. If you are interested, you can study it privately.

Guess you like

Origin blog.csdn.net/wangrenhaioylj/article/details/113576305