RS A Library for asymmetric encryption in MQ L5
Info
The RS A Library for asymmetric encryption in MQ L5 is a Library for MetaTrader 5 that example usages raw rsa implementation:this simple example demonstrates how the rsa encryption could be implemented. First, create an instance of the RSA class then prepare your message (the data that you want to encrypt) as uchar[], where each element is one byte.
Usage
This tool is typically used for enhancing chart analysis and decision making.
Platform
This Library works exclusively on MetaTrader 5 (both build 600+ and newer versions).
Setup
Place the downloaded file in MQL5/Libraries folder via File ? Open Data Folder in MetaTrader 5.
How to Install and Use RS A Library for asymmetric encryption in MQ L5
1. Storage: Place library files in the MQL/Libraries directory to ensure they are accessible to your projects.
2. Implementation: Include the library in your code using the #import directive, ensuring you match the exact function names and parameters.
3. Compilation: Ensure the library is present in the directory before you compile your main EA or script, as the compiler links them during this phase.
4. Management: Keep libraries organized in sub-folders if you manage many custom functions to maintain a clean project structure.
Frequently Asked Questions
Q: What is a library file used for? A: Libraries store reusable code modules, allowing you to centralize common logic used by multiple EAs or indicators.
Q: Is a library executable? A: No, libraries are non-executable files containing functions; they must be imported into an EA, indicator, or script to function.
Q: Can I update a library while the platform is running? A: You should compile your EA or script after updating a library to ensure the latest code changes are integrated.
What this tool does
Example Usages Raw RSA Implementation:This simple example demonstrates how the RSA encryption could be implemented.
Typical Use Case
This Library excels in automated trading and technical analysis on MetaTrader 5.
Compatible Platform & Setup
This Library works on MetaTrader 5. Place the file in the MQL5/Libraries folder and restart the terminal.
Description & Settings
Related: Session Time Filter Library - another powerful library for MetaTrader 5 traders.
Example Usages
Raw RSA Implementation:
This simple example demonstrates how the RSA encryption could be implemented. First, create an instance of the RSA class then prepare your message (the data that you want to encrypt) as uchar[], where each element is one byte. Finally, call EncryptPKCS1v15 (plain, cipher) to do the encryption. The result is as a char array and stored in cipherData[]:
Also recommended: AS Q Neural Net Pure MQ L5 Neural Network Library - similar library with strong performance on MetaTrader 5.
The result of encryption is printed into the console. This result will change every time due to padding behaviorUsing RSA + AES in a real project: In real-world cryptographic systems—including secure trading platforms, web servers, and distributed APIs—RSA is rarely used to encrypt large data directly. RSA encryption is computationally intensive and limited by key size, making it unsuitable for large payloads. To achieve both high-performance and strong security, RSA is typically combined with AES in a hybrid encryption model that leverages the strengths of both algorithms.
The hybrid encryption workflow generally follows these steps:
Generate a random AES session key (for example, 128 or 256 bits).
Encrypt this AES key using the RSA public key. Only the owner of the corresponding private key can later decrypt it.
Encrypt the actual data payload using AES — a fast, symmetric cipher ideal for large messages or files.
Transmit both components together:
The AES-encrypted data (fast and compact).
The RSA-encrypted AES key (small but secure). This approach combines the advantages of both algorithms. RSA enables secure key exchange between two parties without a pre-shared secret, while AES provides high-speed encryption for the actual data. In this model, RSA functions as the key protector, and AES functions as the data protector. Together, they form the foundation of modern secure communication protocols, such as SSL/TLS, HTTPS, and VPN tunnels.
Step 1—Server generates RSA public key:
Use Python, Java, OpenSSL, or any backend environment to generate modulus (hex string), exponent (normally 65537), and private key (kept on your server side).
The EA will only use the public values. Do not expose the private key to the client side.
Step 2— EA creates an RSA instance and loads the key:
Step 3 — EA prepares a request message:
Example payload can be a login request containing:EA ID, account number, and timestamp. Make a proper JSON string for your request. You can use any third-party library to make the JSON string. The snippet below demonstrates what the expected JSON string should look like:
Step 4 — EA encrypts the AES session key with RSA:
The AES key used for the rest of the session:
Step 5 — EA encrypts the actual data with AES:
Step 6 —EA sends everything to the server:
Step 7 — Server-side decryption (conceptual):
Step 8 — Client side decrypts server response:
By following these steps, real-world cryptography can be implemented directly within the MetaTrader 5 environment, resulting in a reliable and fully portable solution for end users. You can read to get more information about RSA encryption system.
The RSA library
Here is the RSA class source code. You can also use this library for big Integer arithmetic.
You may also like: Asynchronous Institutional Trade Engine (Library) - excellent alternative for library users on MetaTrader 5.
⚠ Limitations & Risk Warning
- This tool is provided for educational and testing purposes only.
- Past performance does not guarantee future results.
- Trading involves substantial risk of loss. Use on a demo account first.
- Results may vary depending on market conditions, broker, and settings.
- We recommend thorough backtesting and forward testing before using with real funds.