Силовая атака решетки Кардано - C#
Формулировка задачи:
Подскажите как можно подключить словарь слов dictionary.txt на полный перебор, по правилу: подставлять каждое слово по порядку в word (шифрование производится по первому вхождению каждой буквы слова в тексте), если имеется возможность вставить второе, третье и более слов после первого, вставляем. Иначе, если букв для слова не хватает пропускаем его, и выдаем все получившиеся варианты. Например, пекло+патрон; поп+патриот и т.д.
У меня шифр решетки Кардано (без поворотов!) код шифрования и дешифрования:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ConsoleApplication4
{
class Program
{
public static string word = "приветбрат!";
public static string input = "ПеРгамент клеопатры Из египта отраВлЕн Токсином, Будь остоРожен с ним, цезАрь, Такова твоя учесть!";
static void Main(string[] args)
{
Console.WriteLine("Шифруемое слово: " + word + "\n");
Console.WriteLine("Криптограмма : " + input + "\n");
input = input.Replace(' ', '_');
input = input.ToLower();
Matrix();
Console.WriteLine("\nКриптограмма: " + input);
Console.WriteLine("\nКлюч : " + Crypt(word, input));
string key = Crypt(word, input);
Console.Write("\nДешифровка : ");
DeCrypt(input, key);
Console.ReadLine();
}
public static void Matrix()
{
Console.WriteLine("Матрица:");
int b = 0;
int c = input.Length / word.Length;
if (input.Length % word.Length != 0)
{
int d = input.Length % word.Length;
c++;
int f = word.Length - d;
input = input.PadRight(input.Length + f);
}
char[,] Matrix = new char[word.Length, c];
for (int j = 0; j < c; j++)
{
for (int i = 0; i < word.Length; i++)
{
Matrix[i, j] = input[b];
Console.Write("{0} ", Matrix[i, j]);
b++;
}
Console.WriteLine();
}
}
static string Crypt(string word, string input)
{
var sb = new StringBuilder();
int i = 0, j = 0;
for (; i < word.Length; i++)
{
for (; j < input.Length; j++)
{
if (word[i] == input[j])
{
sb.Append("1");
j++;
break;
}
sb.Append("0");
}
}
for (; j < input.Length; j++)
sb.Append("0");
return sb.ToString();
}
static void DeCrypt(string input, string key)
{
key = Crypt(word, input);
for (int i = 0; i < key.Length; i++)
{
if (Convert.ToString(key[i]) == "1")
{
Console.Write(input[i]);
}
}
}
}
}Решение задачи: «Силовая атака решетки Кардано»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Linq;
namespace ConsoleApplication4
{
class Program
{
public static string word = "привет";
public static string input = "ПеРгамент И перо отраВлЕно Токсином";
public static StreamReader sr = new StreamReader(@"F:\dictionary.txt", Encoding.Default);
static void Main(string[] args)
{
Console.WriteLine("Шифруемое слово: " + word + "\n");
Console.WriteLine("Криптограмма: " + input + "\n");
input = input.Replace(' ', '_');
input = input.ToLower();
Matrix();
Console.WriteLine("\nКриптограмма: " + input);
Console.WriteLine("\nКлюч : " + Crypt(word, input));
string key = Crypt(word, input);
Console.WriteLine("\nДешифровка : ");
DeCrypt(input, key);
Console.WriteLine("\nВзламываем : ");
Analize(input, key);
Console.ReadKey();
}
public static void Matrix()
{
Console.WriteLine("Матрица:");
int b = 0;
int c = input.Length / word.Length;
if (input.Length % word.Length != 0)
{
int d = input.Length % word.Length;
c++;
int f = word.Length - d;
input = input.PadRight(input.Length + f);
}
char[,] Matrix = new char[word.Length, c];
for (int j = 0; j < c; j++)
{
for (int i = 0; i < word.Length; i++)
{
Matrix[i, j] = input[b];
Console.Write("{0} ", Matrix[i, j]);
b++;
}
Console.WriteLine();
}
}
static string Crypt(string word, string input)
{
var sb = new StringBuilder();
int i = 0, j = 0;
for (; i < word.Length; i++)
{
for (; j < input.Length; j++)
{
if (word[i] == input[j])
{
sb.Append("1");
j++;
break;
}
sb.Append("0");
}
}
for (; j < input.Length; j++)
sb.Append("0");
return sb.ToString();
}
static void DeCrypt(string input, string key)
{
key = Crypt(word, input);
for (int i = 0; i < key.Length; i++)
{
if (Convert.ToString(key[i]) == "1")
{
Console.Write(input[i]);
}
}
}
static void Analize(string input, string key)
{
string[] src = sr.ReadToEnd().Split('\n');
for (int i = 0; i < src.GetLength(0); i++)
{
key = Crypt(src[i], input);
for (int j = 0; j < key.Length; j++)
{
if (Convert.ToString(key[j]) == "1")
{
//if (input[j].Length != src[i].Length)
//{
//break;
//}
Console.Write(input[j]);
}
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}