使用node.js解密base64密文

时间:2019-06-06 23:28:55

标签: node.js pycrypto

我有这个密文:

7GfBCT3ZLyS+yZ3gmyMi6zPneB7Chuu7gitxlZbPlA47Y2hpKPhfxqkW8pr7Jygv/ew7Oqk+YG9fn+yCh6KcrKb+1UfdrdLNr1FdFxbQvhFVr76My1Mm4NYDE+a8q/FCIiTjPvukGhxlR6qiS5qfg9bl3IwpJJfetSemqLNGNSQw6LJXP15HvfZUjmuV3ezsvwsX2fOsdUbvgHI22Owp85tC8KVq79RAA58wHardPRSr4I1kjsF8kDg0jSJefoaNvf64XaiySPQCqKNBDjBQNKLs1SQvMxFzATMBNtSm/dwN8Ul8OO3WxNH0aNIOcbB786v8pYdPpYYd0im93ze0rUlC+DCRDnMS42wSSL7L0PaoHD24HKp8MTyQ/x7xRUCquKZ7P8g/MCq8dFvb86RfuDIF+I5H+T//jvwMXI3rvRoM5ajXnhEuN+ylRmJT+IqOQEVpuZ2yZdnydtbV31GP4k0MCORC7gxtcnR9TjZ/YWpTLuVTN+MA+FColvgiMzNDwWzIeHvuu45RT8M9N7aEU6SWyCnjar73ceb9q6qhIUZXnu3fS1BfY1fhZWwX3xWFRyF+aNwv5d+h2rKMb2kRCNPMsCiNR8ozunweMHA69mW4PxbuWXtARuKY4CsmquM8kAXUZTTi2SpX7Jl06EMApaqZRpw8PS2hajy5rTL8xedonCzeG5UdwKNSQIjw3c5rpL9eXpyOqAIsIxphKTGHeKR6a6yo/TSY2naiYf4rnas5ymvYd2rTJTq9M3QWAbdwwPBBBhn9znR1xUZ7+Flj2asjc+/5NgIWuHw0mbxRLFYMnsIiStN1ySjmdtjgAHvfpcgQBwa9LrnuQHFYZoFN1dnTdiuLburLmscsUXRseNXNEGavBuRK63c9+fPH6Zj20Ej3iYZV79IAxEPNzCJ/LO4RHNxWHqN/C77uheCtQ3rX7RxA+crqwXJ5r5ExazQvJTFBHi6FK4eS65TqAjVpBYZm/PAaec4TUICGw2HN3clwQj6YF4qB0k2t5TbrP1gb6Y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TO41cr1ii7gzoCx7UTyw72yzpOXE0faUaX7iwUzEGRBl4cpS+XUUEvc5fArRtStfb7IlAAjFIPXfTJ80C0holP5sdh4i1Wo3vj2JGZrXg8UhVyOMjKGVuVQFRX2ix+zBWJmLlC3PV26RB4oxJGE6UOObqFbOXC/QYKp8JTbyHV1vWcVoExDM88jGSO3AOiGFx0qJnTBoaLAbqldl2vn519eLBsbksPE5al4AFem211Jkr7xsCkTN2BOJ1dH3EN3tJbBfp48ApN85L381Dz1ZHelnzzz5Y4XYm89Pys6aqazkm1/glh4SxOWVxUQ/FCG6kdzfe/Ra7Mmn31XgVY8MV8vwy4HTnvo2iw+tWuVLw5URZQDla5CnS94zvHohqAl62Tx6vHtXphewvh9agmzpqpmmi/7TPawykGCHAP8yT89fiIw+yntofcV9R2A8pUfMjyOM4vpCvlGIlWxlJHj7HAXvcd6le7iQichOFMZ8PjYtvaDDtY1m9JdclfmNpKrL/Zsr/AplV6JxubIAk8yX009TqiDUb/IA4Q1o9NH1+TZjr0r7y88eIrPJeZbVjOCFEgurPS3NFOKmJdsvqfh7GtsOnxIFdDZvHN9AN21uStvFO/NcqCsYy0IRFZxZN88muYcJAkIrj4CLzD4qm9OAYP/rcS42FRYJNNHaSjbl7b53tyX11ulWi2bTjK7o9IPeinf3rIXNoaQ42BAbP+8JLXRw59E1al2INIkt7q38Qnwekf/wOcPhFAHW2yY7AebVdSiEJyFzI0fqIrfCt734urzDb5lejT77O1K8eX7/ppKRlYUytddxqGgAdrvJxUq8sdF1aUnwS7AbqWPaUG+YA9Q66jBA==

使用此python函数进行加密 encrypt_tuple

from Crypto import Random
from Crypto.Cipher import AES

def encrypt_tuple(tupla, key):
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(key, AES.MODE_CBC, iv)
    return base64.b64encode(iv + cipher.encrypt(pad(str(tupla))))

def pad(s):
    return s + ((16 - len(s) % 16) * '*')

