首页 > 其他分享 >Vue使用AES加密

Vue使用AES加密

时间:2024-05-16 10:19:50浏览次数:8  
标签:AES Vue 加密 decrypt js key encrypt CryptoJS

1. 安装crypto-js库:

npm install crypto-js
# 或
yarn add crypto-js

2. 封装encryption.js

// utils/encryption.js
import CryptoJS from 'crypto-js';

// AES加密
export function encrypt(text, key) {
  return CryptoJS.AES.encrypt(text, key).toString();
}

// AES解密
export function decrypt(ciphertext, key) {
  const bytes = CryptoJS.AES.decrypt(ciphertext, key);
  return bytes.toString(CryptoJS.enc.Utf8);
}

3. 使用

<template>
  <div>
    <h1>AES Encryption Example</h1>
    <input v-model="inputText" placeholder="Enter text to encrypt" />
    <input v-model="encryptionKey" placeholder="Enter encryption key" />
    <button @click="handleEncrypt">Encrypt</button>
    <button @click="handleDecrypt">Decrypt</button>
    <p>Encrypted Text: {{ encryptedText }}</p>
    <p>Decrypted Text: {{ decryptedText }}</p>
  </div>
</template>

<script>
// 导入封装好的加解密方法
import { encrypt, decrypt } from '@/utils/encryption';

export default {
  data() {
    return {
      inputText: '',
      encryptionKey: '',
      encryptedText: '',
      decryptedText: ''
    };
  },
  methods: {
    handleEncrypt() {
      this.encryptedText = encrypt(this.inputText, this.encryptionKey);
    },
    handleDecrypt() {
      this.decryptedText = decrypt(this.encryptedText, this.encryptionKey);
    }
  }
};
</script>

<style scoped>
/* 添加一些简单的样式 */
input {
  margin-bottom: 10px;
  display: block;
}
button {
  margin-right: 10px;
}
</style>

标签:AES,Vue,加密,decrypt,js,key,encrypt,CryptoJS
From: https://www.cnblogs.com/lpkshuai/p/18195442

相关文章

  • elementUI中Vue 2方式<el-table>表格中列表列头内容过长,不换行处理
    使用render-header属性<el-table-column...:render-header="headerRender"></el-table-column>methods:{headerRenderer(h,{column}){//使用h函数创建VNode,防止表头内容换行returnh('div',{style:{......
  • vue 常见面试题
    1.VueJS的特点是什么?VueJS的特点有以下几个:1.简洁易用:VueJS的核心库只关注视图层,提供了简洁明了的API,便于开发者快速上手和编写代码。2.双向数据绑定:VueJS采用了基于数据劫持的双向数据绑定机制,能够自动追踪数据的变化,并通过更新视图实现数据的自动同步。3.组件化:VueJS支......
  • vue2 使用echarts实现地图点击进入下一层级+点击空白处回退
    先验知识:vue2中echarts的安装和显示中国地图:https://www.cnblogs.com/sunshine233/p/16140522.html鼠标事件: https://echarts.apache.org/zh/api.html#echartsInstance.onecharts.getMap():https://echarts.apache.org/zh/api.html#echarts.getMap监听“空白处”的事件:https:/......
  • vue 面试题
    以下是一些常见的Vue面试题:1.Vue.js是什么?有什么特点?Vue.js是一个用于构建用户界面的渐进式JavaScript框架。它采用了MVVM(模型-视图-视图模型)的架构模式,可以使开发者更容易地构建高效、可维护的Web应用程序。Vue.js具有以下特点:1.简洁:Vue.js的核心库只关注视图层,减少了学习......
  • zip伪加密
    目录1.前文:2.原理:1.压缩源文件数据区:2.压缩源文件目录区:3.压缩源文件目录结束标志:3.判断是否加密:1.无加密:2.伪加密:3.真加密:4.修改方法:5.其他途径:6.反例:(1)用binwalk-e......
  • vue 前端配置
    前端配置1axios安装cnpminstallaxios-Smain.js中importaxiosfrom'axios'Vue.prototype.$axios=axios2elementui安装cnpminstallelement-ui-Smain.js中importElementUIfrom'element-ui';import'element-ui/lib/theme-cha......
  • vue3 pinia
    pinia状态管理器,统一状态管理,组件间通信state、getter和action,我们可以假设这些概念相当于组件中的data、computed和methods1安装npminstallpinia2在store/xx.js,写入代码,可以定义多个import{defineStore}from"pinia";exportconstuseCountStore=defineS......
  • vue3的入门--setup
    代码量:200行以上博客量:1时间:2h vue2中的data和methods可以与setup并列写,但是:data和methods可以利用this调用setup中的数据而,setup中,不能调用data和methods中的数据<!--Person.vue--><template><divclass="person"><h2>姓名:{{name}}</h2>&......
  • vue监听事件实例
    <!DOCTYPEhtml><html><head><metacharset="utf-8"><title>Vue测试实例-菜鸟教程(runoob.com)</title><scriptsrc="https://cdn.staticfile.net/vue/2.4.2/vue.min.js"></script></he......
  • Vue3 vue-grid-layout布局添加右键事件
    示例code<template><divclass="dashboard-container"><ulclass='contextmenu'v-show="menuConfig.visible":style="{left:menuConfig.left+'px',top:menuConfig.top+'px'}">......