前端使用rsa加密解密 使用 jsencrypt库 vite模式下特殊处理 已在教程中

  1. rsa加密有长度限制 如果数据太大(不建议使用) 解密会失败
  2. 配合压缩解压来进行使用
  3. 前端解密需要私钥 后端返回的私钥格式默认是xml 要让他转化成 pem 格式

加密解密

//引入
import { JSEncrypt } from 'jsencrypt';
import { key } from './key.js';
import { zip, unzip } from './zlip';

let decrypt = new JSEncrypt();
decrypt.setKey(key); // 一般后端指定

// 加密
export async function encryptionRSA(param) {
  const result = await zip(param);
  return new Promise((resolve, reject) => {
    if (!result) return reject(false);
    let passwordEncryp = decrypt.encrypt(result);
    resolve(passwordEncryp);
  });
}

// 解密
export function decryptRSA(msg) {
  const decryptMsg = decrypt.decrypt(msg);
  return new Promise(async (resolve, reject) => {
    if (!decryptMsg) return reject(false);
    const result = await unzip(decryptMsg);
    if (!result) reject(false);
    resolve(result);
  });
}

压缩解压

vite 编译模式下需要特殊处理
yarn add @esbuild-plugins/node-globals-polyfill
yarn add @esbuild-plugins/node-modules-polyfill
yarn add buffer
rollup-plugin-node-polyfills 是 @esbuild-plugins/node-modules-polyfill 自带的

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vuejsx from "@vue/babel-plugin-jsx";
// node polyfill
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'
import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill'
import rollupNodePolyFill from 'rollup-plugin-node-polyfills'

const path = require("path");

export default defineConfig({
  hmr: true, // 热更新
  plugins: [
    vue(),
    vuejsx({}),
  ],
  resolve: {
    // 配置路径别名
    alias: {
      "@": path.resolve(__dirname, "./src"),
      util: 'rollup-plugin-node-polyfills/polyfills/util',
      sys: 'util',
      events: 'rollup-plugin-node-polyfills/polyfills/events',
      stream: 'rollup-plugin-node-polyfills/polyfills/stream',
      path: 'rollup-plugin-node-polyfills/polyfills/path',
      querystring: 'rollup-plugin-node-polyfills/polyfills/qs',
      punycode: 'rollup-plugin-node-polyfills/polyfills/punycode',
      url: 'rollup-plugin-node-polyfills/polyfills/url',
      string_decoder: 'rollup-plugin-node-polyfills/polyfills/string-decoder',
      http: 'rollup-plugin-node-polyfills/polyfills/http',
      https: 'rollup-plugin-node-polyfills/polyfills/http',
      os: 'rollup-plugin-node-polyfills/polyfills/os',
      assert: 'rollup-plugin-node-polyfills/polyfills/assert',
      constants: 'rollup-plugin-node-polyfills/polyfills/constants',
      _stream_duplex: 'rollup-plugin-node-polyfills/polyfills/readable-stream/duplex',
      _stream_passthrough: 'rollup-plugin-node-polyfills/polyfills/readable-stream/passthrough',
      _stream_readable: 'rollup-plugin-node-polyfills/polyfills/readable-stream/readable',
      _stream_writable: 'rollup-plugin-node-polyfills/polyfills/readable-stream/writable',
      _stream_transform: 'rollup-plugin-node-polyfills/polyfills/readable-stream/transform',
      timers: 'rollup-plugin-node-polyfills/polyfills/timers',
      console: 'rollup-plugin-node-polyfills/polyfills/console',
      vm: 'rollup-plugin-node-polyfills/polyfills/vm',
      zlib: 'rollup-plugin-node-polyfills/polyfills/zlib',
      tty: 'rollup-plugin-node-polyfills/polyfills/tty',
      domain: 'rollup-plugin-node-polyfills/polyfills/domain',
      buffer: 'rollup-plugin-node-polyfills/polyfills/buffer-es6',
      process: 'rollup-plugin-node-polyfills/polyfills/process-es6'
    },
  },
  optimizeDeps: {
    esbuildOptions: {
      // Node.js global to browser globalThis
      define: {
        global: 'globalThis'
      },
      // Enable esbuild polyfill plugins
      plugins: [
        NodeGlobalsPolyfillPlugin({
          process: true,
          buffer: true
        }),
        NodeModulesPolyfillPlugin()
      ]
    }
  },
  build: {
    rollupOptions: {
      plugins: [
        // Enable rollup polyfills plugin
        // used during production bundling
        rollupNodePolyFill()
      ]
    }
  },
});

import zlib from 'zlib';
import {Buffer} from 'buffer'

// =========================================压缩=========================================
const zip = value => {
    return new Promise((resolve, reject) => {
      zlib.gzip(value, (err, buffer) => {
        if (err) {
          console.error('压缩错误:', err);
          reject(false);
        }
        resolve(buffer.toString('base64'));
      });
    });
  },
  // ======================================解压===========================================
  unzip = value => {
    const buffer = Buffer.from(value, 'base64');
    return new Promise((resolve, reject) => {
      zlib.gunzip(buffer, (err, buffer) => {
        if (err) {
          console.error('解压错误:', err);
          reject(false);
        }
        if (buffer) resolve(buffer.toString());
      });
    });
  };
export { zip, unzip };

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值