Advertisement
MaksNew

RailFence Algorithm

Feb 28th, 2022
1,103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using Laba1.Interfaces;
  4.  
  5. namespace Laba1.Algorithms
  6. {
  7.     public class RailFence : IAlgorithm
  8.     {
  9.         public string Decrypt(string text, string key)
  10.         {
  11.             int rkey = Convert.ToInt32(key);
  12.             char[,]  charMatrix = new char[rkey, text.Length];
  13.             bool dir_down = false;
  14.             int row = 0, col = 0;
  15.             for (int i = 0; i < text.Length; i++)
  16.             {
  17.                 if (row == 0 || row == rkey - 1)
  18.                     dir_down = !dir_down;
  19.                 charMatrix[row, col++] = '*';
  20.                 if (dir_down)
  21.                     row++;
  22.                 else
  23.                     row--;
  24.             }
  25.             int ind = 0;
  26.             for (int i = 0; i < rkey; i++)            
  27.             {
  28.                 for (int j = 0; j < text.Length; j++)
  29.                 {
  30.                     if (charMatrix[i, j] == '*')
  31.                         charMatrix[i, j] = text[ind++];
  32.                 }
  33.             }
  34.             string resultText = "";
  35.             row = 0;
  36.             col = 0;
  37.             for (int i = 0; i < text.Length; i++)    
  38.             {
  39.                 if (row == 0)
  40.                     dir_down = true;
  41.                 if (row == rkey - 1)
  42.                     dir_down = false;
  43.                 resultText += charMatrix[row, col++];
  44.                 if (dir_down)
  45.                     row++;
  46.                 else
  47.                     row--;
  48.             }
  49.             return resultText;
  50.         }
  51.  
  52.         public string Encrypt(string text, string key)
  53.         {
  54.             var rkey = Convert.ToInt32(key);
  55.             var matrix = new char[rkey, text.Length];
  56.             int k = 0;
  57.             int j = 0;
  58.             int buf = 0;
  59.             string resultString = "";
  60.             while (k < text.Length)
  61.             {
  62.                 for (int i = 0; (i < rkey) && (j < text.Length); i++)
  63.                 {
  64.                     matrix[i, buf] = text[buf];
  65.                     buf = ++j;
  66.                     k++;
  67.                 }
  68.                 for (int i = rkey - 2; (i > 0) && (j < text.Length); i--)
  69.                 {
  70.                     matrix[i, buf] = text[buf];
  71.                     buf = ++j;
  72.                     k++;
  73.                 }
  74.             }
  75.             k = 0;
  76.             while (k < text.Length)
  77.             {
  78.                 for (int i = 0; i < rkey; i++)
  79.                 {
  80.                     for (int y = 0; y < text.Length; y++)
  81.                     {
  82.                         if (matrix[i, y] != '\0')
  83.                             resultString += matrix[i, y];
  84.                         k++;
  85.                     }
  86.                 }
  87.             }
  88.             return resultString;
  89.         }
  90.     }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement