forked from nique_372/MQLArticles
73 lines
2.5 KiB
MQL5
73 lines
2.5 KiB
MQL5
//+------------------------------------------------------------------+
| |
//| TestFile.mq5 |
| |
//| Copyright 2026, MetaQuotes Ltd. |
| |
//| https://www.mql5.com |
| |
//+------------------------------------------------------------------+
| |
#property copyright "Copyright 2026, MetaQuotes Ltd."
| |
#property link "https://www.mql5.com"
| |
#property version "1.00"
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
#include "Encrypt.mqh"
| |
| |
//+------------------------------------------------------------------+
| |
//| Script program start function |
| |
//+------------------------------------------------------------------+
| |
void OnStart()
| |
{
| |
//---
| |
const string test_path = "MQLArticles\\Encryptor\\Test.txt";
| |
const string test_content = "Hola mundo esto es un test de encriptacion";
| |
| |
//--- Creamos el archivo de prueba
| |
int fh = FileOpen(test_path, FILE_WRITE | FILE_TXT | FILE_COMMON);
| |
if(fh == INVALID_HANDLE)
| |
{
| |
Print("Fallo al crear Test.txt, error = ", GetLastError());
| |
return;
| |
}
| |
FileWriteString(fh, test_content);
| |
FileClose(fh);
| |
Print("Archivo creado: ", test_path);
| |
| |
//--- Encryptamos
| |
CEncryptor encryptor;
| |
CRandomSimple random;
| |
encryptor.SetAleatoryKey(CRYPT_AES256, RandomSimpleGenerateRandomUchar, &random);
| |
Print("Key = ", encryptor.Key());
| |
| |
if(!encryptor.EncryptFile(test_path, true, CRYPT_AES256, true, ".enc"))
| |
{
| |
Print("Fallo al encriptar");
| |
return;
| |
}
| |
Print("Archivo encriptado: ", test_path + ".enc");
| |
| |
//--- Desencryptamos
| |
const string enc_file = test_path + ".enc";
| |
if(!encryptor.DecryptFile(enc_file, true, CRYPT_AES256, false, "-")) // Quiatrmos el .enc (sobreescribimos)
| |
{
| |
Print("Fallo al desencriptar");
| |
return;
| |
}
| |
Print("Archivo desencriptado: ", test_path + ".enc.dec");
| |
| |
//--- Leemos el resultado y verificamos
| |
fh = FileOpen(test_path, FILE_READ | FILE_TXT | FILE_COMMON);
| |
if(fh == INVALID_HANDLE)
| |
{
| |
Print("Fallo al abrir archivo desencriptado, error = ", GetLastError());
| |
return;
| |
}
| |
string result = FileReadString(fh);
| |
FileClose(fh);
| |
| |
//---
| |
Print("Contenido original = ", test_content);
| |
Print("Contenido recuperado = ", result);
| |
}
| |
//+------------------------------------------------------------------+
| |
| |
//+------------------------------------------------------------------+
|