Загрузка данных


using System;
using System.Text;

namespace Laboratornaya_2_VerticalPermutation
{
    public static class Cipher
    {
        public static string Encrypt(string text, string key)
        {
            CheckText(text, "Введите сообщение.");
            CheckKey(key);

            int columns = key.Length;
            int rows = (text.Length + columns - 1) / columns;
            char[,] table = new char[rows, columns];
            int position = 0;

            for (int row = 0; row < rows; row++)
            {
                for (int column = 0; column < columns; column++)
                {
                    if (position < text.Length)
                    {
                        table[row, column] = text[position];
                        position++;
                    }
                }
            }

            int[] order = GetColumnOrder(key);
            StringBuilder result = new StringBuilder();

            for (int i = 0; i < order.Length; i++)
            {
                int column = order[i];

                for (int row = 0; row < rows; row++)
                {
                    int textPosition = row * columns + column;

                    if (textPosition < text.Length)
                    {
                        result.Append(table[row, column]);
                    }
                }
            }

            return result.ToString();
        }

        public static string Decrypt(string text, string key)
        {
            CheckText(text, "Введите шифрограмму.");
            CheckKey(key);

            int columns = key.Length;
            int rows = (text.Length + columns - 1) / columns;
            int lastRowLength = text.Length % columns;
            string[] columnText = new string[columns];
            int[] order = GetColumnOrder(key);
            int position = 0;

            for (int i = 0; i < order.Length; i++)
            {
                int column = order[i];
                int columnLength = rows;

                if (lastRowLength != 0 && column >= lastRowLength)
                {
                    columnLength--;
                }

                if (columnLength > 0)
                {
                    columnText[column] = text.Substring(position, columnLength);
                    position += columnLength;
                }
                else
                {
                    columnText[column] = "";
                }
            }

            StringBuilder result = new StringBuilder();

            for (int row = 0; row < rows; row++)
            {
                for (int column = 0; column < columns; column++)
                {
                    if (row < columnText[column].Length)
                    {
                        result.Append(columnText[column][row]);
                    }
                }
            }

            return result.ToString();
        }

        private static int[] GetColumnOrder(string key)
        {
            int[] order = new int[key.Length];

            for (int i = 0; i < order.Length; i++)
            {
                order[i] = i;
            }

            for (int i = 0; i < order.Length - 1; i++)
            {
                for (int j = i + 1; j < order.Length; j++)
                {
                    char first = char.ToUpperInvariant(key[order[i]]);
                    char second = char.ToUpperInvariant(key[order[j]]);

                    if (first > second)
                    {
                        int temporary = order[i];
                        order[i] = order[j];
                        order[j] = temporary;
                    }
                }
            }

            return order;
        }

        private static void CheckText(string text, string errorMessage)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                throw new ArgumentException(errorMessage);
            }
        }

        private static void CheckKey(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("Введите ключевое слово.");
            }

            for (int i = 0; i < key.Length; i++)
            {
                if (!char.IsLetter(key[i]))
                {
                    throw new ArgumentException("Ключ должен содержать только буквы без пробелов.");
                }

                char current = char.ToUpperInvariant(key[i]);

                for (int j = 0; j < i; j++)
                {
                    if (current == char.ToUpperInvariant(key[j]))
                    {
                        throw new ArgumentException("В ключе не должно быть повторяющихся букв.");
                    }
                }
            }
        }
    }
}