uniapp或小程序使用jsencrypt

uniapp或小程序使用报错

jsencrypt.js使用了window对象,app、小程序中没有窗口对象的环境

解决办法

在jsencrypt.js文件插入以下代码

var navigator2 = {
	appName: 'Netscape',
	userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
};
var window2 = {
	ASN1: null,
	Base64: null,
	Hex: null,
	crypto: null,
	href: null
};

然后替换jsencrypt.js文件中的变量:
navigator 替换为 modificationNavigator
window 替换为 modificationWindow

新的问题

jsencrypt.js在新版中依赖了很多其他的文件,不是纯净版的,需要使用旧版本的

旧版本 - 原版

原版

uniapp版

使用

// 引入
import JSEncrypt from '../../lib/jsencrypt.js'

// 使用
const encrypt = new JSEncrypt()
encrypt.setPublicKey('自己的pubKey')
let newPassword = encrypt.encrypt('输入的密码')

你可能感兴趣的:(小程序,前端,uniapp,小程序,jsencrypt,rsa)