Home > Article > Web Front-end > How to implement AES encryption using JavaScript
With the popularization of the Internet and the increasingly frequent data transmission, data security issues are becoming increasingly important. In order to ensure data security, encryption algorithms are widely used. AES (Advanced Encryption Standard) is one of the most commonly used symmetric encryption algorithms currently and is widely used for encryption protection during data transmission and storage.
This article will introduce in detail how to use JavaScript to implement AES encryption.
1. Understanding AES Encryption
The AES encryption algorithm is a symmetric encryption algorithm, that is, the same key is used for encryption and decryption, usually called the "secret key". AES can encrypt and decrypt data. The key length of AES can be 128 bits, 192 bits or 256 bits. By using keys of different lengths, different levels of security can be achieved.
The AES encryption algorithm is essentially a replacement and permutation encryption algorithm that uses a key and an initialization vector (IV) to perform encryption or decryption operations to produce ciphertext or plaintext. The AES encryption algorithm is divided into three modes: electronic codebook (ECB), cipher block chaining (CBC) and counter mode (CTR). This article uses Advanced Encryption Standard mode (CTR).
2. Introduction of CryptoJS library
CryptoJS is a powerful JavaScript encryption library that supports multiple encryption algorithms, including AES encryption. Before we start implementing AES encryption, we need to introduce the CryptoJS encryption library. CryptoJS can be introduced through JavaScript script tags or directly using downloaded js files.
In order to reflect the integrity and practicality of AES encryption, here we choose download.min.js to introduce the CryptoJS library.
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script>
3. Implementing AES encryption
It is very simple to implement AES encryption using CryptoJS. You only need to pass in the plaintext and key to get the encrypted ciphertext. The following is an example of AES encryption based on CTR mode:
/** * AES加密方法 * @param {*} data 明文 * @param {*} key 密钥 * @returns 密文 */ function aesEncrypt(data, key) { // OpenSSL向量 var iv = CryptoJS.lib.WordArray.random(16); var encrypted = CryptoJS.AES.encrypt(data, key, { iv: iv, mode: CryptoJS.mode.CTR, padding: CryptoJS.pad.Pkcs7 }); // 将密钥和向量进行拼接,一起返回 return encrypted.ciphertext.toString(CryptoJS.enc.Base64) + ':' + iv.toString(CryptoJS.enc.Hex); }
The above code takes the incoming plaintext and key as parameters, generates a random vector and uses CTR mode for AES encryption, and finally adds the encrypted ciphertext and Vector concatenation and return.
4. Implementing AES decryption
It is equally easy to implement AES decryption using CryptoJS. As long as we pass in the encrypted ciphertext and key, we can get the decrypted plaintext.
/** * AES解密方法 * @param {*} data 密文 * @param {*} key 密钥 * @returns 明文 */ function aesDecrypt(data, key) { // 拆分密文和向量 var parts = data.split(':'); var ciphertext = CryptoJS.enc.Base64.parse(parts[0]); var iv = CryptoJS.enc.Hex.parse(parts[1]); var decrypted = CryptoJS.AES.decrypt({ ciphertext: ciphertext }, key, { iv: iv, mode: CryptoJS.mode.CTR, padding: CryptoJS.pad.Pkcs7 }); return decrypted.toString(CryptoJS.enc.Utf8); }
The above code takes the incoming ciphertext and key as parameters, splits the ciphertext and vector, uses CTR mode to perform the AES decryption operation, and finally returns the decrypted plaintext.
5. Test AES encryption and decryption
After our above code is implemented, the AES encryption and decryption test is as follows:
// 初始化密钥 var key = CryptoJS.enc.Utf8.parse('qwer1234qwer1234'); // 加密测试 var data = 'Hello World'; var encrypted = aesEncrypt(data, key); console.log('加密后的密文:', encrypted); // 解密测试 var decrypted = aesDecrypt(encrypted, key); console.log('解密后的明文:', decrypted);
Run the above code to test AES encryption and decryption function, you can see the AES-encrypted ciphertext and decrypted plaintext output on the console.
6. Summary
The above is the specific process of using JavaScript to implement AES encryption. With the help of CryptoJS encryption library, we can easily implement AES encryption and decryption. The AES encryption algorithm is widely used in the field of data security. If you are concerned about data security issues, you can learn and use this algorithm to implement data encryption.
The above is the detailed content of How to implement AES encryption using JavaScript. For more information, please follow other related articles on the PHP Chinese website!