首页 > web前端 > Vue.js > 正文

vuejs怎么实现密码加密

藏色散人
发布: 2021-10-27 15:24:15
原创
7264人浏览过
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-screen
ode_modules
ode-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-screen
ode_modules
ode-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
odejs
ode_cachel_logs1-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
在这里插入图片描述
在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视频教程精选

以上就是vuejs怎么实现密码加密的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号