DES加解密(附带解决转义问题)-VUE

定义全局方法

复制代码
main.js

import DESKeys from "./utils/key";
import CryptoJS from "crypto-js";    // npm install crypto-js@4.2.0  

/**
 * 配置全局的加密方法(DES)
 * 参数 plaintext 需要加密的字符串
 */
Vue.prototype.$encruptionDes = function (plaintext) {
  //进行加密
  let DesKey = DESKeys;
  let key = CryptoJS.enc.Utf8.parse(DesKey);
  let encrypted = CryptoJS.DES.encrypt(plaintext, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7,
  });

  return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
};

/**
 * 配置全局的解密方法(DES)
 * 参数 ciphertext 需要解密的密文(Base64编码)
 */
Vue.prototype.$decryptionDes = function (ciphertext) {
  let DesKey = DESKeys;
  let key = CryptoJS.enc.Utf8.parse(DesKey);
  let encryptedWordArray = CryptoJS.enc.Base64.parse(ciphertext);
  let cipherParams = CryptoJS.lib.CipherParams.create({
    ciphertext: encryptedWordArray
  });
  let decrypted = CryptoJS.DES.decrypt(cipherParams, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7,
  });
  return decrypted.toString(CryptoJS.enc.Utf8);
};

// 定义反转义函数
Vue.prototype.$htmlUnescape = function(str) {
  if (!str) return '';
  const div = document.createElement('div');
  div.innerHTML = str;
  const result = div.textContent || div.innerText || '';
  div.remove(); 
  return result;
};

utils/key.js

let ab = 'CAE';
let bc = 'q2n';
let cd = 'qno';
let de = 'tM=';
let DesKeys = ab + bc + cd + de;

export default DesKeys;

使用方法

复制代码
存取之前进行编码
存:encodeURIComponent(this.bindPassword)
取:let userPassword = decodeURIComponent(this.$decryptionDes(userPassword));

console.log('返回值Des解密解决转义', this.$htmlUnescape(userAccount), 
相关推荐
pps-key6 小时前
网络攻防入门:从“黑客梦”到合法实操(生存篇)
开发语言·web安全·php
汤姆宝库7 小时前
BPE分词器的完整实现:字节对合并与词表构建
开发语言·人工智能·c#
kyriewen8 小时前
编辑弹窗点了取消,列表里的数据怎么还是变了?
前端·javascript·人工智能
Karl_fang8 小时前
🥳 再也不用拿起手机抄验证码!我做了一个 Android → Mac 的「验证码接力」工具
前端·macos·ai编程
驭渊的小故事8 小时前
订单服务如何调用商品服务?一次 RestTemplate 跨服务调用入门
java·开发语言·php
Sakura_j_h_9 小时前
MCP SDK 2.0 实战:用 MCPServer 搭本地笔记检索服务
开发语言·笔记·python·qt·ai工具·mcp·mcpserver
微笑星河q9 小时前
C++STL【vector】常用接口函数使用和迭代器失效问题
开发语言·c++
谭光志9 小时前
如何让 AI 高效工作
前端·后端·ai编程
code2cat10 小时前
【随笔】MCP长任务:查询已经返回,为什么任务还没有完成
javascript·人工智能·后端·mcp
Eider199810 小时前
Python基础语法之并发相关3
开发语言·python