泡菜哥
泡菜哥
  • 发布:2023-12-03 20:17
  • 更新:2023-12-03 20:31
  • 阅读:302

Crypto.js 后端解密

分类:uni-app

uniapy 里面引用的加密方法-crypto.js

数据加密后传到后端,由于后端是python写的,需要用python解密。折腾了大半天2边的加密解密都对不上。有没有crypto.js 对应的python后端解密代码?

下面是crypto.js的代码:

// aes192算法 - 加密  
function encryptUseAes192(data, key) {  
    const cipher = crypto.createCipher('aes192', key);  
    let encrypted = cipher.update(data, 'utf8', 'hex');  
    encrypted += cipher.final('hex');  
    // encrypted 为加密后的内容  
    return encrypted;  
}  

// aes192算法 - 解密  
function decryptUseAes192(data, key) {  
    // aes192 算法  
    let decrypted;  
    try {  
        const decipher = crypto.createDecipher('aes192', key);  
        decrypted = decipher.update(data, 'hex', 'utf8');  
        decrypted += decipher.final('utf8');  
        try {  
            decrypted = JSON.parse(decrypted);  
        } catch (err) {}  
    } catch (err) {  
        throw "解密失败";  
    }  
    // decrypted 为解密后的内容,即最开始需要加密的原始数据文本data  
    return decrypted;  
}  

// aes-256-ecb算法 - 加密  
function encryptUseAes256Ecb(data, key) {  
    let paddedData = Buffer.from(data);  
    let paddedkey = key;  
    if (paddedkey.length > 32) {  
        paddedkey = paddedkey.substring(0, 32);  
    }  
    paddedkey = Buffer.from(paddedkey);  
    const cipher = crypto.createCipheriv('aes-256-ecb', paddedkey, '');  
    cipher.setAutoPadding(false);  
    const blockSize = 16; // AES块大小为16字节  
    const paddingSize = blockSize - (paddedData.length % blockSize);  
    const paddingBuffer = Buffer.alloc(paddingSize, paddingSize);  
    paddedData = Buffer.concat([paddedData, paddingBuffer]);  
    let encrypted = cipher.update(paddedData, null, 'base64');  
    encrypted += cipher.final('base64');  
    return encrypted;  
}  

// aes-256-ecb算法 - 解密  
function decryptUseAes256Ecb(data, key) {  
    let paddedkey = key;  
    if (paddedkey.length > 32) {  
        paddedkey = paddedkey.substring(0, 32);  
    }  
    paddedkey = Buffer.from(paddedkey);  
    let decrypted;  
    try {  
        const decipher = crypto.createDecipheriv('aes-256-ecb', paddedkey, '');  
        decipher.setAutoPadding(false);  

        let decrypted = decipher.update(data, 'base64');  
        decrypted += decipher.final();  

        const lastByte = decrypted.charCodeAt(decrypted.length - 1);  
        const paddingSize = lastByte;  
        decrypted = decrypted.slice(0, decrypted.length - paddingSize);  
        try {  
            decrypted = JSON.parse(decrypted);  
        } catch (err) {}  
        return decrypted;  
    } catch (err) {  
        throw "解密失败";  
    }  
    return decrypted;  
}
2023-12-03 20:17 负责人:无 分享
已邀请:
L***@163.com

L***@163.com

qwer123

  • HRK

    请勿回复与问题无关的话语哦

    2023-12-03 21:21

要回复问题请先登录注册