泡菜哥
泡菜哥
  • 发布:2023-12-04 16:01
  • 更新:2023-12-04 16:01
  • 阅读:268

利用Crypto.js前端加密 ,python后端解密问题

分类:uni-app

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

前端数据加密后传到python写的后端系统,所以需要用python代码来解密。现在2边的加密解密算法和机制好像匹配不上。有人帮忙看一下crypto.js 对应的解密代码python该如何写?教程里面只提供了java和PHP的示例。

下面是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;    
}

举例,比如加密 “Hello, world!” 得到的加密字符串是:
3f49c5a31c650853f2691585cdbe45ff
key = '1234567890123456'

这是目前使用的python解密代码,一直报错:

from Crypto.Cipher import AES  
import base64  
def aes_decrypt(key, encrypted_text):  
    key_bytes = key.encode('utf-8')  
    key_bytes += b'\x00' * (16 - len(key_bytes) % 16)  
    aes = AES.new(key_bytes, AES.MODE_ECB)  
    encrypted_bytes = base64.b64decode(encrypted_text.encode('utf-8'))  
    decrypted_bytes = aes.decrypt(encrypted_bytes).rstrip(b'\x00')  
    return decrypted_bytes.decode('utf-8')
2023-12-04 16:01 负责人:无 分享
已邀请:

要回复问题请先登录注册