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加密验证

相关推荐
程序猿_极客1 小时前
【2025 年最新版】Java JDK 安装与环境配置教程(附图文超详细,Windows+macOS 通用)
java·开发语言·windows·macos·jdk
二哈喇子!4 小时前
BOM模型
开发语言·前端·javascript·bom
二哈喇子!4 小时前
Vue2 监听器 watcher
前端·javascript·vue.js
二哈喇子!4 小时前
空指针异常
开发语言
咚为4 小时前
Rust Print 终极指南:从底层原理到全场景实战
开发语言·后端·rust
%xiao Q5 小时前
GESP C++五级-202406
android·开发语言·c++
Psycho_MrZhang5 小时前
Neo4j Python SDK手册
开发语言·python·neo4j
Traced back5 小时前
# C# + SQL Server 实现自动清理功能的完整方案:按数量与按日期双模式
开发语言·c#
sin22015 小时前
MyBatis的执行流程
java·开发语言·mybatis
web3.08889995 小时前
1688图片搜索API,相似商品精准推荐
开发语言·python