No result from decode with pass data between 2 routines.

Chaiya Srisawat  



Hello,

I am write MT5, and would like to encode some data files, So i create the 2 routine for encode and decode password for testing,

however the decode is not come when pass data between 2 routines, Please advise what is the wrong!

..

int OnInit()

{

uchar crytPWD[];

PassWord_Encode("455555",crytPWD);

string pwd=PassWord_Decode(crytPWD);

return(INIT_SUCCEEDED);

}

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

//| Decrypt CryptCode information and return the Client

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

string PassWord_Decode(uchar &cryptPassword[])

  {

   string   MasterKey;

   uchar dst[],key[];

   MasterKey="BEOK777";

   StringToCharArray(MasterKey,key);

   CryptDecode(CRYPT_HASH_SHA256,cryptPassword,key,dst);

   Print("1-DeCode..crypt: ",CharArrayToString(cryptPassword),", DST: ",CharArrayToString(dst));

   return(CharArrayToString(dst));

  }

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

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

//| Encrypt client information and return the password

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

void PassWord_Encode(string PassWord, uchar &cryptPassword[])

  {

   string   MasterKey;

   uchar src[],key[];

   MasterKey="BEOK777";

   StringToCharArray(MasterKey,key);

   StringToCharArray(PassWord,src);

   CryptEncode(CRYPT_HASH_SHA256,src,key,cryptPassword);

   Print("3-EnCode..SRC: ",CharArrayToString(src),", DST: ",CharArrayToString(cryptPassword));

  }

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

pwd result

but result of decode routine is not come..

What wrong of this decode?

chaiya

Petr Nosek  
Chaiya Srisawat:



Hello,

I am write MT5, and would like to encode some data files, So i create the 2 routine for encode and decode password for testing,

however the decode is not come when pass data between 2 routines, Please advise what is the wrong!

..

but result of decode routine is not come..

What wrong of this decode?

chaiya

I'm not absolutely sure but I think you can use CRYPT_HASH_SHA256 only to hash (you can't use this constant in CryptDecode()). CryptEncode(CRYPT_HASH_SHA256,...) creates 256 bit key that you can use with CRYPT_AES256. I've edited your functions so that they work properly.

#define KEY_LENGTH (32)
//+------------------------------------------------------------------+
//| Decrypt CryptCode information and return the Client              |
//+------------------------------------------------------------------+
string PassWord_Decode(uchar &cryptPassword[])
  {
   string MasterKey="BEOK777";
   uchar src[],dst[],key[],hashkey[];
//--- Creating hash key
   ArrayResize(key,KEY_LENGTH);
   for(int i=0;i<KEY_LENGTH;i++) key[i]=uchar((i*17)%59);
//--- Hash MasterKey
   StringToCharArray(MasterKey,src,0,StringLen(MasterKey));
   ResetLastError();
   if(CryptEncode(CRYPT_HASH_SHA256,src,key,hashkey)<1) printf("Error encode: %d",_LastError);
//--- Decode with hashed MasterKey
   ResetLastError();
   if(CryptDecode(CRYPT_AES256,cryptPassword,hashkey,dst)<1) printf("Error decode: %d",_LastError);
   Print("1-DeCode..crypt: ",CharArrayToString(cryptPassword),", DST: ",CharArrayToString(dst));
   return(CharArrayToString(dst));
  }

//+------------------------------------------------------------------+
//| Encrypt client information and return the password               |
//+------------------------------------------------------------------+
void PassWord_Encode(string PassWord, uchar &cryptPassword[])
  {
   string MasterKey="BEOK777";
   uchar src[],key[],hashkey[];
//--- Creating hash key
   ArrayResize(key,KEY_LENGTH);
   for(int i=0;i<KEY_LENGTH;i++) key[i]=uchar((i*17)%59);
//--- Hash MasterKey
   StringToCharArray(MasterKey,src,0,StringLen(MasterKey));
   ResetLastError();
   if(CryptEncode(CRYPT_HASH_SHA256,src,key,hashkey)<1) printf("Error encode: %d",_LastError);
//--- Encode with hashed MasterKey
   StringToCharArray(PassWord,src,0,StringLen(PassWord));
   ResetLastError();
   if(CryptEncode(CRYPT_AES256,src,hashkey,cryptPassword)<1) printf("Error encode: %d",_LastError);
   Print("3-EnCode..SRC: ",CharArrayToString(src),", DST: ",CharArrayToString(cryptPassword));
  }
Icham Aidibe  

Wow ! I didn't know there was an encryption routine in MQL ! 

Chaiya Srisawat  
Petr Nosek:

I'm not absolutely sure but I think you can use CRYPT_HASH_SHA256 only to hash (you can't use this constant in CryptDecode()). CryptEncode(CRYPT_HASH_SHA256,...) creates 256 bit key that you can use with CRYPT_AES256. I've edited your functions so that they work properly.

Thank you very much.  It is very complex programming for me to do so.. chaiya