通用前端RSA加密/解密封裝

碼云倉(cāng)庫(kù)地址:https://gitee.com/April_lee/common.git

RSA.js 封裝內(nèi)容 (注:已解決長(zhǎng)內(nèi)容無法加密解密問題)

import { JSEncrypt } from './jsencrypt.js' // 基于rsa的加密解密庫(kù), 文件內(nèi)容太長(zhǎng),需要的請(qǐng)前往我的碼云倉(cāng)庫(kù)獲取
 
// 加密公鑰
const key = `` // 找后端給
 
// 加密
export function rsaEncrypt (msg) {
  const jsencrypt = new JSEncrypt()
  jsencrypt.setPublicKey(key)
  const encryptMsg = jsencrypt.encryptLong(msg)
  return encryptMsg
}
 
// 解密私鑰
const privateKey = `` // 找后端給

// 解密
export function rsaDecrypt (msg) {
  const decrypt = new JSEncrypt()
  decrypt.setPrivateKey(privateKey)
  const decryptMsg = decrypt.decryptLong(msg)
  return decryptMsg
}

使用示例

import { rsaEncrypt, rsaDecrypt } from 'RSA.js'
// 加密
// 一般內(nèi)容
rsaEncrypt(加密內(nèi)容)  
// JSON格式加密
rsaEncrypt(encodeURI(JSON.stringify(加密內(nèi)容)))  


// 解密
// 一般解密
rsaDecrypt(解密內(nèi)容) 
// 解密JSON格式
let data = rsaDecrypt(解密內(nèi)容) 
!!decodeURIComponent(data) ? JSON.parse(decodeURIComponent(data)) : data
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容