- rsa加密有长度限制 如果数据太大(不建议使用) 解密会失败
- 配合压缩解压来进行使用
- 前端解密需要私钥 后端返回的私钥格式默认是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 };