Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace rail_fence
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.WriteLine("Wpisz tekst do zaszyfrowania");
  14.             Console.WriteLine();
  15.  
  16.             string clearText = Console.ReadLine();
  17.            
  18.             Console.WriteLine("Podaj wartość klucza");
  19.             Console.WriteLine();
  20.  
  21.             int key = int.Parse(Console.ReadLine());
  22.  
  23.             string cipherText = Cipher(clearText, key);
  24.             Console.WriteLine("Zaszyfrowany tekst: {0}", cipherText);
  25.  
  26.             Console.WriteLine();
  27.  
  28.             string decipherText = Decipher(cipherText, key);
  29.             Console.WriteLine("Rozszyfrowany tekst: {0}", decipherText);
  30.  
  31.             Console.ReadKey();
  32.         }
  33.  
  34.         private static char[][] BuildCleanMatrix(int rows, int cols)
  35.         {
  36.             char[][] result = new char[rows][];
  37.  
  38.             for (int row = 0; row < result.Length; row++)
  39.             {
  40.                 result[row] = new char[cols];
  41.             }
  42.  
  43.             return result;
  44.         }
  45.  
  46.         private static string BuildStringFromMatrix(char[][] matrix)
  47.         {
  48.             string result = string.Empty;
  49.  
  50.             for (int row = 0; row < matrix.Length; row++)
  51.             {
  52.                 for (int col = 0; col < matrix[row].Length; col++)
  53.                 {
  54.                     if (matrix[row][col] != '\0')
  55.                     {
  56.                         result += matrix[row][col];
  57.                     }
  58.                 }
  59.             }
  60.  
  61.             return result;
  62.         }
  63.  
  64.         private static char[][] Transpose(char[][] matrix)
  65.         {
  66.             char[][] result =
  67.                 BuildCleanMatrix(matrix[0].Length, matrix.Length);
  68.  
  69.             for (int row = 0; row < matrix.Length; row++)
  70.             {
  71.                 for (int col = 0; col < matrix[row].Length; col++)
  72.                 {
  73.                     result[col][row] = matrix[row][col];
  74.                 }
  75.             }
  76.  
  77.             return result;
  78.         }
  79.  
  80.         private static string Cipher(string clearText, int key)
  81.         {
  82.             string result = string.Empty;
  83.  
  84.             char[][] matrix = BuildCleanMatrix(key, clearText.Length);
  85.  
  86.             int rowIncrement = 1;
  87.             for (int row = 0, col = 0; col < matrix[row].Length; col++)
  88.             {
  89.                 if (
  90.                     row + rowIncrement == matrix.Length ||
  91.                     row + rowIncrement == -1
  92.                     )
  93.                 {
  94.                     rowIncrement *= -1;
  95.                 }
  96.  
  97.                 matrix[row][col] = clearText[col];
  98.  
  99.                 row += rowIncrement;
  100.             }
  101.  
  102.             result = BuildStringFromMatrix(matrix);
  103.  
  104.             return result;
  105.         }
  106.  
  107.         private static string Decipher(string cipherText, int key)
  108.         {
  109.             string result = string.Empty;
  110.  
  111.             char[][] matrix = BuildCleanMatrix(key, cipherText.Length);
  112.  
  113.             int rowIncrement = 1;
  114.             int textIdx = 0;
  115.  
  116.             for (
  117.                 int selectedRow = 0;
  118.                 selectedRow < matrix.Length;
  119.                 selectedRow++
  120.                 )
  121.             {
  122.                 for (
  123.                     int row = 0, col = 0;
  124.                     col < matrix[row].Length;
  125.                     col++
  126.                     )
  127.                 {
  128.                     if (
  129.                         row + rowIncrement == matrix.Length ||
  130.                         row + rowIncrement == -1
  131.                         )
  132.                     {
  133.                         rowIncrement *= -1;
  134.                     }
  135.  
  136.                     if (row == selectedRow)
  137.                     {
  138.                         matrix[row][col] = cipherText[textIdx++];
  139.                     }
  140.  
  141.                     row += rowIncrement;
  142.                 }
  143.             }
  144.  
  145.             matrix = Transpose(matrix);
  146.             result = BuildStringFromMatrix(matrix);
  147.  
  148.             return result;
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement