crypto-js aes加密解密

装置

npm install crypto-js –save

unit.js

import CryptoJS from "crypto-js";
 //秘钥
const CRYPTOJSKEY= "abcdefghighkml";
 
export default {
  
  //加密
  /*
  * {param} plaintText 加密明文
  * return  str 加密效果
  */
  encrypt(plaintText) {
    var plaintText = plaintText;
    var options = {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    };
    var key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);
    var encryptedData = CryptoJS.AES.encrypt(plaintText, key, options);
    var encryptedBase64Str = encryptedData.toString();
    return encryptedBase64Str;
  },
  //解密
  /*
  * {param} plaintText 解密密文
  
  * return  str 解密效果
  */
  decrypt(encryptedBase64Str, type) {
   
    var encryptedBase64Str = encryptedBase64Str;
    var options = {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    };
    var key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);
    // 解密
    var decryptedData = CryptoJS.AES.decrypt(encryptedBase64Str, key, options);
    // 解密后,须要根据Utf8的体式格局将明文转位字符串
    var decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);
    return decryptedStr;
  }
};

运用

 //引入
 import Util from "util";
 //加密
 let userPwd = Util.encrypt('123456');
    原文作者:乖摸摸头
    原文地址: https://segmentfault.com/a/1190000013432325
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