#!/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.

# Since this is an encryption script, we're doing the reverse of the above to produce an encrypted query string
# that will match what AtData will produce.

key="d6c60c8e688e0afa7f49416897aadc1f"
clearText="label=this_is_a_really_long_data_labelthis_is_a_really_long_data_labelthis_is_a_really_long_data_label&md5_email=337c1f89a68e64e5b8264c8d5121992c"
echo Clear text: ${clearText}
b64CipherText=$(echo -n ${clearText} | openssl aes-128-ecb -K ${key} -base64)
echo Standardized B64: ${b64CipherText}
# "echo" converts the newlines in $b64CipherText to spaces so in our sed command we're converting spaces to %20%20
atdataEqsValue=$(echo -n ${b64CipherText} | sed 's/+/-/g; s/\//_/g; s/=//g; s/ /%20%20/g;')
echo URL-friendly EQS: ${atdataEqsValue}

