0

I am trying to decrypt AES with GAS. The target of decryption is a document file retrieved by Amazon Selling Partner API.
The key, iv, and URL are obtained by the API, and I want to decrypt the data downloaded by accessing the URL with the key and iv.
However, the decrypted text is either empty or garbled.
Can you please tell me what is wrong with the following code? The code uses cCryptoGS, which is a wrapper library for 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;
};

[output]

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
thimone
  • 1
  • 4
  • I referred to these answers for decryption in CryptoJS and decryption of the SP API documentation [How to decrypt AES with CryptoJS](https://stackoverflow.com/a/66815024) [Decrypting Amazon SP API Report Document using python](https://stackoverflow.com/a/66865573) – thimone Jun 20 '21 at 11:56

1 Answers1

0

The following code solved the problem

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('')
}
thimone
  • 1
  • 4