Error CryptDecode - page 3

 
All sorted!

Thank you SO much Alain.

My excuse was being 7 days into a software bender that was all VB except for the MQL4 module that was left till the end :)

I did try creating a HexToArray function but could not get it to work. This is my first time on the forum and I really appreciate the support, I also like your style, not spoon-feeding us with solutions but providing pointers in the right direction so we have to work it out and therefore gain a deeper understnading of the issue.

I knew what I wanted to achieve but yesterday could not see the wood for the trees.

Next time I'll remember to format the posts correctly, its a really neat editor.

Phill!
 
asurething:
All sorted!

Thank you SO much Alain.

My excuse was being 7 days into a software bender that was all VB except for the MQL4 module that was left till the end :)

I did try creating a HexToArray function but could not get it to work. This is my first time on the forum and I really appreciate the support, I also like your style, not spoon-feeding us with solutions but providing pointers in the right direction so we have to work it out and therefore gain a deeper understnading of the issue.

I knew what I wanted to achieve but yesterday could not see the wood for the trees.

Next time I'll remember to format the posts correctly, its a really neat editor.

Phill!
Welcome. Happy to help.
 

I stumbled over same problem and found that the origin of this problem is the function

StringToCharArray

which also copies the termination zero to uchar array. So the string + 0 . The example exposed on

https://docs.mql4.com/common/cryptencode

can't work. You need to replace

StringToCharArray

by your own function, not copying termination 0 to array because this is done in StringToCharArray. 

int my_StringToCharArray(string& source,uchar& dest[])
{
   int len=StringLen(source);
   if(len > 0)
   {
      StringToCharArray(source,dest,0,len); 
   }
   return len;
}

This way, all possible encryptions (except the hashes of course) are reversible.

CryptEncode - Common Functions - MQL4 Reference
CryptEncode - Common Functions - MQL4 Reference
  • docs.mql4.com
CryptEncode - Common Functions - MQL4 Reference
 

I think might my code can help a little

 
   string text="The quick brown fox jumps over the lazy dog";
   string keystr="01234567890123456789012345678901";
   uchar src[],dst[],key[],b64[];
         
   StringToCharArray(keystr,key,0,StringLen(keystr));
   StringToCharArray(text,src,0,StringLen(text));
         
   CryptEncode(CRYPT_AES256,src,key,dst);   
   CryptEncode(CRYPT_BASE64,dst,key,b64);

   Print("B64="+CharArrayToString(b64));


Expected output: NsFJlGQScUEazmSEykVeO8+GIc+waNCNu4TUsr7v7yhEQLoJ+XRFSpMaG3G8TkxT // This is confirmed from an online tool.
Got:             NsFJlGQScUEazmSEykVeO8+GIc+waNCNu4TUsr7v7yjJd+HmS5mZCcoTvtTHaIxk
I am facing issue with MQL4 encryption. Please see above code, the expected output vs actual. Why is the Base64 not matching? To be specific, the last part of the base64s are not matching. Any help will be greatly appreciated.
 
Joh:
I am facing issue with MQL4 encryption. Please see above code, the expected output vs actual. Why is the Base64 not matching? To be specific, the last part of the base64s are not matching. Any help will be greatly appreciated.

I was thrown off on the same issue when checking with an online tool for decoding/encoding. The problem is in PHP ( which is likely what you come across if you look online) is that there are more options than MQL4.

PHP:

$key = "01234567890123456789012345678901";

$method = "AES-256-ECB";

$out = openssl_decrypt ($in, $method , $key, OPENSSL_ZERO_PADDING);

will work with your MQL4 solution, however:

$out = openssl_decrypt ($in, $method , $key);

will not, the onlne tool you used probably did not have the "OPENSSL_ZERO_PADDING" flag set.

 
Michael Kroeker:

Thanks Michael for your response. The online tool(I used https://www.devglan.com/online-tools/aes-encryption-decryption) is having only limited options. So I have tried this using Ruby language.

Following is the MQL4 code base:

string text="The quick brown fox jumps over the lazy dog";
string keystr="01234567890123456789012345678901";
uchar src[],dst[],key[],b64[];
         
StringToCharArray(keystr,key,0,StringLen(keystr));
StringToCharArray(text,src,0,StringLen(text));
         
CryptEncode(CRYPT_AES256,src,key,dst);   
CryptEncode(CRYPT_BASE64,dst,key,b64);

Print("MQL Base64="+CharArrayToString(b64));

Following is the equivalent Ruby code base:

require 'openssl'
require 'base64'
data = "The quick brown fox jumps over the lazy dog"
cipher = OpenSSL::Cipher.new('AES-256-ECB')
cipher.encrypt
cipher.key = "01234567890123456789012345678901"
e = cipher.update(data) + cipher.final
b64 = Base64.encode64(e)
puts "Ruby-Base64=#{b64}"

Output from MQL4 and Ruby. Any help what is happening here? In Ruby there is no option to set any Padding:

MT4 B64  - NsFJlGQScUEazmSEykVeO8+GIc+waNCNu4TUsr7v7yjJd+HmS5mZCcoTvtTHaIxk
Ruby B64 - NsFJlGQScUEazmSEykVeO8+GIc+waNCNu4TUsr7v7yhEQLoJ+XRFSpMaG3G8\nTkxT\n
online   - NsFJlGQScUEazmSEykVeO8+GIc+waNCNu4TUsr7v7yhEQLoJ+XRFSpMaG3G8TkxT  // This is the online tool output which is irrelevant for this discussion, however just sharing

Again,  the MT4 Base64 and Ruby Base64 are different, specifically the last part. What am I missing? Do I need to do something on Ruby side or MQL4 side?

Reason: