1. Install js-base64 library
npm install js-base64 --save-dev
2. Introduce js-base64 library in Vue components
import { Base64 } from 'js-base64';
3. Use js-base64 library for encryption
// Encrypted stringconst str = 'hello world'; const encodedStr = (str); (encodedStr); // Output: aGVsbG8gd29ybGQ= // Decrypt the stringconst decodedStr = (encodedStr); (decodedStr); // Output: hello world
Use the() method to encrypt the string, and use the() method to decrypt the encrypted string. You can use these methods to encrypt and decrypt strings as needed. Please note that encrypted strings may contain special characters and need to be URL-encoded or Base64-encoded before they can be passed in the URL.
Other encryption methods
crypto-js
crypto-js is a JavaScript encryption library that supports a variety of encryption algorithms, including AES, DES, Triple DES, RC4, SHA-1, SHA-256, MD5, etc. You can use crypto-js in your Vue project to implement encryption and decryption.
jsencrypt
jsencrypt is a JavaScript RSA encryption library that supports RSA encryption and decryption operations. You can use jsencrypt in your Vue project to implement RSA encryption and decryption.
bcryptjs
bcryptjs is a JavaScript encryption library that supports bcrypt algorithms. You can use bcryptjs in your Vue project to implement password encryption and verification.
-js
// Import crypto-js libraryconst CryptoJS = require('crypto-js'); // Data to be encryptedconst data = 'Hello, world!'; // Encryptionconst encryptedData = (data, 'secret key 123').toString(); ('Encrypted data:', encryptedData); // Decryptconst decryptedData = (encryptedData, 'secret key 123').toString(.Utf8); ('Decrypted data:', decryptedData);
// Import jsencrypt libraryconst JSEncrypt = require('jsencrypt').default; // Data to be encryptedconst data = 'Hello, world!'; // Create public and private key instancesconst encryptKey = new JSEncrypt(); const decryptKey = (); // Set public and private keys (real keys are required in actual use)('-----BEGIN PUBLIC KEY-----\n' + PUBLIC_KEY + '\n-----END PUBLIC KEY-----'); ('-----BEGIN RSA PRIVATE KEY-----\n' + PRIVATE_KEY + '\n-----END RSA PRIVATE KEY-----'); // Encrypt dataconst encryptedData = (data); ('Encrypted data:', encryptedData); // Decrypt the dataconst decryptedData = (encryptedData); ('Decrypted data:', decryptedData);
// Import bcryptjs libraryconst bcrypt = require('bcryptjs'); const saltRounds = 10; // Set the number of iterations of salt (recommended to be at least 10)const password = 'password'; // Password to be encryptedconst hashedPassword = (password, saltRounds); // Encrypt password (get hash password)
This is the end of this article about the encryption methods in vue. For more related vue encryption content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!