vuejs實現(xiàn)密碼加密的方法:1、通過npm引入“crypto-js”依賴;2、創(chuàng)建js文件引入“crypto-js”并寫入加密方法;3、在需要加密的組件內(nèi)使用cryptoObj加密方法即可。
本文操作環(huán)境:windows7系統(tǒng)、vue2.9.6版,DELL G3電腦。
vue中使用crypto-js實現(xiàn)密碼加密(此處只記錄了前端加密)
1、npm引入crypto-js依賴
2、創(chuàng)建js文件引入crypto-js并寫入加密方法
3、在需要加密的組件內(nèi)使用cryptoObj加密方法
1、npm引入crypto-js依賴
npm install crypto-js -D
npm install crypto-js -D
若出現(xiàn)報錯,我的報錯如下(可能是因為使用了淘寶鏡像):
npm ERR! code 1npm ERR! path E:Usersyidu_Documentspccm-screennode_modulesnode-sass npm ERR! command failed npm ERR! command C:WINDOWSsystem32cmd.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:ProgramDataAnaconda3python.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:Usersyidu_Documentspccm-screennode_modulesnode-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 Filesnodejsnode_cachel_logs2021-05-06T07_10_11_380Z-debug.log
所以之后我使用淘寶鏡像進行安裝
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)
2、創(chuàng)建js文件引入crypto-js并寫入加密方法
在src-assets文件夾下創(chuàng)建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;
3、在需要加密的組件內(nèi)使用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: {// 新增或編輯驗證規(guī)則 account: [ { required: true, message: '賬號不能為空' } ], password: [ { required: true, message: '請輸入密碼', trigger: 'blur' }, { min: 13, message: '密碼長度應(yīng)大于12位', trigger: 'blur' }, { pattern: /^(?=.*[a-zA-Z])(?=.*[1-9])(?=.*[W]).{13,}$/, message: '必須包含大小寫字母、數(shù)字的組合、特殊字符,長度大于12位' } ], }, } }, created() { }, methods:{ startLogin:(){ let password=cryptoJSObj.encryptFunc(form.password) //此處password為加密后的密碼,form.password為輸入的密碼 }, }}</script>
到這里就全部完成了。
推薦:《最新的5個vue.js視頻教程精選》