Как да декриптирате AES с Google Apps Script

Опитвам се да дешифрирам AES с GAS. Целта на декриптирането е файл с документ, извлечен от API на Amazon Selling Partner.
Ключът, iv и URL се получават от API и искам да дешифрирам изтеглените данни чрез достъп до URL адреса с ключа и iv.
Дешифрираният текст обаче е празен или изкривен.
Можете ли да ми кажете какво не е наред със следния код? Кодът използва cCryptoGS, която е обвиваща библиотека за CryptoJS.

const decrypt_test = () => {
  const url = 'https://tortuga-prod-fe.s3-us-west-2.amazonaws.com/%2FNinetyDays/amzn1.tortuga.3.5d4685fe-cdf1-4f37-8dfc-a25b85468e34.T1J5QXLEXAMPLE';

  const response = UrlFetchApp.fetch(url);
  const file = response.getContentText();

  const key = 'xiZ8FGT6pYo49ZwfvAplJxKgO0qW46Morzs5aEXAMPLE';
  const iv = 'aoGh0rhbB3ALlCFKiEXAMPLE';
  const enc_key = cCryptoGS.CryptoJS.enc.Base64.parse(key);
  const enc_iv = cCryptoGS.CryptoJS.enc.Base64.parse(iv);
  const cipherParams = cCryptoGS.CryptoJS.lib.CipherParams.create({
      ciphertext: file//cCryptoGS.CryptoJS.enc.Base64.parse(file)
  });

  console.log(`enc_key_length:${enc_key.words.length}`);
  console.log(`enc_iv_length:${enc_iv.words.length}`);

  const decryptedMessage = cCryptoGS.CryptoJS.AES.decrypt(cipherParams, enc_key, { iv: enc_iv, mode: cCryptoGS.CryptoJS.mode.CBC}).toString();

  console.log(`decryptedMessage:${decryptedMessage}`);

  return decryptedMessage;
};

[изход]

2021/06/20 20:04:04 debug   enc_key_length:8
2021/06/20 20:04:04 debug   enc_iv_length:4
2021/06/20 20:04:04 debug   decryptedMessage:bfc095f3ecec221e8585ceb68031078d25112f5f26ea2c1f80470f5f4f19f2e1c2cd94638e8666c3486fa29191b568bcd9e8d5a3bdcbbc05456f0567bb6cdae675fa044f94e560379d16b1d370cd7c4a9c5afbbcf4fde2694ed01c1b7950eaabc65e46c4640d8f0814bfe66e8ae65f7768136ac4615624be25373d665ee8fde82742e26664d7c09c61ac8994dc3052f0f22d5042f0b407d696e3c84a3906350dc60c46001ef7865d0c6594c57c5af22616688e028f52d4f12b538d0580c420fdcb0ee61287d4ee2629cd7d39f739d63e84dd75e948eaffb4383076f0c66997

person thimone    schedule 20.06.2021    source източник
comment
Позовах се на тези отговори за декриптиране в CryptoJS и декриптиране на документацията на SP API Как да декриптирате AES с CryptoJS Декриптиране на документ за отчет на Amazon SP API с помощта на python   -  person thimone    schedule 20.06.2021


Отговори (1)


Следният код реши проблема

const decrypt_test = () => {
  const url = 'https://tortuga-prod-fe.s3-us-west-2.amazonaws.com/%2FNinetyDays/EXAMPLE';

  let options = {
    'method': 'get',
    'muteHttpExceptions': true,
  };

  const response = UrlFetchApp.fetch(url, options);
  
  const file = response.getBlob().getBytes();

  const key = 'xiZ8FGT6pYo49ZwfvAplJxKgO0qW46MoEXAMPLE';
  const iv = 'aoGh0rhbB3ALlCFKiuJEXAMPLE';
  const enc_key = cCryptoGS.CryptoJS.enc.Base64.parse(key);
  const enc_iv = cCryptoGS.CryptoJS.enc.Base64.parse(iv);
  const cipherParams = cCryptoGS.CryptoJS.lib.CipherParams.create({
      ciphertext: cCryptoGS.CryptoJS.enc.Hex.parse(hexes(file))
  });

  const decryptedMessage = cCryptoGS.CryptoJS.AES.decrypt(cipherParams, enc_key,
                           { iv: enc_iv, mode: cCryptoGS.CryptoJS.mode.CBC}).toString();

  console.log(`decryptedMessage:${decryptedMessage}`);

  const bin = bytes(decryptedMessage)
  const myBlob = Utilities.newBlob(bin, MimeType.TEXT, "decrypted.csv");
  DriveApp.createFile(myBlob);
};

const bytes = (hexstr) => {
  ary = [];
    for (var i = 0; i < hexstr.length; i += 2) {
      ary.push(parseInt(hexstr.substr(i, 2), 16));
  }
  return ary;
}

const hexes = (ary) => {
  return ary.map((e) => ( '00' + (e < 0 ? e += 0x0100 : e).toString(16)).slice(-2)).join('')
}
person thimone    schedule 11.07.2021