Beispiel #1
0
        public string Crypt(string text, string abc, bool encrypt)
        {
            Checker.TextNull(text);

            var len = abc.Length;
            // the number of significant digits
            var count = (int)Math.Floor(Math.Log10(len * len) + 1);

            if (encrypt)
            {
                Checker.TextContain(text, abc);

                if (text.Length % 2 != 0)
                {
                    throw new CipherException("Длина текста для шифрования не кратна 2.\n" +
                                              "Добавьте или удалите один символ.");
                }
            }
            else
            {
                Checker.TextEncDigit(text);

                var num = text.Length % count;
                if (num != 0)
                {
                    throw new CipherException($"Длина шифрованного текста не кратна {count}.\n" +
                                              $"Введено {num} из {count} чисел.");
                }
            }

            var encAbc = GetEncryptedAlphabet(null, null, abc);
            var result = "";

            if (encrypt)
            {
                for (var i = 0; i < text.Length - 1; i += 2)
                {
                    var row = abc.IndexOf(text[i]);
                    var col = abc.IndexOf(text[i + 1]);
                    result += $"{encAbc[row, col]}";
                }
            }
            else
            {
                for (var i = 0; i < text.Length - count + 1; i += count)
                {
                    var temp = text.Substring(i, count);
                    if (ArrayOperations.ContainsIn(temp, encAbc, out var x, out var y))
                    {
                        result += $"{abc[x]}{abc[y]}";
                    }
Beispiel #2
0
        public string Crypt(string text, string abc, bool encrypt)
        {
            var encAbc = GetEncryptedAlphabet(null, null, abc);

            Checker.TextNull(text);

            if (encrypt)
            {
                Checker.TextContain(text, abc);
            }
            else
            {
                Checker.TextEncDigit(text);

                if (text.Length % 2 != 0)
                {
                    throw new CipherException("Длина шифрованного текста не кратна 2.\n" +
                                              "Добавьте иди удалите одну цифру.");
                }
            }

            var result = "";

            if (encrypt)
            {
                foreach (var ch in text)
                {
                    if (ArrayOperations.ContainsIn(ch.ToString(), encAbc, out var x, out var y))
                    {
                        result += $"{x + 1}{y + 1}";
                    }
                }
            }
            else
            {
                for (var i = 0; i < text.Length - 1; i += 2)
                {
                    var temp = text.Substring(i, 2);
                    if (ContainsOut(temp, encAbc, out var x, out var y))
                    {
                        result += encAbc[x, y];
                    }