Art~Common irreversible encryption, symmetric encryption, and asymmetric encryption

Preface

Encryption is a very important means for us to ensure data security, especially in the widespread use of tokens, encryption means has a pivotal position.

A good encryption algorithm is mainly reflected in two points:

  1. Not easy to crack
  2. The efficiency of encryption and decryption is better

For example, in my current work, encryption is mainly used for the generation of some digital signatures or the verification of some data to protect data security and data integrity.

digital signature

Digital signature, in simple terms, is a way to verify your identity by providing identifiable digital information. A set of digital signatures usually defines two complementary operations, one for signing and the other for verification.
The sender holds the private key that can represent his identity (the private key cannot be leaked), and the recipient holds the public key corresponding to the private key, which can be used to verify its identity when receiving information from the sender.

Insert picture description here

Irreversible encryption

Irreversible encryption uses some hashing algorithms, which are mainly used for data integrity protection.

MD5

MD5 uses a hash function, and its typical application is to generate a digest of a piece of information to prevent tampering. Strictly speaking, MD5 is not an encryption algorithm but a digest algorithm. No matter how long the input is, MD5 will output a string with a length of 128 bits (usually 32 characters in hexadecimal).

public static final byte[] computeMD5(byte[] content) {
    
    
    try {
    
    
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        return md5.digest(content);
    } catch (NoSuchAlgorithmException e) {
    
    
        throw new RuntimeException(e);
    }
}

SHA1 algorithm

SHA1 is the same popular message digest algorithm as MD5, but SHA1 is more secure than MD5. For messages less than 2^64 bits in length, SHA1 will generate a 160-bit message digest. Based on MD5, SHA1's information digest feature and irreversibility (generally speaking), it can be used in scenarios such as checking file integrity and digital signatures.

public static byte[] computeSHA1(byte[] content) {
    
    
    try {
    
    
        MessageDigest sha1 = MessageDigest.getInstance("SHA1");
        return sha1.digest(content);
    } catch (NoSuchAlgorithmException e) {
    
    
        throw new RuntimeException(e);
    }

Insert picture description here

Symmetric encryption

Symmetric encryption algorithm is applied earlier encryption algorithm, also known as shared-key encryption algorithm . In the symmetric encryption algorithm, there is only one key used, and both the sender and the receiver use this key to encrypt and decrypt data. This requires both the encryption and decryption parties to know the encryption key in advance.

There is a certain risk of key leakage, but the key design is indeed much easier than the asymmetric method. And there is no need to pass the public key like asymmetric encryption. The biggest advantage is that the cost of encryption and decryption is relatively small compared to asymmetric.

  • Data encryption process: In the symmetric encryption algorithm, the data sender passes the plaintext (original data) and the encryption key together through a special encryption process to generate a complex encrypted ciphertext for transmission.

  • Data decryption process: After receiving the ciphertext, if the data receiver wants to read the original data, it needs to use the encryption key and the inverse algorithm of the same algorithm to decrypt the encrypted ciphertext to restore it to a readable plaintext .

AES, DES, and 3DES are all symmetric block encryption algorithms, and the encryption and decryption process is reversible.

DES algorithm

The DES encryption algorithm is a block cipher that uses 64 bits as a block to encrypt data. Its key length is 56 bits, and the same algorithm is used for encryption and decryption.
The DES encryption algorithm keeps the key secret, while the public algorithm includes encryption and decryption algorithms. In this way, only those who have the same key as the sender can interpret the ciphertext data encrypted by the DES encryption algorithm. Therefore, to decipher the DES encryption algorithm is actually to search for the encoding of the key. For a key with a length of 56 bits, if an exhaustive method is used to search, the number of operations is 2 ^ 56 times.

3DES algorithm

It is a symmetric algorithm based on DES, which uses three different keys to encrypt a piece of data three times with higher strength.

AES algorithm (emphasis)

The AES encryption algorithm is an advanced encryption standard in cryptography. The encryption algorithm adopts a symmetric block cipher system. The minimum key length supported is 128 bits, 192 bits, and 256 bits, and the block length is 128 bits. The algorithm should be easy for various hardware and software. achieve. This encryption algorithm is a block encryption standard adopted by the US Federal Government.
AES itself is to replace DES, AES has better security, efficiency and flexibility.

Insert picture description here

Asymmetric encryption

RSA algorithm

RSA encryption algorithm is currently the most influential public key encryption algorithm, and is generally considered to be one of the best public key schemes. RSA is the first algorithm that can be used for both encryption and digital signature. It can resist all cryptographic attacks known so far and has been recommended by ISO as a public key data encryption standard.

The RSA encryption algorithm is based on a very simple fact of number theory: it is easy to multiply two large prime numbers, but it is extremely difficult to factor the product of the product, so the product can be publicly used as an encryption key.

ECC algorithm

ECC is also an asymmetric encryption algorithm. The main advantage is that in some cases, it uses a smaller key than other methods, such as the RSA encryption algorithm, which provides a comparable or higher level of security. However, one disadvantage is that the implementation of encryption and decryption operations takes longer than other mechanisms (compared to the RSA algorithm, which consumes more CPU).
Insert picture description here

Symmetric algorithm and asymmetric encryption algorithm

Symmetric algorithm
Key management: relatively difficult, not suitable for the Internet, generally used in internal systems.
Security: Medium
Encryption speed: several orders of magnitude faster (software encryption and decryption speed is at least 100 times faster, and can encrypt and decrypt several M bits of data per second), Suitable for large data volume encryption and decryption processing

Asymmetric algorithm
Key management: easy to manage keys
Security: high
Encryption speed: relatively slow, suitable for small data volume encryption and decryption or data signature

Guess you like

Origin blog.csdn.net/Shangxingya/article/details/114990100