Article directory
1. Problem: An error is reported when referencing the jsencrypt encryption library in vue3.0
2. Reason: jsencrypt library does not support es6 ES module module export, vue3 does not support commonjs import and export
const JSEncrypt= require('@/utils/jsencrypt.min.js')
const publicKey = '....'
const priviteKey= '....'
// 加密
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(priviteKey);
return encryptor.decrypt(txt);
}
3. Solution: change the configuration file
Modify the script file to:
! function(t, e) {
t.JSEncrypt = e()
}(modificationWindow, (function() {
//....
})
export default modificationWindow
File resource: (
已经改好的文件
)
Link: https://pan.baidu.com/s/1JuwrWltoHNHMtmPiHtvymA?pwd=u651
Extraction code: u651
4. Specific use
import JSEncrypt from '@/utils/jsencrypt.min.js';
export function encrypt(txt) {
const encryptor = new JSEncrypt.JSEncrypt ();
encryptor.setPublicKey(publicKey); // 设置公钥
return encryptor.encrypt(txt); // 对需要加密的数据进行加密
}
//解密
export function decrypt(txt) {
const encryptor = new JSEncrypt.JSEncrypt ();
encryptor.setPrivateKey(priviteKey);
return encryptor.decrypt(txt);
}
successfully resolved