[JS] Introduction to common front-end encryption algorithms MD5, AES, RSA, example of using rsa encryption using jsencrypt in Vue3

Front-end encryption has been used before, here is a summary of encryption

1. Introduction to Encryption Algorithms

The classic cryptographic algorithms are: hash hash algorithm , symmetric encryption algorithm and asymmetric encryption algorithm .

1.1 Hash hash algorithm

  • Hash (Hash) algorithm : A one-way algorithm that can generate a Hash value for the target information through the Hash algorithm, but cannot regain the target information through this Hash value.
  • Application scenarios : Commonly used in irreversible password storage, information integrity verification, etc.
  • Common Hash algorithms : MD2, MD4, MD5, HAVAL, SHA.

One-way hash function MD5

MD5 has a fixed length, no matter how many bytes the input content has, the final output is 128 bits, that is, 16 bytes. This also explains why MD5 and other one-way hash functions are irreversible - the output fixed length means that there will be data loss.
Usually, we can use hexadecimal literal value to represent it, every 4 bits are displayed in hexadecimal literal value, and what we get is a string with a length of 32 bits. Note that one-way hash functions such as MD5 are highly discrete, which means that as long as the input plaintext is different, the result will be completely different, even if there is only one more space.

MD5 has the following usage scenarios:

  • Password protection : For example, instead of directly storing the user's password, store the MD5 result of the password. But now it is generally believed that using MD5 for encryption is not very safe, and more is to use the highly discrete characteristics of MD5 to use it for digital signatures, integrity verification, cloud disk instant transmission, etc.;
  • Digital signature : We can publish its MD5 at the same time when releasing the program. In this way, after others download the program, they can calculate the MD5 by themselves, and after a comparison, they can know whether the program has been tampered with, such as implanting a Trojan horse.
  • Integrity check : For example, when the front end transmits a very large piece of data to the back end, in order to prevent data loss during network transmission, the MD5 of a piece of data can be generated at the front end and sent to the back end together, so that the back end can calculate it again after receiving the data MD5, you know whether the data is complete.

1.2 Symmetric encryption algorithm

  • Symmetric encryption algorithm : 相同密钥The encryption algorithm used for encryption and decryption.
  • Advantages and disadvantages : The advantage of the symmetric encryption algorithm lies in the high speed of encryption and decryption and the difficulty of cracking when using a long key.
  • Common symmetric encryption algorithms : DES, 3DES, DESX, Blowfish, IDEA, RC4, RC5, RC6 and AES.
    insert image description here

1.3 Asymmetric encryption algorithm

  • Symmetric encryption algorithm : refers to 不同密钥the encryption algorithm used for encryption and decryption, also known as 公私钥encryption.
  • Advantages and disadvantages : The disadvantage of asymmetric encryption is that the speed of encryption and decryption is much slower than that of symmetric encryption. In extreme cases, it can even be 1000 times slower than asymmetric encryption.
  • Common symmetric encryption algorithms : RSA, ECC (for mobile devices), Diffie-Hellman, El Gamal, DSA (for digital signatures).
    insert image description here

The performance of an encryption algorithm can usually be measured in terms of the complexity of the algorithm itself, the length of the key (the longer the key, the more secure it is), the speed of encryption and decryption, etc. Among the above-mentioned algorithms, except for the insufficient key length of DES and the slow speed of MD2, which have been gradually eliminated, other algorithms are still used in the current encryption system products.

2. Encryption algorithm used in Vue3

Install encryption library crypto-js or jsencrypt

The encryption and decryption process of JSEncrypt needs to use OpenSSL to generate the secret key. OpenSSL is an open source software that implements the SSL protocol. It can be used to generate certificates, certificate signatures, generate secret keys, encrypt and decrypt, etc. For example, my company's recent project has a requirement that when localhost uses the https protocol, openssl is used.

The following are two common open source encryption libraries

# 官网 https://www.npmjs.com/package/crypto-js
# crypto-js进行AES加密,安装: 
npm i --save crypto-js 

# 官网 https://www.npmjs.com/package/jsencrypt
# jsencrypt进行RSA加密,安装: 
npm i --save jsencrypt

Used in the code (the public key is generally provided by the backend, and the backend saves its own private key):

import JSEncrypt from 'jsencrypt';

const crypt = new JSEncrypt();
crypt.setKey(import.meta.env.VITE_PUBLIC_kEY); // 设置公钥-此处是引用环境变量
console.log("passWord",crypt.encrypt(userInfo.password));

put a network diagram
insert image description here

Encryption sample code https://blog.csdn.net/qq_32247819/article/details/120821664

Guess you like

Origin blog.csdn.net/weixin_42960907/article/details/126930561