VigenerCipher

Author: Copyright 2024, Pavel Shutovskiy
0 Views
0 Downloads
0 Favorites
VigenerCipher
ÿþ//+------------------------------------------------------------------+

//|                                                VigenerCipher.mq4 |

//|                                 Copyright 2024, Pavel Shutovskiy |

//|                 https://www.mql5.com/en/users/strannik-ps/seller |

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

#property copyright "Copyright 2024, Pavel Shutovskiy"

#property link      "https://www.mql5.com/en/users/strannik-ps/seller"

#property version   "1.00"

#property strict

#property show_inputs



input string text       = "Hello, world!"; // Messadge:

input string secretkey  = "LIME";          // Key:



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

// Function for encoding and decoding the Vigenere cipher, taking into account the case, special characters, and spaces.

string VigenereExt(string message, string key, int mode) {



   static string ExtSumbol[95] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S",

                                  "T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l",

                                  "m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4",

                                  "5","6","7","8","9","+","/","!","@","*","?","(",")",",",".","-","_","=",":",

                                  ";","%","#","[","]","{","}","|","$","<",">","&","`","~","^","\\","'","\""," "};



  int size = ArraySize(ExtSumbol);

  

// We remove only those characters from the message and the key that are not in the array ExtSymbol.

  string clean_message = "";

  string clean_key = "";

  

      for (int i = 0; i < StringLen(message); i++) {

        for (int c = 0; c < size; c++) {

          string ch = StringSubstr(message, i, 1);

          if (ExtSumbol[c] == ch) {

            clean_message += ch;

          }

        }

      }

      for (int i = 0; i < StringLen(key); i++) {

        for (int c = 0; c < size; c++) {

          string ch = StringSubstr(key, i, 1);

          if (ExtSumbol[c] == ch) {

            clean_key += ch;

          }

        }

     }



// Repeat the key until it becomes the same length as the message.

   string repeated_key = "";

   int j = 0;

      for (int i = 0; i < StringLen(clean_message); i++) {

         repeated_key += StringSubstr(clean_key, j, 1);

         j++;

      if (j == StringLen(clean_key)) {

         j = 0;

       }

   }

   

// Encode or decode the message using the array.

   string result = "";

   int index_message = -1;

   int index_key = -1;

   

      for (int i = 0; i < StringLen(clean_message); i++) {

         string ch_message = StringSubstr(clean_message, i, 1);

         string ch_key = StringSubstr(repeated_key, i, 1);

// Assign the index.

         for (int l = 0; l < size; l++) {

            if(ExtSumbol[l] == ch_message)

               index_message = l;

            if(ExtSumbol[l] == ch_key)

               index_key = l;

         }



   int index_result;

      if (mode == 0) { // Encoding

         index_result = (index_message + index_key) % size;

      } else if (mode == 1) { // Decoding

         index_result = (index_message - index_key + size) % size;

      }

      result += ExtSumbol[index_result];

   }

   return result;

}

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

//| Script program start function                                    |

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

void OnStart()

  {

// Encrypt the message "Hello, world!" with the key "LIME."

      string encrypted = VigenereExt(text, secretkey, 0);

      Print(encrypted);

      

// Decrypt the message with the key 

      string decrypted = VigenereExt(encrypted, secretkey, 1);

      Print(decrypted); // It will output "Hello, world!"

}

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

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---