A little bit confused about the public key and the private key, search for some information, do some sorting it, first look at this:
Encryption--Public key
Decryption--Private key
Signature--Private key
Authentication--Public key
Look at this. The private key may be used for signing, in fact, it is equivalent to the private key encryption, and public key authentication is to use the public key decryption, but not for confidential information. Because the public key is public.
One more:
1) The public and private keys appear in pairs
2) The public key is called the public key, and only the private key is known
3) data encrypted with the public key can only be decrypted by the corresponding private key
4) data encrypted with the private key can only be decrypted by the corresponding public key
5) If the public key can be decrypted, it must be the corresponding private key plus the secret
6) If the private key can be decrypted, it must be the corresponding public key plus the secret
Both the public key and the private key can be used to encrypt or decrypt---as long as it is guaranteed to be encrypted with a, B decryption is done. As to whether a is a public key or a private key, it can be used for different purposes.
One, public key encryption, private key decryption--for confidential information
If you want to send a message to someone secretly, you can encrypt it with his public key. Because only he knew his private key, so the news is only he himself can untie, so you have achieved your purpose.
Second, private key encryption, public key decryption--for digital signature
Strictly speaking, the private key encryption is to use the private key to encrypt the digest, the receiver can be decrypted with the public key, the decryption is successful, the sender of the verifiable information is the owner of the private key. Because the public key is public, it cannot play the role of confidential information.
If you want to post an announcement, you need a means to prove that it's really you, not someone else's imposter. Then you can attach a piece of content that is encrypted with your private key at the beginning or end of your announcement (for example, a paragraph of your announcement text), and all others can use your public key to decrypt it to see if the solution is consistent. If so, it means that the announcement is really your---because only your public key can unlock the contents of your private key encryption, while others cannot get your private key.
But this is only the first part of the digital signature: prove that you sent the news. The digital signature also has the second part: to prove that the message content is indeed complete---that is, without any form of tampering (including replacement, missing, new).
To achieve the second part of the digital signature, you need to do a hash of your announcement (MD5 or SHA1), and then use your private key to encrypt the hash as a signature and publish it together. When someone receives your announcement, he can use your public key to decrypt your signature, if the decryption is successful, and the decrypted hash value is indeed consistent with the original text of your announcement, then he proved two points: the news is really you sent, and the content is complete.
Third, the public key authentication-digital certificate
Hackers can replace your public key, and then use his private key to do a digital signature to send you messages, and you use the hacker forged public key can be successfully verified, you will mistakenly think the source has not changed.
In this case, the CA (Certificate Center Certificate Authority) is required to authenticate the public key. The certificate center uses its own private key to encrypt the public key of the sender of the message along with some related information, and generates a digital certificate (Certificate).
This can refer to this article: what is a digital signature? , it's very good, the diagram is clear.
In the actual use, the public key will not appear alone, always appear as a digital certificate to ensure the security and effectiveness of the public key.
Symmetric and asymmetric algorithms
Symmetric algorithm is that the encryption process and decryption process is symmetric, with a key encryption, you can use the same key to decrypt. An algorithm that uses a public private key is a non-symmetric encryption algorithm.
HTTPS generally uses the following algorithms, which include asymmetric and symmetric encryption algorithms:
Asymmetric Encryption algorithm: RSA,DSA/DSS
Symmetric encryption algorithm: Aes,rc4,3des
Hash algorithm: md5,sha1,sha256
The asymmetric encryption algorithm is used to encrypt the generated password during the handshake, and the symmetric encryption algorithm is used to encrypt the data that is actually transmitted, while the hash algorithm is used to verify the integrity of the data.
V. Working principle of HTTPS
HTTPS requires a handshake between the client (browser) and the server (Web site) before transmitting the data, which establishes the password information for both parties to encrypt the transmitted data during the handshake. TLS/SSL protocol is not only a set of encrypted transmission protocols, but also an artist-designed artwork, TLS/SSL using asymmetric encryption, symmetric encryption and hash algorithm. A brief description of the handshake process is as follows:
1. The browser sends a set of encryption rules that it supports to the Web site.
2. The website selects a set of cryptographic algorithms and hash algorithms, and sends its own identity information back to the browser in the form of a certificate. The certificate contains information such as the website address, the encrypted public key, and the issuing authority of the certificate.
3. After obtaining the website certificate, the browser will do the following tasks:
A) Verify the legality of the certificate (the issuing authority is legal, the certificate contains the address of the website is consistent with the address being accessed, etc.), if the certificate is trusted, the browser bar will display a small lock, otherwise the certificate is not trusted to prompt.
b) If the certificate is trusted, or if the user accepts an untrusted certificate, the browser generates a random number of passwords and encrypts them with the public key provided in the certificate.
c) Use the agreed hash to calculate the handshake message, encrypt the message with the generated random number, and then send all previously generated information to the Web site.
4. After the Web site receives the data from the browser, do the following:
A) Use your own private key to decrypt the information to remove the password, use the password to decrypt the browser's handshake message, and verify that the hash is consistent with the browser.
b) Encrypt a handshake message with a password and send it to the browser.
5. The browser decrypts and calculates the hash of the handshake message, if it is consistent with the hash of the server, at which point the handshake process ends, and all the communication data will be encrypted by the random password generated by the previous browser and using the symmetric encryption algorithm.
Reference:
Public key, private key, SSL (speaking very lively)
Which of the RSA's public and private keys is used for encryption and which is used for decryption?
Using OpenSSL for RSA encryption and decryption
The Android Client and PHP service-side res Public private key are encrypted with each other
HTTPS those things (a) HTTPS principle
Public keys, private keys, digital signatures, digital certificates, symmetric and asymmetric algorithms, HTTPS