1

Тема: Шифр частоколу

Зашифровує текст шифром частоколу.

private string T1EncryptData(string decrData, int key)
        {
            if (key == 1) return decrData;
            var decrDataLength = decrData.Length;

            var decrVector = new int[decrDataLength];
            var delta = 1;
            var charPositionInEncryptedString = 0;
            for (var i = 0; i < decrDataLength; i++)
            {
                if (charPositionInEncryptedString > key - 2 || (charPositionInEncryptedString <= 0 && delta < 0)) delta = -delta;
                decrVector[i] = charPositionInEncryptedString;
                charPositionInEncryptedString += delta;
            }
            var res = new string[key];
            for (var i = 0; i < decrDataLength; i++)
            {
                for (var j = 0; j < key; j++)
                {
                    if (decrVector[i] == j)
                        res[j] += decrData[i];
                }
            }
            var result = string.Empty;
            for (var i = res.Length - 1; i != -1; i--)
                result += res[i];
            return result;
        }