MQLArticles/Utils/Encrypt/TestFile.mq5

73 lines
2.5 KiB
MQL5
Raw Permalink Normal View History

2026-03-26 18:15:47 -05:00
//+------------------------------------------------------------------+
//| 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";
2026-03-27 09:40:19 -05:00
if(!encryptor.DecryptFile(enc_file, true, CRYPT_AES256, false, "-")) // Quiatrmos el .enc (sobreescribimos)
2026-03-26 18:15:47 -05:00
{
Print("Fallo al desencriptar");
return;
}
Print("Archivo desencriptado: ", test_path + ".enc.dec");
//--- Leemos el resultado y verificamos
2026-03-27 09:40:19 -05:00
fh = FileOpen(test_path, FILE_READ | FILE_TXT | FILE_COMMON);
2026-03-26 18:15:47 -05:00
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);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+