Home > Article > Web Front-end > How to implement password encryption in vuejs
vuejs实现密码加密的方法:1、通过npm引入“crypto-js”依赖;2、创建js文件引入“crypto-js”并写入加密方法;3、在需要加密的组件内使用cryptoObj加密方法即可。
本文操作环境:windows7系统、vue2.9.6版,DELL G3电脑。
1、npm引入crypto-js依赖
2、创建js文件引入crypto-js并写入加密方法
3、在需要加密的组件内使用cryptoObj加密方法
npm install crypto-js -D
npm install crypto-js -D
若出现报错,我的报错如下(可能是因为使用了淘宝镜像):
npm ERR! code 1npm ERR! path E:\Users\yidu_\Documents\pccm-screen\node_modules\node-sass npm ERR! command failed npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node-gyp rebuild npm ERR! gyp info it worked if it ends with ok npm ERR! gyp info using node-gyp@3.8.0npm ERR! gyp info using node@14.15.1 | win32 | x64 npm ERR! gyp ERR! configure error npm ERR! gyp ERR! stack Error: Command failed: D:\ProgramData\Anaconda3\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3];npm ERR! gyp ERR! stack File "<string>", line 1npm ERR! gyp ERR! stack import sys; print "%s.%s.%s" % sys.version_info[:3];npm ERR! gyp ERR! stack ^npm ERR! gyp ERR! stack SyntaxError: invalid syntax npm ERR! gyp ERR! stack npm ERR! gyp ERR! stack at ChildProcess.exithandler (child_process.js:308:12)npm ERR! gyp ERR! stack at ChildProcess.emit (events.js:315:20)npm ERR! gyp ERR! stack at maybeClose (internal/child_process.js:1048:16)npm ERR! gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)npm ERR! gyp ERR! System Windows_NT 10.0.19042npm ERR! gyp ERR! command "D:\\Program Files\\nodejs\\node.exe" "E:\\Users\\yidu_\\Documents\\pccm-screen\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebu ild" npm ERR! gyp ERR! cwd E:\Users\yidu_\Documents\pccm-screen\node_modules\node-sass npm ERR! gyp ERR! node -v v14.15.1npm ERR! gyp ERR! node-gyp -v v3.8.0npm ERR! gyp ERR! not ok npm ERR! A complete log of this run can be found in:npm ERR! D:\Program Files\nodejs\node_cachel\_logs\2021-05-06T07_10_11_380Z-debug.log</string>
所以之后我使用淘宝镜像进行安装
cnpm install crypto-js -D
cnpm install crypto-js -D
安装成功:
√ Installed 1 packages √ Linked 0 latest versions √ Run 0 scripts √ All packages installed (1 packages installed from npm registry, used 283ms(network 278ms), speed 4.58kB/s, json 1(1.27kB), tarball 0B)
在src-assets文件夹下创建js文件 cryp.js
在cryp.js文件中引入crypto-js并写入加密方法:
import CryptoJS from 'crypto-js'var cryptoObj = { /* 加密 */ encryptFunc: (message) => { var key = '12345678900';//前后端约定好的秘钥 var keyHex = CryptoJS.enc.Utf8.parse(key); var encrypted = CryptoJS.AES.encrypt(message, keyHex, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); return encrypted.toString(); },}export default cryptoObj;
<script> import cryptoJSObj from '@/assets/cryp.js' export default { name: 'Login', data(){ // 手机号码验证 var contactPhone = (rule, value, callback) => { if (!value) { return callback(new Error('手机号不能为空')) } else { const reg = /^1[3|4|5|7|8][0-9]\d{8}$/ if (reg.test(value)) { callback() } else { return callback(new Error('请输入正确的手机号')) } } }; return{ loading:false, form: { account: '', password: '', }, formRules: {// 新增或编辑验证规则 account: [ { required: true, message: '账号不能为空' } ], password: [ { required: true, message: '请输入密码', trigger: 'blur' }, { min: 13, message: '密码长度应大于12位', trigger: 'blur' }, { pattern: /^(?=.*[a-zA-Z])(?=.*[1-9])(?=.*[\W]).{13,}$/, message: '必须包含大小写字母、数字的组合、特殊字符,长度大于12位' } ], }, } }, created() { }, methods:{ startLogin:(){ let password=cryptoJSObj.encryptFunc(form.password) //此处password为加密后的密码,form.password为输入的密码 }, }}</script>
到这里就全部完成了。
推荐:《最新的5个vue.js视频教程精选》
The above is the detailed content of How to implement password encryption in vuejs. For more information, please follow other related articles on the PHP Chinese website!