Detailed steps for using AES.js in Vue

Detailed steps for using AES.js in Vue

Use of AES encryption

Data transmission encryption and decryption processing---AES.js

first step:

Install crypto-js dependency in vue

npm install crypto-js --save-dev

Step 2:

Create a new AES.js file in the static directory, for example:

Step 3:

Fill in the following code in AES.js

import CryptoJS from "crypto-js";
// npm install crypto-js --save-dev
//Randomly generate a specified number of 32-bit keys
export default {
  generatekey(num) {
    let library =
      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    let key = "";
    for (var i = 0; i < num; i++) {
      let randomPoz = Math.floor(Math.random() * library.length);
      key += library.substring(randomPoz, randomPoz + 1);
    }
    return key;
  },
  //encrypt(word, keyStr) {
    keyStr = keyStr ? keyStr : "CXMGNcYwTrtsadQmV935ONNXMUycpG1g"; //Judge whether ksy exists. If not, use the defined key
    var key = CryptoJS.enc.Utf8.parse(keyStr);
    var srcs = CryptoJS.enc.Utf8.parse(word);
    var encrypted = CryptoJS.AES.encrypt(srcs, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.toString();
  },
  //decryptiondecrypt(word, keyStr) {
    keyStr = keyStr ? keyStr : "CXMGNcYwTrtsadQmV935ONNXMUycpG1g";
    var key = CryptoJS.enc.Utf8.parse(keyStr);
    var decrypt = CryptoJS.AES.decrypt(word, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    return CryptoJS.enc.Utf8.stringify(decrypt).toString();
  }
};

Step 4:

Introduce where encryption is needed

import AES from "@/common/AES.js";

Step 5:

Call

//If it is an object/array, you need to convert it into a string by JSON.stringify first //Call the encryption method var encrypts = AES.encrypt(JSON.stringify(cars),keys);
//Call the decryption method var dess = JSON.parse(AES.decrypt(encrypts,keys));
console.log(encrypts)
console.log(encrypts.length)
console.log(dess) 

This concludes this article on the detailed steps of using AES.js in Vue. For more relevant content about using AES.js in Vue, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to implement AES data encryption based on python3 and Vue
  • Using AES to implement password encryption and decryption in vue project (ECB and CBC modes)

<<:  Detailed analysis of several situations in which MySQL indexes fail

>>:  Docker nginx implements one host to deploy multiple sites

Recommend

Solution to changing the data storage location of the database in MySQL 5.7

As the data stored in the MySQL database graduall...

Five guidelines to help you write maintainable CSS code

1. Add a comment block at the beginning of the sty...

How to use pdf.js to preview pdf files in Vue

When we preview PDF on the page, some files canno...

Detailed discussion of several methods for deduplicating JavaScript arrays

Table of contents 1. Set Deduplication 2. Double ...

Div css naming standards css class naming rules (in line with SEO standards)

There are many tasks to be done in search engine o...

Implementing image fragmentation loading function based on HTML code

Today we will implement a fragmented image loadin...

MySQL max_allowed_packet setting

max_allowed_packet is a parameter in MySQL that i...

Understanding render in Vue scaffolding

In the vue scaffolding, we can see that in the ne...

Installation steps of docker-ce on Raspberry Pi 4b ubuntu19 server

The Raspberry Pi model is 4b, 1G RAM. The system ...

MySQL backup table operation based on Java

The core is mysqldump and Runtime The operation i...

How to use MySQL limit and solve the problem of large paging

Preface In daily development, when we use MySQL t...

Using MySQL in Windows: Implementing Automatic Scheduled Backups

1. Write a backup script rem auther:www.yumi-info...

How to use SVG icons in WeChat applets

SVG has been widely used in recent years due to i...