Взлом решетки Кардано с помощью силовой атаки (полным перебором) - C#
Формулировка задачи:
Подскажите пожалуйста как лучше реализовать силовую атаку решетки Кардано (полный перебор ключей) или может другой вариант взлома. Есть слово: "привет!", есть криптограмма: "ПеРгамент клеопатры Из египта отраВлЕн Токсином!"
И ключ криптограммы(решетка) вот такой:101000000000000000001000000000000010100100000001
Код шифрования/дешифрования
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);
input = input.Replace(' ', '_');
input = input.ToLower();
Console.WriteLine("\n" + "Текст: " + input);
Console.WriteLine("Ключ : " + Crypt(word, input));
string key = Crypt(word, input);
Console.Write("Дешифровка : ");
DeCrypt(input, key);
Console.ReadLine();
}
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
Листинг программы
string INPUT = "Привет мир рад встрече";
char[] гластные = new char[ ] { 'у', 'е', 'а', 'о', 'э', 'я', 'и', 'ю', 'ь', 'ъ', 'й' };
string[] Существ = System.IO.File.ReadAllLines( "c:\\Существ.txt" ).Where( word => word.Length > 5 ).ToArray();
List<string> НовыйСловарь = new List<string>();
Существ.ToList().Where( word => word.Length > 5 ).ToList().ForEach( word =>
{
string newWord = word.TrimEnd( гластные );
char lastchar = newWord[ newWord.Length - 1 ];
newWord += ( lastchar == 'н' ? "ный" : "енный" );
НовыйСловарь.Add( newWord );
} );
string[] Прилаг = НовыйСловарь.ToArray();
НовыйСловарь = new List<string>();
Существ.ToList().Where( word => word.Length > 5 ).ToList().ForEach( word =>
{
string newWord = word.TrimEnd( гластные );
newWord += "ит";
НовыйСловарь.Add( newWord );
} );
string[] Глаголы = НовыйСловарь.ToArray();
List<string[]> dicList = new List<string[ ]>() { Прилаг, Существ, Глаголы };
int _dicChanger = 0;
bool _ducUp = true;
int _i_word = 0;
int leng = Существ.Length;
List<char> patternList = INPUT.ToLower().ToCharArray().ToList().Where( a => a != ' ' ).ToList(); //ОБРАЗЕЦ
int _i_pat = 0;
string result_S = "";
List<bool> reslust_B = new List<bool>();
while ( true ) {
int MAX = 5; //максимальное колво букв в слове
System.Random rand = new System.Random();
/*MAX = rand.Next( 1, System.Math.Min( 5, patternList.Count - _i_pat ) );*/
int count = System.Math.Min( MAX, patternList.Count - _i_pat );
string word = null;
List<bool> code = new List<bool>();
while ( word == null ) {
int[] index = new int[ count ];
for ( ; _i_word < leng && word == null; _i_word += rand.Next(1,40)) {
for ( int i = 0; i < index.Length; i++ ) {
int offset = i == 0 ? 0 : index[ i - 1 ] + 1;
index[ i ] =
dicList[ _dicChanger ][ _i_word ]
.ToLower()
.Substring( offset )
.IndexOf( patternList[ _i_pat + i ] );
if ( index[ i ] == -1 ) break;
index[ i ] += offset;
}
if ( index.All( a => a != -1 ) ) {
word = dicList[ _dicChanger ][ _i_word ].ToLower() + " ";
for ( int n = 0; n < dicList[ _dicChanger ][ _i_word ].Length; n++ )
code.Add( index.Any( a => a == n ) );
code.Add( false );
_i_pat += index.Length;
} //FIND
} //findChar
if ( _i_word >= leng ) _i_word = 0;
count--;
} //findWord
if ( _ducUp ) {
_dicChanger++;
if ( _dicChanger == dicList.Count - 1 ) _ducUp = false;
}
else {
_dicChanger--;
if ( _dicChanger == -1 ) {
_ducUp = true;
word += "! ";
code.Add( false );
code.Add( false );
_dicChanger = 0;
}
}
// print(_dicChanger);
result_S += word;
reslust_B.AddRange( code );
if ( patternList.Count == _i_pat ) {
if ( !result_S.EndsWith( "! " ) ) {
result_S += '!';
reslust_B.Add( false );
}
break;
}
} //findfine
int ind = 0;
print( string.Concat( result_S.ToCharArray().ToList().ConvertAll( a => reslust_B[ ind++ ] ? a.ToString().ToUpper() : a.ToString().ToLower() ).ToArray() ) );
print( reslust_B.Aggregate( "", ( current, boolcar ) => current + ( boolcar ? 1 : 0 ) ) ); //печать