Error CryptDecode

Back to topics list  | 1 2 To post a new topic, please log in or register
avatar
2
sergiome35 2016.05.18 20:31 #
 
franzk:

Thank you for your response. The code actually comes from the example provided in the documentation for CrpytEncode() here: https://docs.mql4.com/common/cryptencode

It should just point out where my problem is.

Seems I have to add this workaround in order to make it work

 

using dst3 for decrypting afterwards because after converting the string to an array it is adding a "0" character to the array messing with the arraysize CryptDecode is expecting. Looks like a bug in StringToCharArray() maybe? In my real code I am now calling the function below instead of StringToCharArray() 

All solved now (hopefully) ;)

Thanks again everyone! 

Actually, that works fine:

 

 void OnStart()

//+------------------------------------------------------------------+

{

   string text="The quick brown fox jumps over the lazy dog";

   string keystr="9ADE769DE7B2D51F6EBBC3CC7F744ZY5";


   uchar src[],dst[],key[];

   //--- prepare key

   StringToCharArray(keystr,key);

   

   //--- copy text to source array src[]

   StringToCharArray(text,src);

   

   //--- print initial data

   PrintFormat("Initial data: size=%d, string='%s'",ArraySize(src),CharArrayToString(src));

   

   //--- encrypt src[] with AES 256-bit key in key[]

   int res=CryptEncode(CRYPT_AES256,src,key,dst);


   //--- check error

   if(res>0)

     {

      //--- print encrypted data

      PrintFormat("Encoded data: size=%d %s",res,ArrayToHex(dst));

      //--- decode dst[] to src[]


      //--- decrypt dst[] with AES 256-bit key in key[]

      res=CryptDecode(CRYPT_AES256,dst,key,src);


      //--- check error     

      if(res>0)

        {

         //--- print decoded data

         PrintFormat("Decoded data: size=%d, string='%s'",ArraySize(src),CharArrayToString(src));

        }

      else

         Print("Error in CryptDecode. Error code=",GetLastError());

     }

   else

      Print("Error in CryptEncode. Error code=",GetLastError());

}


string ArrayToHex(uchar &arr[],int count=-1)

  {

   string res="";

//--- check

   if(count<0 || count>ArraySize(arr))

      count=ArraySize(arr);

//--- transform to HEX string

   for(int i=0; i<count; i++)

      res+=StringFormat("%.2X",arr[i]);

//---

   return(res);

  }

 

avatar
3
zschmied 2016.07.02 18:39 #
 
You need to set the size of the char array with StringLen() in the function call:

string String = "Some string";
uchar  CharString[];
StringToCharArray(String,CharString,0,StringLen(String));

The array will then be the proper size. You'd think this would happen automatically... When dealing with function issues, always look up the parameters: https://docs.mql4.com/convert/stringtochararray
avatar
2
Leonardo 2016.07.16 11:08 #
 

My snippet for this post, but remember this rule first :

 

 

 

I made a class with 2 functions "Encrypt" and "DeCrypt" :

 

class Crypter{
   
   public :
   
      Crypter( void );
               
      ~Crypter( void );

      string EnCrypt
      (
         ENUM_CRYPT_METHOD method,
         string toKey,
         string toEnCrypt
      
      );
      
      string DeCrypt
      (
         ENUM_CRYPT_METHOD method,
         string toKey,
         string toDeCrypt
      
      );

};

Crypter::Crypter( void ){ /* TODO */ };
Crypter::~Crypter( void ){ /* TODO */ };

string AwesomeUtilityGlobalClass_1_0_0::EnCrypt
(
   ENUM_CRYPT_METHOD method,
   string toKey,
   string toEnCrypt

){

   uchar src[],dst[],key[];
   
   StringToCharArray( toKey, key, 0, StringLen( toKey ) );
   StringToCharArray( toEnCrypt,src, 0, StringLen( toEnCrypt ) );

   int res = CryptEncode( method, src, key, dst );
   
   return ( res > 0 ) ? CharArrayToString( dst ) : "";

};

string AwesomeUtilityGlobalClass_1_0_0::DeCrypt
(
   ENUM_CRYPT_METHOD method,
   string toKey,
   string toDeCrypt

){

   uchar src[],dst[],key[];
   
   StringToCharArray( toKey, key, 0, StringLen( toKey ) );
   StringToCharArray( toDeCrypt, src, 0, StringLen( toDeCrypt ) );   

   int res = CryptDecode( method, src, key, dst );

   return ( res > 0 ) ? CharArrayToString( dst ) : "";

};

 

 

Save this class with "Crypt.mqh", then import in to your project for example :

#include "Crypt.mqh"

// Global object
Crypter AU;

// Now test your pass keys and method
int OnInit(){
   
   string MexToCrypt = "Awesome message for you, Hello World ! XD";
   string Password = "This Is My Password ! Ehmmmm you like my password ?";
   Print( MexToCrypt, " ", Password );
   
   ENUM_CRYPT_METHOD CryptoMethod   = CRYPT_AES128;
   ENUM_CRYPT_METHOD CryptoMethodTO = CRYPT_BASE64;
   
   string Crypted = AU.EnCrypt( CryptoMethod, Password, MexToCrypt );
   string CryptedTo64 = AU.EnCrypt( CryptoMethodTO, Password, Crypted );
   PrintFormat( "B64 : %s", CryptedTo64 );
   
   string DecryptedFrom64 = AU.DeCrypt( CryptoMethodTO, Password, CryptedTo64 );
   string DeCrypted = AU.DeCrypt( CryptoMethod, Password, DecryptedFrom64 );   
   PrintFormat( "Decripted : %s", DeCrypted );
            
   return( INIT_SUCCEEDED );

}

 

 Result :

 

 

 

I hope this help, enjoy ;) 

 

Back to topics list   | 1 2  

To add comments, please log in or register