Base 64 Encoding using Adobe Campaign Standard

 For one of the integration we had to do the base64 encoding using ACS and it was not that easy initially with the existence of the "MYTH" that ACS doesn't support JavaScript. Here is the code where I declared a class Converted inside a Content Block (it has to be block, not fragment) named "cbBase64EncodeLib"

Note this process is only applicable if you have to use the encoded output inside email, it will not work if you need to save the value in a table

<%

var Converter ={

        _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

        hexToBase64: function(hex) {

            //var str = Converter.hexToString(hex);

            //return Converter.base64Encode(str);


            var hexStr = hex.toString(); //force conversion


            var binary = "";


            for (var i = 0; i < hexStr.length; i++) {

                var h = hexStr.charAt(i);

                temp = parseInt(h, 16).toString(2);

                binary += this.padStart(temp, 4, "0");

            }


            var noOfPading = 0;

            var paddingString = "";

            var noOfBits = hexStr.length * 4;


            switch (noOfBits % 6) {

                case 2:

                    noOfPading = 4;

                    binary += "0000"; //Adding 4 zeroes at the beginning as per Base64 standard

                    paddingString = "==";

                    break;

                case 4:

                    noOfPading = 2;

                    binary += "00"; //Adding 4 zeroes at the beginning as per Base64 standard

                    paddingString = "=";

                    break;

                default:

                    noOfPading = 0;

            }

            //Now the binary string length is now dividable by 6


            noOfBits = binary.length;

            var noOfbase64Chars = noOfBits / 6;

            var base64Str = "";

            for (var i = 0; i < noOfbase64Chars; i++) {

                var bin6Char = binary.substr(i * 6, 6);

                var intOfBin = parseInt(bin6Char, 2);

                base64Str += this._keyStr.charAt(intOfBin);

            }

            base64Str +=paddingString;

            return base64Str;

        },

        padStart : function(str, len, charToPad){

            var padedStr = str;

            while(padedStr.length < len )

            {

                padedStr = charToPad + padedStr;

            }

            return padedStr;

        }

    };

%>


Calling the function from another Content Block as below

<%@ include fragment='cbBase64EncodeLib' %>

<% 

var hexValue =  context.targetData.tyEncText; //Reading the value from inside email

var base64EncodedStr = Converter.hexToBase64(hexValue);

//If you have to use it in URL

var encryptedURL = "http://myurl.com" + "?param=" + escapeUrl(base64EncodedStr);

%>

<a href="<%= encryptedURL %>">My Link</a>

Comments

  1. hi , this is a great article .I have tried the above code , however i am getting the below error .
    JST-310008 Failed to process directives '<%@ include' (content textdefaultContent).
    DLV-490041 Unknown content block '#39;cbBase64EncodeLib', or you do not have the appropriate rights to access its definition.

    ReplyDelete
  2. The first part of the code is related to declaring the 'cbBase64EncodeLib'. From the error it seems you didn' t declare the content block not fragment

    ReplyDelete

Post a Comment

Popular posts from this blog

Avoid Proxy for HttpClientRequest - IOB-090007 Network error (send(), errno=10054: an existing connection was forcibly closed by the remote host

Adobe Campaign: Call Java Script from Input Form

Call stored procedure from Adobe Campaign Classic