Summary of RSA encryption and decryption solutions in js

Summary of RSA encryption and decryption solutions in js

1. Demand background
  • In the recent development of the vue project, RSA encryption and decryption operations are required for the user's mobile phone number.
  • Introduction: RSA encryption was proposed in 1977 by Ronald Rivest (Ron Rivest), Adi Shamir (Adi Shamir) and Leonard Adleman (Leonard Adleman). All three were working at MIT at the time . RSA is composed of the initial letters of their three surnames. It usually first generates a pair of RSA keys, one of which is a secret key, which is kept by the user; the other is a public key, which can be disclosed to the outside world, and can even be registered in the network server. To increase security strength, the RSA key must be at least 500 bits long. This makes encryption computationally intensive. In order to reduce the amount of calculation, when transmitting information, the combination of traditional encryption method and public key encryption method is often used, that is, the information is encrypted with the improved DES or IDEA session key, and then the session key and information are encrypted with the RSA key Summary.
  • RSA is the most widely studied public key algorithm. It has been nearly 30 years since it was proposed. It has experienced the test of various attacks and is gradually accepted by people. It is generally considered to be one of the best public key schemes at present. In 1983, the Massachusetts Institute of Technology applied for a patent for the RSA algorithm in the United States.
solution
  • Considering the convenience of use, the jsencrypt plug-in is needed here, which is used as follows:

  • 
    // 1、安装pinyin-pro(推荐使用淘宝镜像):
    npm install jsencrypt --save
    
    // 2、在项目中引入:
    import JSEncrypt from 'jsencrypt'
    
    // 3、加密(origin:原始text,key:公钥)
    const encryptMI = (origin, key) => {
          
          
      const encrypt = new JSEncrypt()
      encrypt.setPublicKey(key)
      return encrypt.encrypt(origin)
    }
    
    // 4、解密(origin:加密text,key:私钥)
    const decryptMI = (cipher, key) => {
          
          
      const decrypt = new JSEncrypt()
      decrypt.setPrivateKey(key)
      return decrypt.decrypt(cipher)
    }
    
    
  • solve.

おすすめ

転載: blog.csdn.net/qq_34917408/article/details/128233636