Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace rail_fence
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Wpisz tekst do zaszyfrowania");
- Console.WriteLine();
- string clearText = Console.ReadLine();
- Console.WriteLine("Podaj wartość klucza");
- Console.WriteLine();
- int key = int.Parse(Console.ReadLine());
- string cipherText = Cipher(clearText, key);
- Console.WriteLine("Zaszyfrowany tekst: {0}", cipherText);
- Console.WriteLine();
- string decipherText = Decipher(cipherText, key);
- Console.WriteLine("Rozszyfrowany tekst: {0}", decipherText);
- Console.ReadKey();
- }
- private static char[][] BuildCleanMatrix(int rows, int cols)
- {
- char[][] result = new char[rows][];
- for (int row = 0; row < result.Length; row++)
- {
- result[row] = new char[cols];
- }
- return result;
- }
- private static string BuildStringFromMatrix(char[][] matrix)
- {
- string result = string.Empty;
- for (int row = 0; row < matrix.Length; row++)
- {
- for (int col = 0; col < matrix[row].Length; col++)
- {
- if (matrix[row][col] != '\0')
- {
- result += matrix[row][col];
- }
- }
- }
- return result;
- }
- private static char[][] Transpose(char[][] matrix)
- {
- char[][] result =
- BuildCleanMatrix(matrix[0].Length, matrix.Length);
- for (int row = 0; row < matrix.Length; row++)
- {
- for (int col = 0; col < matrix[row].Length; col++)
- {
- result[col][row] = matrix[row][col];
- }
- }
- return result;
- }
- private static string Cipher(string clearText, int key)
- {
- string result = string.Empty;
- char[][] matrix = BuildCleanMatrix(key, clearText.Length);
- int rowIncrement = 1;
- for (int row = 0, col = 0; col < matrix[row].Length; col++)
- {
- if (
- row + rowIncrement == matrix.Length ||
- row + rowIncrement == -1
- )
- {
- rowIncrement *= -1;
- }
- matrix[row][col] = clearText[col];
- row += rowIncrement;
- }
- result = BuildStringFromMatrix(matrix);
- return result;
- }
- private static string Decipher(string cipherText, int key)
- {
- string result = string.Empty;
- char[][] matrix = BuildCleanMatrix(key, cipherText.Length);
- int rowIncrement = 1;
- int textIdx = 0;
- for (
- int selectedRow = 0;
- selectedRow < matrix.Length;
- selectedRow++
- )
- {
- for (
- int row = 0, col = 0;
- col < matrix[row].Length;
- col++
- )
- {
- if (
- row + rowIncrement == matrix.Length ||
- row + rowIncrement == -1
- )
- {
- rowIncrement *= -1;
- }
- if (row == selectedRow)
- {
- matrix[row][col] = cipherText[textIdx++];
- }
- row += rowIncrement;
- }
- }
- matrix = Transpose(matrix);
- result = BuildStringFromMatrix(matrix);
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement