Preschool guidelines
Virtual character design
In order to facilitate us to learn the iOS signature mechanism, this paper sets up four virtual characters, respectively
- Alice and Bob: Communicate with each other
- EVe: Eavesdropper
- Mallory: Active attacker
Message communication and eavesdropping
- If encryption is not used, both parties communicate in plaintext
- But such communications are vulnerable to eavesdropping and theft of confidential information
- So how do you prevent information from being monitored? We need to encrypt the communication information.
- The plaintext is first encrypted into ciphertext and sent to the receiver
Encrypt and decrypt?
How to encrypt and decrypt?
The message sender uses the key for encryption
The receiver of the message uses a key for decryption
Type of password
We can divide passwords into two types, depending on how the key is used
Symmetrical password
Symmetric ciphers use the same key for encryption and decryption
There are three commonly used symmetric cryptography algorithms
Data Encryption Standard (DES)
DES is a symmetric cipher algorithm that encrypts 64-bit plaintext into 64-bit ciphertext. The key length is 56 bits. In terms of specifications, the length of the key is actually 64bit, but a bit is set every 7 bits for error checking, so the length of the key is 56bit. DES encrypts only 64bit data at a time. Therefore, you need to iterate DES encryption when encountering large data. And because DES encryption algorithm can be cracked in a short time, it is not recommended to use.
3DES
3DES is a password algorithm obtained by repeating DES three times, also called triple DES. This algorithm is still used by some banking institutions, but the processing speed is not high, and there are security problems.
- 3DES encrypts plain text into ciphertext by encryption-decryption-encryption
- 3DES decrypts ciphertext by decrypting, encrypting, and decrypting it into plain text
In the preceding encryption and decryption process, the three keys are different, so they are also called DES-EDE3
- If all the keys in the preceding process are the same, the result is equivalent to that of ordinary DES, and the three times encryption and decryption is meaningless
- If key 1 and key 3 are the same, but key 2 is different, it is called DES-ede2
Advanced Encryption Standard (AES)
AES is a symmetric cryptographic algorithm that replaces DES as the new standard. The key length of AES can be 128, 192, and 256 bits.
Key distribution problem
What is the key distribution problem?
When symmetric passwords are used, key distribution problems must occur, as shown in the following figure
Suppose Alice sends the message encrypted with symmetric password to Bob, and Bob needs to get Alice’s encrypted key to view the plaintext information, so Alice sends the key to Bob at the same time. In the process of sending the key, Eve may eavesdrop, and Eve gets the stolen key and ciphertext. It can also be parsed to get clear text messages.
How to solve the key distribution problem?
To solve the key distribution problem, you can use the following methods
- Share keys in advance. That is, Alice and Bob share a secret key in advance, which cannot be transmitted over the network. However, this method is very troublesome and is not recommended.
- Key distribution center
- Diffie-hellman key exchange
- Public key password. Public key cryptography is now a common way, and it is the main way we learn.
Public-key Cryptography
In public key Cryptography, the key is classified into encryption key and decryption key. They are not the same key, so public key Cryptography is also called Asymmetric Cryptography.
In public key cryptography:
- The encryption key is public, so it is called a public key.
- The decryption key is kept by the message receiver and cannot be made public. Therefore, it is also called a private key.
- The public key and private key are one-to-one and cannot be created separately. A pair of public and private keys is called a key pair.
- The ciphertext encrypted by a public key can be decrypted only by using the private key corresponding to the public key.
- The ciphertext encrypted by the private key can be decrypted only by using the corresponding public key of the private key.
Public key cryptography solves key distribution problems
As mentioned above, the key distribution problem can be solved by using public key cryptography. The specific solution process is as follows:
- First, the message receiver generates a pair of public and private keys.
- Sends the public key to the message sender.
- The message sender encrypts the message with a public key.
- The sender sends the encrypted ciphertext to the receiver.
- The receiver decrypts the ciphertext with the private key to obtain the plaintext data.
RSA
RSA is the most widely used public key cryptography algorithm. Its name is a combination of the first letters of the surnames of the three developers — Ron Rivest, Adi Shamir, and Leonard Adleman
Hybrid cryptosystem
Contrast symmetric and asymmetric ciphers
- Symmetric cipher can not solve the problem of key distribution well
- The encryption and decryption speed of public key passwords is slow. Because the size of the encrypted ciphertext is the same as the size of the message itself, the encryption and decryption speed of a large amount of data is slow.
- In order to solve the key distribution problem, and improve the speed of encryption and decryption. Therefore, a combination of symmetric cryptography and public key cryptography was adopted to learn from each other’s strengths. Today, the SSL/TLS used for cryptographic communication on the network are hybrid cryptographic systems.
Hybrid cryptosystem – encryption
Session Key
- A session key is a temporary key that is generated randomly for this communication and is generated using a pseudorandom number generator
- As the key of symmetric cipher, the session key is used to encrypt messages to improve the speed
Encryption step – Send a message
- First, the message sender needs to get the public key of the message receiver
- The second step is to generate the session key, which acts as the key for the symmetric cipher and encrypts the message into ciphertext
- Third, use the message receiver’s public key to encrypt the session key
- Fourth, the encryption results generated in step 2 and step 3 are sent to the message receiver
The content sent includes
- Messages encrypted with session keys (encrypted with symmetric ciphers)
- Session key encrypted with public key (encrypted with public key password)
Hybrid cryptosystem – decryption
After receiving the message from == sender ==, you need to decrypt the message as follows:
- First, the message receiver decrypts the session key with its own private key
- In the second step, decrypt the message with the session key obtained in the first step and get the plaintext data
Hybrid cryptosystem – Summary of complete steps for encryption and decryption
Use Alice as the == message sender == and Bob as the message receiver
Sending a message (encrypted)
- Bob first generates a pair of public and private keys
- Bob shares the public key with Alice
- Alice randomly generates a session key (temporary key)
- Alice encrypts the message that needs to be sent with the session key (symmetric cryptography)
- Alice encrypts the session key with Bob’s public key (public-key cryptography, also known as asymmetric cryptography)
- Alice sends Bob the results of steps 4 and 5
Receive message (decrypt)
- Bob decrypts the session key (public-key cryptography, also known as asymmetric cryptography) using his own private key
- Bob decrypts the sent message using the session key (symmetric cryptography)
Monomial hash function
A one-way hash function can calculate the hash value based on the message content. The length of the hash value is independent of the length of the message. Whether the message is 1bit, 10M or 100G, the one-way hash function will calculate the fixed length of the hash value.
Characteristics of unidirectional hash functions
- A fixed-length hash value is calculated based on a message of arbitrary length.
- Fast calculation speed, can quickly calculate the hash value
- Different messages, even if there is a difference of 1bit of data, the hash value is different
- It’s unidirectional and irreversible
A common one-way hash function
The one-way hash function is also called message digest function and hash function. The output hash value is also known as message digest, fingerprint.
Several common one-way hash functions are as follows
MD4, MD5
Generates a 128bit hash value, MD stands for Message Digest, which is no longer secure
SHA-1
Generates a hash value of 160 bits, which is also currently unsafe
SHA-2
The hash value of SHA-256, SHA-384, and SHA-512 is 256 bits, 384 bits, and 512 bits respectively. The longer the hash value, the more secure it is
SHA-3
Secure Hash algorithm-3 (SHA3) is a unidirectional Hash function Algorithm released as a new standard to replace sha-1, which has been attacked in theory. Companies and cryptographers around the world submitted a number of candidates for SHA-3, and after five years of selection, the Keccak algorithm was officially adopted as the SHA-3 standard in 2012.
Application of unidirectional hash functions
Prevent data from being tampered with
- Prevent file tampering by passing the file through a one-way hash function to get the hash value. Store the hash value in a safe place. If the file is tampered with after a period of time, you can get the latest file and compare the latest hash value with the previous hash value to determine whether the file is tampered with.
- The software is tampered with. In order to spread the communication load, some software companies will put their software on mirror sites for users to download. So how can users tell if software downloaded from mirror sites has been maliciously tampered with? Generally, software companies will put the hash value obtained through the hash function on the official website for users to compare. As long as the hash value obtained by the downloaded software is consistent with the official website, it indicates that the downloaded software has not been maliciously tampered. For example, you can click the VNC official website to view the software.
Password encryption
When App login, usually need to check account number and password in the database, however, saved passwords are generally SHA – 2 after the hash function of hash value rather than text passwords, so when logging in need for the user to enter the password hashing algorithm, get the hash value, again with a database to store the hash value of comparison, To determine whether the password is correct.
In addition, due to the irreversibility of the hash function, even if others get the hash value of the password stored in the database by improper means, they cannot get the real password of the user. This greatly increases the security of user data.
A digital signature
In fact, it was impossible to verify the authenticity of the message using the previous symmetric, asymmetric, or hybrid cryptography systems. That is, the receiver of the message cannot determine whether the message was sent by the message sender. It is also possible that someone else sent the message disguised as a message sender. So how do you verify the message’s authenticity? Authentication is performed using a digital signature.
Two kinds of behavior of digital signature
- Generate a signature. This is done primarily by the message sender and is generated through a “signature key”
- Verify the signature. This is done by the message receiver and authenticated by the Authentication key
So how do you guarantee that the message sender signed the signature himself? The answer is to sign the message using the sender’s own private key. Above, we know that the public key is public and available to all, so in public key cryptography, anyone can use the public key to encrypt.
In digital signatures, anyone can use a public key to verify the signature.
Digital signature versus public key cryptography
A digital signature is simply the reverse of a public key password
The process of digital signature
Common digital signature process
- First, the message sender generates a pair of public and private keys.
- The message sender sends the public key to the message receiver.
- The sender encrypts the message with his private key to obtain the signature information.
- The message sender sends the message along with the signature to the message receiver.
- The message receiver decrypts the signature information using the public key of the message sender to obtain the message in the signature
- The recipient compares the decrypted message with the received message. If the decrypted message is the same with the received message, the signature verification is successful
However, the signature information is obtained by encrypting the original message. If the size of the message is 1 MB, the size of the encrypted signature is also 1 MB, and the message sent to the receiver is 2 MB
Improve the digital signature process
A one-way hash function is used to improve on the previous digital signature process.
- First the message sender calculates the hash value of the message using a one-way hash function
- The message sender uses its own private key to encrypt the hash value obtained in the first step to generate signature information
- The message sender sends the signature information along with the message to the message receiver
- The receiver uses the sender’s public key to decrypt the signature information and obtain the decrypted hash value
- The message receiver performs a one-way hash of the message to obtain the hash value
- The receiver compares the decrypted hash value in the signature with the hash value obtained by the direct hash function. If the hash value is the same, the signature verification succeeds.
Complete signature process
The role of digital signatures
Based on the above points, the functions of digital signature can be summarized:
- Verify that the message is complete
- Identify whether message content has been tampered with
- Prevents the message sender from denying sending the message
Digital signature issues
- First of all, what would happen if someone changed the content of the document or the content of the signature? The result is a signature verification failure, proving that the file contents have been tampered with.
- Moreover, during the digital signature process, the plaintext of the message is directly sent to the message receiver, which cannot guarantee the security of the message. The function of digital signature is not to guarantee the confidentiality of data, but only to identify whether the message content has been tampered with.
To use digital signatures correctly, it is necessary to verify that the public key of the signature must belong to the real sender. Because a man-in-the-middle attack may occur between the sender and receiver, the specific attack steps are as follows:
- The message receiver sends its own public key to the message sender.
- The middleman eavesdrops on the contents of the communication and obtains the public key sent out by the receiver of the message.
- The middleman intercepts the message receiver’s public key and sends his own public key to the message sender
- The sender uses the received public key to encrypt the message and sends the ciphertext to the receiver.
- The middleman intercepts the ciphertext, decrypts it with its own private key, and obtains the plaintext message. The message is then encrypted using the public key of the previously intercepted message receiver, and the forged ciphertext is sent to the message receiver.
- The message receiver receives the ciphertext, decrypts it with its own private key, and finally gets the plaintext message.
In the above message delivery process, the sender and receiver are unaware of the presence of the middleman, but the message bei has been leaked.
The above communication was attacked by a man-in-the-middle, which would result
- The public key was forged by a middleman
- Digital signature invalidation
Therefore, the public key must be verified before the signature can be verified. How do you verify the validity of a public key? You need to pass the certificate.
certificate
When we see certificates, we will think of driving licenses, graduation certificates and so on, which are certified by authoritative organizations. In cryptography, the full name of a Certificate is public-key Certificate (PKC). It’s similar to a driver’s license or a student’s card.
- The certificate contains personal information such as name, email address, and the person’s public key
- And a Certificate Authority (CA) imposes a digital signature
A CA is an individual or organization that can determine that “the public key really belongs to this person” and generate a digital signature
- There are international organizations, governmental organizations
- There are businesses that make money by providing certification services
- Individuals can also set up certification bodies
Use of certificates
There are several steps to use the certificate:
- The receiver generates its own key pair. 2. The receiver registers its own public key with the authentication authority
- The authentication authority uses its own private key to digitally sign the message receiver’s public key and generate a certificate
- The message sender gets the public key (certificate) of the message receiver with the digital signature of the authentication authority from the authentication authority
- The sender uses the public key of the authentication authority to verify the digital signature and verify the validity of the public key of the receiver.
- The message sender encrypts the message using the message receiver’s public key and sends it to the message receiver.
- The message receiver decrypts the ciphertext using its own private key to get the final message
After the authentication process of the authentication authority is added, there is no public key transfer process between the message sender and the message receiver. The message sender obtains the public key of the message receiver from the authentication authority. In this way, the problem of public key forgery caused by the man-in-the-middle attack is eliminated
The procedure for registering and downloading certificates is as follows
- The receiver of the message registers the public key with the authentication authority
- The authentication authority digitally signs the public key of the message receiver, generates a certificate, and saves the certificate in the repository
- The message sender downloads the certificate from the certification authority’s repository
- The message sender uses the public key of the authentication authority to verify the certificate and obtain the public key of the message receiver