我还有用python编写的解密功能:

def decrypt_tuple(ciphertext, key):
    """Decifratura simmetrica della tupla in modalità CBC con AES"""
    text = base64.b64decode(ciphertext)
    iv = text[:16]
    cipher = AES.new(key, AES.MODE_CBC, iv)
    dec = cipher.decrypt(text[16:]).decode('utf-8')
    return dec

它工作正常,但我需要使用nodejs解密此密文。

加密密钥是这样的:

key = b'\x13\xe3`X\x06J\x0b\x04\xeb\xc4\x82\xbdV\\\x83\xb7'

我尝试了所有操作,但无法使其正常运行。 我关注了this example,但没有关注...

var crypto = require('crypto');

password = 'abcabcabc1abcabcabc1abcabcabc132';
var cryptoStr = 'Q336OpFur65nt1NgGUebbgx5hmwpcH3iUEd4mXq8qVwXL91qpLSaFecgKpsVvQEiT0DOMwK3TpUksPnjbr3wKA==';

var buf = new Buffer(cryptoStr, 'base64');
var iv = buf.toString('binary', 0, 16);
var crypt = buf.toString('base64', 16);

var decipher = crypto.createDecipheriv('aes-256-cbc', password, iv);
decipher.setAutoPadding(false);
var dec = decipher.update(crypt,'base64','utf-8');
dec += decipher.final('utf-8'); 

console.log('Decrypted content: ' + dec);

编辑

我的nodejs解密函数是这样的:

var buf = new Buffer(data, 'base64');
var keyBuff = new Buffer('\x13\xe3`X\x06J\x0b\x04\xeb\xc4\x82\xbdV\\\x83\xb7');
var iv = buf.toString('binary', 0, 16);
var crypt = buf.toString('base64', 16);
var decipher = crypto.createDecipheriv('aes-128-cbc', keyBuff, iv);
decipher.setAutoPadding(false);
var dec = decipher.update(crypt,'base64','utf-8');
dec += decipher.final('utf-8'); 

但是它不起作用...会引发此错误:

Invalid IV length

1 个答案:

答案 0 :(得分:0)

这是我要解决的问题:

我拿了钥匙:

key = b'\x13\xe3`X\x06J\x0b\x04\xeb\xc4\x82\xbdV\\\x83\xb7'

然后在python shell上,使用以下命令将其转换为base64:

base64.b64encode(b'\x13\xe3`X\x06J\x0b\x04\xeb\xc4\x82\xbdV\\\x83\xb7')

结果是这样的:

b'E+NgWAZKCwTrxIK9VlyDtw=='

然后,我复制了该字符串,但不包含第一个字符('b'),并将其粘贴到替换旧密钥的nodeJs脚本中。

我还更改了解密函数的代码,这是新代码:

function decypherData(data){
    var buf = new Buffer(data, 'base64');
    var key = new Buffer('E+NgWAZKCwTrxIK9VlyDtw==', 'base64');
    var iv = buf.slice(0, 16);
    var crypt = buf.slice(16);
    var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
    decipher.setAutoPadding(false);
    var dec = decipher.update(crypt,'base64','utf-8');
    dec += decipher.final('utf-8'); 
    return dec;
}

如您所见,我不再使用旧密钥,而是使用base64版本。

这是解密的输出:

"{'end': '1451717060986', 'segments': [{'start': '1451716695254', 'end': '1451717060986'}], 'manipulations': [{'start': '1451716696410', 'end': '1451716701152', 'sensor': 'NotRefrigeratedFood'}, {'start': '1451716700915', 'end': '1451716702675', 'sensor': 'WaterBottle'}, {'start': '1451716704106', 'end': '1451716704949', 'sensor': 'Pasta'}, {'start': '1451716706301', 'end': '1451716707303', 'sensor': 'NotRefrigeratedFood'}, {'start': '1451716706612', 'end': '1451716707792', 'sensor': 'Pasta'}, {'start': '1451717037742', 'end': '1451717041675', 'sensor': 'WaterBottle'}, {'start': '1451717044121', 'end': '1451717045349', 'sensor': 'WaterBottle'}, {'start': '1451717048433', 'end': '1451717049664', 'sensor': 'WaterBottle'}, {'start': '1451717051376', 'end': '1451717056824', 'sensor': 'WaterBottle'}], 'start': '1451716695254', 'type': 'Clean Up', 'events': [{'state': 'END', 'sensor': 'M1', 'time': '1451716699424'}, {'state': 'ON', 'sensor': 'R1', 'time': '1451716702389'}, {'state': 'OFF', 'sensor': 'R1', 'time': '1451716705403'}, {'state': 'ON', 'sensor': 'R2', 'time': '1451717047949'}, {'state': 'OFF', 'sensor': 'R2', 'time': '1451717052971'}]}*******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************"

感谢@ dave_thompson_085,这使我注意到了AES-256-CBC问题!