#!/bin/bash

# Key: d6c60c8e688e0afa7f49416897aadc1f
# Encryption method: AES-128-ECB

# Note: Pre-treatment of the base64 string may be necessary to make it usable by some base64 decoding methods.
#   - All occurrences of "-" (minus) must be changed to "+" (plus)
#   - All occurrences of "_" (underscore) must be changed to "/" (slash)
#   - All occurrences of "%20%20" must be removed or converted to carriage return + linefeed or just linefeed.
#   - The length of the string, after whitespace has been removed, must be evenly divisible by 4. If it is not,
#     add one or two "=" (equals) characters to make it evenly divisible by 4.

key="d6c60c8e688e0afa7f49416897aadc1f" # 128-bit/16-byte key, represented as series of sixteen hex doubles

atdataEqsValue="AeZFhZnm_ytYdj06wE5ikO4vPv302-SG9Osx_itiNbxF4gIY3CHdBpvpxd6Jaxvx%20%207i8-_fTb5Ib06zH-K2I1vEXiAhjcId0Gm-nF3olrG_HuLz799NvkhvTrMf4rYjW8%20%20rJmJDtsRTiTCWYfZfF6SDcHuTfwpOhubzIEAFNIeJRoOtRe-zev31MASpLRr2A_g%20%20wGPEJDvSZnokppAKPaWe2w";

# Change some characters as specified above
b64CipherText=$(echo -n ${atdataEqsValue} | sed 's/-/+/g; s/_/\//g; s/%20%20//g;')
echo URL-friendly EQS: ${atdataEqsValue}
# Apply "=" padding as specified above
size=${#b64CipherText}
mod4=$((${size} % 4))
fourminus=$(expr 4 - ${mod4})
newmod4=$((${fourminus} % 4))
for ((i=1; i<=${newmod4}; i++)); do
  # Add one equals sign for every mod4 diff
  b64CipherText=${b64CipherText}=
done
echo Standardized B64: ${b64CipherText}

clearText=$(echo ${b64CipherText} | openssl enc -d -aes-128-ecb -K ${key} -base64)
echo Clear text: ${clearText}
