美文网首页
通用前端RSA加密/解密封装

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

作者: 略懂一点html | 来源:发表于2022-03-09 00:23 被阅读0次

    码云仓库地址:https://gitee.com/April_lee/common.git

    RSA.js 封装内容 (注:已解决长内容无法加密解密问题)

    import { JSEncrypt } from './jsencrypt.js' // 基于rsa的加密解密库, 文件内容太长,需要的请前往我的码云仓库获取
     
    // 加密公钥
    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'
    // 加密
    // 一般内容
    rsaEncrypt(加密内容)  
    // JSON格式加密
    rsaEncrypt(encodeURI(JSON.stringify(加密内容)))  
    
    
    // 解密
    // 一般解密
    rsaDecrypt(解密内容) 
    // 解密JSON格式
    let data = rsaDecrypt(解密内容) 
    !!decodeURIComponent(data) ? JSON.parse(decodeURIComponent(data)) : data
    

    相关文章

      网友评论

          本文标题:通用前端RSA加密/解密封装

          本文链接:https://www.haomeiwen.com/subject/wywzrrtx.html