WeChat applet implements CBC encryption/decryption

First of all, you can download my resource WeChat mini program CBC encryption tool.
Insert image description here
After downloading and decompressing, it will be a crypto-js folder.
Insert image description here
This is actually a third-party dependency, but there is currently a problem with the npm installation of the mini program.
Add this tool to your project
and then We encapsulate an encryption.js
reference code in the project as follows

import CryptoJS from './crypto-js/index'

const key = CryptoJS.enc.Utf8.parse("秘钥");
const iv = CryptoJS.enc.Utf8.parse("初始化向量");

// 加密函数
export function encryptByCBC(text) {
    
    
    var cipherText = CryptoJS.AES.encrypt(text, key, {
    
    
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    return cipherText.toString();
}

// 解密函数
export function decryptByCBC(cipherText) {
    
    
    var decryptedText = CryptoJS.AES.decrypt(cipherText, key, {
    
    
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    return decryptedText.toString(CryptoJS.enc.Utf8);
}

First of all, you need to pay attention to the key secret key and the iv initial vector. You need to communicate with the backend. This is a specification for encryption and decryption. Then
introduce this encryption where it needs to be used and
use its encryptByCBC encryption method
Insert image description here
because what I encrypt is a Base64. The string will appear a bit long
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_45966674/article/details/133303455