前端項目中使用RSA加密解密-非對稱加密


本文介紹的rsa加解密方法,適合react,vue,angular框架的前端項目

 

1. 項目里安裝rsa:npm i jsencrypt

2. 項目中引入JSEncrypt:import JSEncrypt from 'jsencrypt/bin/jsencrypt'

3. 實例化一個JSEncrypt對象:let encryptor = new JSEncrypt()

4. 加密:

  4.1 設置公鑰:

     let publicKey = `公鑰`
     encryptor.setPublicKey(publicKey)
  4.2 加密:
     let parm = encryptor.encrypt('要加密的字段')
     console.log(parm)
5. 解密:

  5.1 設置私鑰:

     let publicKey = `私鑰` 
     encryptor.setPrivateKey(privateKey)
  5.2 解密:
     let parm = encryptor.decrypt('要解密的字段')
     console.log(parm)
備注:為了安全起見,私鑰都是存放在服務器上的,解密這個操作最好是后台來做,不建議在前端進行rsa解密
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM