溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

vuejs如何實現(xiàn)密碼加密

發(fā)布時間:2021-10-27 15:34:15 來源:億速云 閱讀:176 作者:小新 欄目:編程語言

這篇文章主要為大家展示了“vuejs如何實現(xiàn)密碼加密”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“vuejs如何實現(xiàn)密碼加密”這篇文章吧。

vuejs實現(xiàn)密碼加密的方法:1、通過npm引入“crypto-js”依賴;2、創(chuàng)建js文件引入“crypto-js”并寫入加密方法;3、在需要加密的組件內使用cryptoObj加密方法即可。

vue中使用crypto-js實現(xiàn)密碼加密(此處只記錄了前端加密)

1、npm引入crypto-js依賴

2、創(chuàng)建js文件引入crypto-js并寫入加密方法

3、在需要加密的組件內使用cryptoObj加密方法

1、npm引入crypto-js依賴

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

若出現(xiàn)報錯,我的報錯如下(可能是因為使用了淘寶鏡像):

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

所以之后我使用淘寶鏡像進行安裝

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
vuejs如何實現(xiàn)密碼加密
在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: {// 新增或編輯驗證規(guī)則
        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>

以上是“vuejs如何實現(xiàn)密碼加密”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI