Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use AES.js in vue

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report