public string Crypt(string text, string key, string abc, bool encrypt) { Checker.KeyNull(key); Checker.KeyContain(key, abc); Checker.TextNull(text); Checker.TextContain(text, abc); var encAbc = string.Join("", key.Union(abc)); var result = ""; foreach (var ch in text) { for (var j = 0; j < abc.Length; j++) { if (encrypt) { if (ch == abc[j]) { result += encAbc[j]; break; } } else { if (ch == encAbc[j]) { result += abc[j]; break; } } } } return(result); }
public string Crypt(string text, string key, string abc, bool encrypt) { Checker.KeyNull(key); Checker.TextNull(text); var indexes = GetIndexes(key); var result = ""; if (encrypt) { var encAbc = GetEncryptedAlphabet(text, key, abc); for (var i = 0; i < key.Length; i++) { var index = Array.IndexOf(indexes, i); for (var j = 0; j < encAbc.GetLength(0); j++) { result += encAbc[j, index]; } } } else { var cols = key.Length; var textLen = text.Length; if (cols > textLen) { throw new CipherException("Длина ключа больше чем шифрованный текст."); } var rows = textLen / cols; if (textLen % cols > 0) { rows += 1; } var textArr = new string[rows, cols]; var count = 0; for (var i = 0; i < key.Length; i++) { var index = Array.IndexOf(indexes, i); for (var j = 0; j < textArr.GetLength(0); j++) { if (count == text.Length) { textArr[j, index] = "•"; } else { textArr[j, index] = text[count++].ToString(); } } } result = string.Join("", textArr.Cast <string>()); } return(result); }
public string[,] GetEncryptedAlphabet(string text, string key, string abc) { Checker.KeyNull(key); Checker.TextNull(text); var textLen = text.Length; var cols = key.Length; var rows = textLen / cols; if (textLen % cols > 0) { rows += 1; } var textArr = new string[rows, cols]; var count = 0; for (var i = 0; i < textArr.GetLength(0); i++) { for (var j = 0; j < textArr.GetLength(1); j++) { if (count == text.Length) { textArr[i, j] = "•"; } else { textArr[i, j] = text[count++].ToString(); } } } return(textArr); }
public string Crypt(string text, string key, string abc, bool encrypt) { Checker.KeyNull(key); Checker.KeyContain(key, abc); Checker.TextNull(text); Checker.TextContain(text, abc); var len = abc.Length; var result = ""; // index of the key symbol var count = 0; foreach (var t in text) { var index = abc.IndexOf(t); var keyIndex = abc.IndexOf(key[count]); if (encrypt) { result += $"{abc[(index + keyIndex) % len]}"; } else { result += $"{abc[(index + len - keyIndex) % len]}"; } count = (count + 1) % key.Length; } return(result); }
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]}"; }
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]; }
public string Crypt(string text, string key, string abc, bool encrypt) { Checker.KeyNull(key); var intKey = Checker.GetKeyInt(key); Checker.TextNull(text); Checker.TextContain(text, abc); // to avoid overflows intKey %= abc.Length; if (!encrypt) { intKey *= -1; } // otherwise invalid index if (intKey < 0) { intKey += abc.Length; } var result = ""; foreach (var ch in text) { for (var j = 0; j < abc.Length; j++) { if (ch == abc[j]) { var temp = (j + intKey) % abc.Length; result += abc[temp]; break; } } } return(result); }
public string Crypt(string text, string key, string abc, bool encrypt) { Checker.KeyNull(key); Checker.KeyContain(key, abc); Checker.TextNull(text); Checker.TextContain(text, abc); // удвоенные буквы for (var i = 0; i < text.Length - 1; i += 2) { if (text[i] == text[i + 1]) { throw new CipherException( $"Необходимо поставить символ между \"{text[i]}{text[i + 1]}\"."); } } if (text.Length % 2 != 0) { throw new CipherException("Текст содержит нечетное количество символов.\n" + "Добавьте или удалите один символ."); } var encAbc = GetEncryptedAlphabet(null, key, abc); var result = ""; // rows == cols var len = encAbc.GetLength(0); for (var i = 0; i < text.Length - 1; i += 2) { var first = text[i].ToString(); var second = text[i + 1].ToString(); if (!ArrayOperations.ContainsIn(first, encAbc, out var x1, out var y1) || !ArrayOperations.ContainsIn(second, encAbc, out var x2, out var y2)) { continue; } // если в одной строке if (x1 == x2) { if (encrypt) { result += $"{encAbc[x1, (y1 + 1) % len]}" + $"{encAbc[x2, (y2 + 1) % len]}"; } else { result += $"{encAbc[x1, (y1 - 1 + len) % len]}" + $"{encAbc[x2, (y2 - 1 + len) % len]}"; } } // если в одном столбце else if (y1 == y2) { if (encrypt) { result += $"{encAbc[(x1 + 1) % len, y1]}" + $"{encAbc[(x2 + 1) % len, y2]}"; } else { result += $"{encAbc[(x1 - 1 + len) % len, y1]}" + $"{encAbc[(x2 - 1 + len) % len, y2]}"; } } // иначе else { result += $"{encAbc[x1, y2]}" + $"{encAbc[x2, y1]}"; } } return(result); }