Home > Web Front-end > Vue.js > body text

How to implement password encryption in vuejs

藏色散人
Release: 2021-10-27 15:24:15
Original
6886 people have browsed it

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

How to implement password encryption in 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
Copy after login
Copy after login
npm install crypto-js -D
Copy after login
Copy after login

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

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>
Copy after login

所以之后我使用淘宝镜像进行安装

cnpm install crypto-js -D
Copy after login
Copy after login
cnpm install crypto-js -D
Copy after login
Copy after login

安装成功:

√ 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)
Copy after login

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

在src-assets文件夹下创建js文件 cryp.js
How to implement password encryption in 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;
Copy after login

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

<script>
  import  cryptoJSObj  from  &#39;@/assets/cryp.js&#39;
  export default {
  name: &#39;Login&#39;,
  data(){
    // 手机号码验证
    var contactPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error(&#39;手机号不能为空&#39;))
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error(&#39;请输入正确的手机号&#39;))
        }
      }
    };
    return{
      loading:false,
      form: {
        account: &#39;&#39;,
        password: &#39;&#39;,
      },

      formRules: {// 新增或编辑验证规则
        account: [
          { required: true, message: &#39;账号不能为空&#39; }
        ],
        password: [
          { required: true, message: &#39;请输入密码&#39;, trigger: &#39;blur&#39; },
          { min: 13, message: &#39;密码长度应大于12位&#39;, trigger: &#39;blur&#39; },
          { pattern: /^(?=.*[a-zA-Z])(?=.*[1-9])(?=.*[\W]).{13,}$/, message: &#39;必须包含大小写字母、数字的组合、特殊字符,长度大于12位&#39; }
        ],
      },
    }
  },
  created() {

  },
  methods:{
    startLogin:(){
      let password=cryptoJSObj.encryptFunc(form.password)
      //此处password为加密后的密码,form.password为输入的密码
    },
  }}</script>
Copy after login

到这里就全部完成了。

推荐:《最新的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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template