We have such a scene where there are senders, receivers, and third parties. The sender wants to send a message to the receiver, and the third party wants to truncate and tamper with the message before forwarding it to the receiver.
How can you be a safe way to communicate? Generally, you need to meet the following three conditions:
Integrity (messages are not tampered with), confidentiality (cannot be decrypted by third parties), and can be authenticated (the receiver knows who sent the message).
First of all, we talk about the next MD5 encryption, strictly speaking, MD5 is not called encryption, it is just a hashing operation, the message is hashed, you can get a summary of the message (Digest, fingerprint).
The MD5 algorithm has three features,
1. Hashing algorithm is irreversible, the hashing operation can be understood as one-way encryption: based on the original message operation to get a summary, according to the summary, can not deduce the original message. For example, as long as there is a complete person, you can obtain its fingerprint, but if only fingerprints, it is impossible to obtain the full information of this person.
2. Any different file, even if only one byte of nuance, the resulting summary is completely different.
3. Regardless of the size of the original message, the summary information obtained by the operation is fixed length.
The following is a process that determines whether a message has been tampered with.
The sender hashes the message, gets a message digest, sends a message digest and message, and explains the hash algorithm used by the digest, such as MD5.
The receiver obtains the message and the digest, uses the same hashing algorithm to hash the received message, obtains the new summary
The receiver determines whether the sender's summary is the same as the summary it generated, and the same holds that the message has not been tampered with, otherwise it is tampered with.
But this does not guarantee a third party intercepting the message, tampering with the message, regenerating the digest, and forwarding it.
Symmetric encryption
Symmetric encryption means that encryption and decryption use the same key, symmetric encryption algorithm des (Data Encryption Standard), AES (Advanced Encryption Standard) and so on.
Symmetric encryption requires both sides to hold the key and ensure that the key is not compromised. But the sender and the recipient always have an initial communication to pass the key, and how to secure it at this time.
Asymmetric encryption
Asymmetric encryption has a set of key pairs, one is public, and anyone can get it, called a public key. One is the safekeeping and strictly confidential, known as the private key. Messages are encrypted by the public key and can only be decrypted by the private key, which can only be decrypted by the public key. Decryption is encrypted via the RSA algorithm.
This encryption method, the third party does not have the private key, is unable to decrypt. But the public key is available to anyone, and anyone can encrypt it and send it to the receiver with the public key, so it cannot satisfy the conditional authentication.
Authentication Mode
In authentication mode, the sender of a message publishes the public key and holds the private key, as follows
1. The sender publishes its own public key, and anyone can obtain
2. Send a message using its own private key to encrypt messages, and then send
3. The receiving party decrypts the message using the sender's public key.
In this mode, if sender A, receiver B, because B holds the public key of a, B can decrypt and know that the message was sent by a. This satisfies the conditions of the authentication
But since A's public key is public, a third party intercepts the message and decrypts it through the public key, which is equivalent to the public. Not satisfied with confidentiality
Asymmetric cryptographic operations are relatively time-consuming and unsuitable for large data. Therefore, its authentication mode has been improved to form a new encryption method, called digital signature.
Digital signatures
The digital signature and authentication mode are very similar, except that the hashing algorithm is included. Steps are as follows
1. The sender is hashing the message and gets the original message digest.
2. The sender encrypts the message digest using its own private key, which is also called signature.
3. The receiving party uses the sender's public key to decrypt the received message and gets a summary.
4. The receiver hashes the message and gets a local summary.
5. Determine whether the sender's summary and local summary are equal to determine if the message has been tampered with.
Digital signatures satisfy both integrity and authentication. But the message itself is not encrypted, and if a third party intercepts the message, it can see the contents of the message. Digital signatures are a great choice if you are passing on sensitive data that is not highly confidential.
Integrated implementation
Send-Side steps
1. Hash out the message to get a message digest.
2. Use the sender's private key to encrypt the message digest (authentication mode ensures that the receiver can confirm itself)
3. Use the receiver's public key to encrypt the message (encryption mode ensures that the message can only be decrypted by the desired receiver)
4. Send message and message digest
Receiver Step
1. Use the sender's public key to decrypt the message digest (authentication mode: Who sent the confirmation message), get the original message digest
2. Use your private key to decrypt the message (Encryption mode: Secure message content)
3. Hash the message to get the local message digest.
4. Compare the original message digest with the local message digest to confirm that the message was tampered with.
It can be seen that, through the above method, the receiver, the sender of all four keys, and then use the hashing algorithm, yes, the previous three security conditions are met. But the asymmetric encryption algorithm is a relatively time-consuming operation, and this scheme is inefficient for large data. But in fact, it can be used to solve the problem of key transfer in symmetric encryption, that is, key transfer is encrypted using this comprehensive method, and then symmetric encryption is used, because it is now secure.
Certificate mechanism
One concept associated with digital signatures is the certificate mechanism, and what is the certificate used to do. In each of these patterns, there is always the assumption that the public key of the receiver or sender is always correct. In fact, unless the other party hand over the public key, otherwise, when passing the public key, it is possible to be tampered with. Then how to solve the problem. This is the need for a certificate mechanism: You can introduce a fair third-party organization, when a party wants to publish the public key, it will its own identity information and public key to the third party, third party to its identity, if there is no problem, then the information and public key packaging into certificates. When you need to get the public key, you can extract the public key from the certificate only if you need to obtain it.