Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use AES.js in vue

Shulou Source: shulou.com Published: 2022-06-03 07:32:10 09月16日 Update

This article mainly explains "how to use AES.js in vue". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use AES.js in vue.

Use of AES encryption

Encrypt and decrypt data transmission-AES.js

Step one:

Install crypto-js dependencies 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 generates a specified number of 32-ary keyexport default {generatekey (num) {let library = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; let key = ""; for (var I = 0; I < num; iTunes +) {let randomPoz = Math.floor (Math.random () * library.length); key + = library.substring (randomPoz, randomPoz + 1);} return key }, / / encrypt encrypt (word, keyStr) {keyStr = keyStr? KeyStr: "CXMGNcYwTrtsadQmV935ONNXMUycpG1g"; / / determine whether ksy exists. If it does not exist, 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 () }, / / decrypt decrypt (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 JSON.stringify to a string 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)

Thank you for your reading, the above is the content of "how to use AES.js in vue", after the study of this article, I believe you have a deeper understanding of how to use AES.js in vue, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Encryption learning content method code place character string object that is idea situation data data transfer array quantity file article more directory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Docker Shulou Tech Info NVidia Redmi