base64_encode_decode

Author: Copyright 2016, MetaQuotes Software Corp.
base64_encode_decode

This script is designed to perform a specific type of data transformation known as Base64 Encoding and Decoding. In simple terms, it takes a piece of plain text and converts it into a different format that is often used for transmitting data safely over the internet or embedding text within files where standard characters might cause issues.

How the Logic Works

1. Input Processing The script starts by taking a specific piece of text provided by the user (for example, a simple phrase like "Text Example").

2. The Encoding Process (Base64Encode) The script breaks down the plain text into its raw numerical components. It then maps these numbers against a predefined standardized alphabet (which includes uppercase letters, lowercase letters, numbers, and specific symbols).

  • It reorganizes the data into groups of bits to create a new string of characters.
  • If the text doesn't fit perfectly into these groups, the script adds "padding" symbols (like =) at the end to ensure the final output is formatted correctly.
  • The result is a scrambled-looking string that represents your original text.

3. The Decoding Process (Base64Decode) Conversely, the script contains a second logic block designed to reverse the transformation. It takes the "scrambled" Base64 string and uses the same standardized alphabet to translate those characters back into their original numerical values.

  • It reconstructs the binary data from the encoded characters.
  • It then converts those numbers back into readable plain text.

4. Execution and Display When the script is initialized in the trading platform, it automatically performs both actions in sequence:

  1. It encodes your input text and saves the result.
  2. It immediately takes that encoded result and decodes it back to its original state.
  3. Finally, it displays a pop-up message box showing you both the "Encoded" version and the "Decoded" version side-by-side so you can see the transformation in real-time.

Summary for Users

Think of this script as a translator. It takes your message, translates it into a specialized "code" used by computers to move data around reliably, and then immediately translates that code back into English so you can verify that no information was lost during the process.

Miscellaneous
It opens Message Boxes to the user
6 Views
0 Downloads
0 Favorites
base64_encode_decode
//+------------------------------------------------------------------+
//|                                               EncryptDecrypt.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input string   inputtext="Text Example";      // Input Text

static uchar ExtBase64Encode[64]=
  {
   'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
   'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
   '0','1','2','3','4','5','6','7','8','9','+','/'
  };

static uchar ExtBase64Decode[256]=
  {
   -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1, -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
   -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  62,  -1,  -1,  -1,  63, 52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  -1,  -1,  -1,  -2,  -1,  -1,
   -1,   0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  -1,  -1,  -1,  -1,  -1,
   -1,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  -1,  -1,  -1,  -1,  -1,
   -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,-1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
   -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,-1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
   -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,-1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
   -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,-1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1 
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Base64Encode(string in,string &out)
  {
   int i=0,pad=0,len=StringLen(in);
   while(i<len)
     {
      int b3,b2,b1=StringGetCharacter(in,i);
      i++;
      if(i>=len) { b2=0; b3=0; pad=2; }
      else
        {
         b2=StringGetCharacter(in,i);
         i++;
         if(i>=len) { b3=0; pad=1; }
         else       { b3=StringGetCharacter(in,i); i++; }
        }
      int c1=(b1 >> 2);
      int c2=(((b1 & 0x3) << 4) | (b2 >> 4));
      int c3=(((b2 & 0xf) << 2) | (b3 >> 6));
      int c4=(b3 & 0x3f);

      out=out+CharToString(ExtBase64Encode[c1]);
      out=out+CharToString(ExtBase64Encode[c2]);
      switch(pad)
        {
         case 0:
            out=out+CharToString(ExtBase64Encode[c3]);
            out=out+CharToString(ExtBase64Encode[c4]);
            break;
         case 1:
            out=out+CharToString(ExtBase64Encode[c3]);
            out=out+"=";
            break;
         case 2:
            out=out+"==";
            break;
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Base64Decode(string in,string &out)
  {
   int i=0,len=StringLen(in);
   int xshift=0,accum=0;
   while(i<len)
     {
      int value=ExtBase64Decode[StringGetCharacter(in,i)];
      if(value<0 || value>63) break;
      accum<<=6;
      xshift+=6;
      accum|=value;
      if(xshift>=8)
        {
         xshift-=8;
         value=accum>>xshift;
         out=out+CharToString((uchar)(value  &0xFF));
        }
      i++;
     }
  }
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   string res_encrypt;string res_decrypt;
   Base64Encode(inputtext,res_encrypt);
   Base64Decode(res_encrypt,res_decrypt);
   MessageBox("Base64Encode for '"+inputtext+"' is : "+res_encrypt+"\n"+"Base64Decode for '"+res_encrypt+"' is : "+res_decrypt,"Result",0);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+

Comments