Hello
Is it possible to encrypt a string in mt5 and send it to a php script to use it after decoded the encrypted string ?
Merci
Thank you.
Do you have an url to example because i can't find. merci
Hi everyone.
I am trying to achieve the above also
First of all please excuse my ignorance on the encryption subject.
I am learning on it.
If you feel this requires a freelance job instead I understand.
I wrote this mql5 code where you specify
-A secret key
- A word/sentence to encrypt
- The Encryption method (default base_64)
It comments and also Prints the Encoded version in Hex ,and Decodes it also.
input string Secret_Key="WHITEFOX"; input string Word_To_Encrypt="The brown bear runs"; input ENUM_CRYPT_METHOD EncryptionMethod=CRYPT_BASE64; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- uchar key[]; uchar dataencrypted[]; char data[]; // pre-encrypted data string encryptedword; string decodedword; StringToCharArray(Secret_Key,key); StringToCharArray(Word_To_Encrypt,data); //Maybe I need to change to // StringToCharArray(Secret_Key,key,0,StringLen(Secret_Key)); // StringToCharArray(Word_To_Encrypt,data,StringLen(Word_To_Encrypt)); //Encrypt data int encryptres=CryptEncode(EncryptionMethod,data,key,dataencrypted); if(encryptres>0) { encryptedword=ArrayToHex(dataencrypted); //--- print encrypted data PrintFormat("Encrypted data: size=%d %s",encryptres,encryptedword); char dataclean[]; // pre-encrypted data int res=CryptDecode(EncryptionMethod,dataencrypted,key,dataclean); if(res>0) { decodedword=CharArrayToString(dataclean); //--- print decoded data PrintFormat("Decoded data: size=%d, string='%s'",ArraySize(dataclean),CharArrayToString(dataclean)); } else Print("Error in CryptDecode. Error code=",GetLastError()); } else Print("Error in CryptEncode. Error code=",GetLastError()); Comment("Encrypted: " + encryptedword + "\n" + "Decrypted: " + decodedword); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ 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); }
What I am trying to do now is take that encrypted word "5647686C49474A796233647549474A6C59584967636E56756377413D" and decode it in PHP .
I am unable to find PHP a code that achieves that .Here is one thing I tried (I did source the internet and use also chat gpt) and I have been unsuccessful.
<?php // Specify the secret key used in MQL5 $secret_key = "WHITEFOX"; // Specify the encrypted data in hexadecimal string format $hex_encoded_data = "5647686C49474A796233647549474A6C59584967636E56756377413D"; // Convert the hexadecimal string to binary $binary_data = hex2bin($hex_encoded_data); // Get the initialization vector (IV) size based on the encryption algorithm $iv_size = openssl_cipher_iv_length("aes-128-ecb"); // Extract the IV from the binary data $iv = substr($binary_data, 0, $iv_size); // Extract the encrypted data (excluding the IV) $encrypted_data = substr($binary_data, $iv_size); // Decrypt the data using the specified secret key and IV $decrypted_data = openssl_decrypt($encrypted_data, "aes-128-ecb", $secret_key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv); // Remove padding (if applied during encryption) $decrypted_data = rtrim($decrypted_data, "\0"); // Display the decrypted data echo "Decrypted Data: " . $decrypted_data; ?>
Forum on trading, automated trading systems and testing trading strategies
CryptEncode / CryptDecode / BCrypt implementations in other programming languages
Marcin Madrzak, 2023.09.10 16:52
Here is the equivalent in PHP
<?php // Define plaintext, keystring and keyhex $plaintext = 'Mother Tell Your Children Not To Walk My Way'; $keystring = 'thisisyoursecretkeydonttellnoone'; $keyhex = bin2hex($keystring); echo "<pre>"; // Dump variables for debugging purposes dump($plaintext); dump($keystring); dump($keyhex); echo "1. Encrypt string and output the result as HEX\n"; $encrypted = CryptMQL($plaintext, $keystring, true, false, true); dump($encrypted); echo "2. Decrypt HEX input\n"; $decrypted = CryptMQL($encrypted, $keystring, false, false, true); dump($decrypted); echo "3. Encrypt string and output the result as Base64\n"; $encrypted = CryptMQL($plaintext, $keystring, true, false, false); dump($encrypted); echo "4. Decrypt Base64 input\n"; $decrypted = CryptMQL($encrypted, $keystring, false, false, false); dump($decrypted); echo "5. Encrypt using HEX key\n"; $encrypted = CryptMQL($plaintext, $keyhex, true, true, true); dump($encrypted); echo "6. Decrypt using HEX key\n"; $decrypted = CryptMQL($encrypted, $keyhex, false, true, true); dump($decrypted); echo "</pre>"; // Function to encrypt or decrypt data based on parameters function CryptMQL($data, $key, $encrypt = true, $keyIsHex = true, $encryptedIsHex = true) { // Convert key from HEX to binary if needed $key = $keyIsHex ? hex2bin($key) : $key; if ($encrypt) { // Pad data with zeros before encryption $data = padUnpadZero($data); // Encrypt data using AES-256-ECB and zero padding $result = openssl_encrypt($data, "AES-256-ECB", $key, OPENSSL_ZERO_PADDING); // Convert result to HEX if needed $result = $encryptedIsHex ? strtoupper(bin2hex(base64_decode($result))) : $result; } else { // Convert data from HEX to binary if needed $data = $encryptedIsHex ? base64_encode(hex2bin(strtolower($data))) : $data; // Decrypt data using AES-256-ECB and zero padding $result = openssl_decrypt($data, "AES-256-ECB", $key, OPENSSL_ZERO_PADDING); // Unpad data after decryption $result = padUnpadZero($result, false); } return $result; } // Function to pad or unpad data with zeros function padUnpadZero($data, $pad = true, $blocksize = 16) { if ($pad) { // Calculate number of zeros needed for padding $pad = $blocksize - (strlen($data) % $blocksize); // Add zeros to the end of data return $data . str_repeat("\0", $pad); } else { // Remove zeros from the end of data return rtrim($data, "\0"); } } // Function to dump variable for debugging purposes function dump($var) { $backtrace = debug_backtrace(); $file = file($backtrace[0]['file']); $line = $file[$backtrace[0]['line'] - 1]; preg_match("#\\$(\w+)#", $line, $match); echo "<pre>"; echo "$match[1]: "; var_dump($var); echo "</pre>"; }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello
Is it possible to encrypt a string in mt5 and send it to a php script to use it after decoded the encrypted string ?
Merci