node.js破译AES 128 ECB

node.js Decipher AES 128 ECB

我尝试在节点上使用 AES 128-ecb 破译 base64 格式的令牌。

key: ed9d26Z0JES0X52Q (改变了一些字符,但长度是正确的)

令牌:O4girrZ2YeLSE1sZ4FSIvp3Edm1GiwBLHmvDIEYCf+xkvbxP6EfYjy+PEB2kaYe0606EyPmlCC0iExVRq9e3Iw==

  decodeToken(token) {
    var key = new Buffer(exchangeKey, 'hex')
    var encrypted = new Buffer(token, 'base64')
    decipher = crypto.createDecipheriv("aes-128-ecb", key, '')
    decipher.setAutoPadding(false)
    result = decipher.update(encrypted).toString();
    return result;
  }

给出:

crypto.js:239 this._handle.initiv(cipher, toBuf(key), toBuf(iv)); ^

Error: Invalid key length at Error (native) at new Decipheriv (crypto.js:239:16) at Object.Decipheriv (crypto.js:236:12)

经过一番搜索,我发现了这个:

// https://github.com/nodejs/node-v0.x-archive/issues/4744#issuecomment-25460050
var aesEcb = new MCrypt('rijndael-128', 'ecb')
aesEcb.open(exchangeKey);
var ciphertext = new Buffer(token, 'base64');
var plaintext = aesEcb.decrypt(ciphertext).toString();
return plaintext

什么回馈

f9712fa5-da4a-49fe-b81f-b48d8cfabf91275RAODW24RS

看起来像预期的格式和长度,但请注意末尾的连线字符。

我也可以使用这个解决方案(以及 trim 额外的字符),但我想

当您打算使用单个十六进制字符作为密钥字节时,您正在将密钥解码为十六进制。不要那样做。您还禁用了填充。启用填充以删除奇怪的字符。