站长资讯网
最全最丰富的资讯网站

vuejs怎么实现密码加密

vuejs实现密码加密的方法:1、通过npm引入“crypto-js”依赖;2、创建js文件引入“crypto-js”并写入加密方法;3、在需要加密的组件内使用cryptoObj加密方法即可。

vuejs怎么实现密码加密

本文操作环境:windows7系统、vue2.9.6版,DELL G3电脑。

vue中使用crypto-js实现密码加密(此处只记录了前端加密)

1、npm引入crypto-js依赖

2、创建js文件引入crypto-js并写入加密方法

3、在需要加密的组件内使用cryptoObj加密方法

1、npm引入crypto-js依赖

npm install crypto-js -D
npm install crypto-js -D

若出现报错,我的报错如下(可能是因为使用了淘宝镜像):

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、创建js文件引入crypto-js并写入加密方法

在src-assets文件夹下创建js文件 cryp.js
vuejs怎么实现密码加密
在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、在需要加密的组件内使用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视频教程精选》

赞(0)
分享到: 更多 (0)