uniapp vue3版本引用 jsencrypt加密库报错:“default“ is not exported by……

个人产生这个异常的原因:将历史项目(vue2)的jsencrypt文件复制到新项目(vue3)里直接引用报错。存在兼容问题,需要重新安装vue3版本的jsencrypt

安装依赖

复制代码
npm install jsencrypt

页面引入

js 复制代码
import { JSEncrypt } from 'jsencrypt'

简单封装

js 复制代码
import {
	JSEncrypt
} from 'jsencrypt'
// 密钥对生成 http://web.chacuo.net/netrsakeypair
const publicKey ='你的公钥'
const privateKey = '你的私钥'

// 加密
export function encrypt(txt) {
	const encryptor = new JSEncrypt()
	encryptor.setPublicKey(publicKey) // 设置公钥
	return encryptor.encrypt(txt) // 对数据进行加密
}

// 解密
export function decrypt(txt) {
	const encryptor = new JSEncrypt()
	encryptor.setPrivateKey(privateKey) // 设置私钥
	return encryptor.decrypt(txt) // 对数据进行解密
}

具体使用

js 复制代码
<script>
	import {
		encrypt
	} from '@/utils/rsa/jsencrypt-vue3.js'
	export default {
		data() {
			return {
			}
		},
		methods: {
			startLogin() {		
			    var param = {
					username: 'xxxxx',
					
					// 调用加密方法,完成密码的rsa加密
					password: encrypt('xxxxxx')
				}
			}
		}
	}
</script>

参考资料

uniapp vue3版本 Android 引用 jsencrypt加密库 报错问题 "default" is not exported by,解决方法
前端VUE3使用RSA加密向后端传输密码
微信小程序使用npm方式增加jsencrypt rsa加密验证

相关推荐
biter down5 小时前
14:pytest-order 插件 顺序控制案例
开发语言·python·pytest
郝学胜-神的一滴6 小时前
Qt 高级开发 009: C++ Lambda 表达式
开发语言·c++·qt·软件构建
星栈独行6 小时前
我在 Rust 全栈项目里用 JWT 做无状态认证
开发语言·后端·rust·前端框架·开源·github·web
石山代码6 小时前
C++ 轻量级日志系统
开发语言·c++
小技与小术7 小时前
玩转Flask
开发语言·python·flask
SilentSamsara7 小时前
Python 性能优化:tracemalloc、profiling 与 C 扩展加速
开发语言·python·青少年编程·性能优化
冰小忆7 小时前
大驼峰命名规范和小驼峰命名规范的区别是什么?
开发语言·python
晓说前端7 小时前
第一篇:为什么学TypeScript?—— 优势、场景与环境搭建
javascript·ubuntu·typescript
ZC跨境爬虫8 小时前
模块化烹饪小程序开发日记 Day7:(菜谱详情接口开发与JSON数据读取全流程)
前端·javascript·css·ui·微信小程序·json
এ慕ོ冬℘゜9 小时前
JS 前端基础面试题
开发语言·前端·javascript