воскресенье, 31 октября 2010 г.
воскресенье, 10 октября 2010 г.
Урок №6 Шифр Плейфера
Шифруем текст:
public static string encrypt(string text, string key)
{
text = prepare(text);
int length = text.Length;
char a, b;
int a_ind, b_ind, a_row, b_row, a_col, b_col;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i += 2)
{
a = text[i];
b = text[i + 1];
a_ind = key.IndexOf(a);
b_ind = key.IndexOf(b);
a_row = a_ind / 5;
b_row = b_ind / 5;
a_col = a_ind % 5;
b_col = b_ind % 5;
if (a_row == b_row)
{
if (a_col == 4)
{
sb.Append(key[a_ind - 4]);
sb.Append(key[b_ind + 1]);
}
else if (b_col == 4)
{
sb.Append(key[a_ind + 1]);
sb.Append(key[b_ind - 4]);
}
else
{
sb.Append(key[a_ind + 1]);
sb.Append(key[b_ind + 1]);
}
}
else if (a_col == b_col)
{
if (a_row == 4)
{
sb.Append(key[a_ind - 20]);
sb.Append(key[b_ind + 5]);
}
else if (b_row == 4)
{
sb.Append(key[a_ind + 5]);
sb.Append(key[b_ind - 20]);
}
else
{
sb.Append(key[a_ind + 5]);
sb.Append(key[b_ind + 5]);
}
}
else
{
sb.Append(key[5 * a_row + b_col]);
sb.Append(key[5 * b_row + a_col]);
}
}
return sb.ToString();
}
Составление таблицы:
private static string prepare(string text)
{
int length = text.Length;
text = text.ToLower();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++)
{
char c = text[i];
if (c >= 97 && c <= 122)
{
if (sb.Length % 2 == 1 && sb[sb.Length - 1] == c)
{
sb.Append('x');
}
sb.Append(c);
}
}
if (sb.Length % 2 == 1)
{
sb.Append('x');
}
return sb.ToString();
}
Дешифровка:
public static string decrypt(string text, string key)
{
text = prepare(text);
int length = text.Length;
char a, b;
int a_ind, b_ind, a_row, b_row, a_col, b_col;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i += 2)
{
a = text[i];
b = text[i + 1];
a_ind = key.IndexOf(a);
b_ind = key.IndexOf(b);
a_row = a_ind / 5;
b_row = b_ind / 5;
a_col = a_ind % 5;
b_col = b_ind % 5;
if (a_row == b_row)
{
if (a_col == 0)
{
sb.Append(key[a_ind + 4]);
sb.Append(key[b_ind - 1]);
}
else if (b_col == 0)
{
sb.Append(key[a_ind - 1]);
sb.Append(key[b_ind + 4]);
}
else
{
sb.Append(key[a_ind - 1]);
sb.Append(key[b_ind - 1]);
}
}
else if (a_col == b_col)
{
if (a_row == 0)
{
sb.Append(key[a_ind + 20]);
sb.Append(key[b_ind - 5]);
}
else if (b_row == 0)
{
sb.Append(key[a_ind - 5]);
sb.Append(key[b_ind + 20]);
}
else
{
sb.Append(key[a_ind - 5]);
sb.Append(key[b_ind - 5]);
}
}
else
{
sb.Append(key[5 * a_row + b_col]);
sb.Append(key[5 * b_row + a_col]);
}
}
return sb.ToString();
}
public static string encrypt(string text, string key)
{
text = prepare(text);
int length = text.Length;
char a, b;
int a_ind, b_ind, a_row, b_row, a_col, b_col;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i += 2)
{
a = text[i];
b = text[i + 1];
a_ind = key.IndexOf(a);
b_ind = key.IndexOf(b);
a_row = a_ind / 5;
b_row = b_ind / 5;
a_col = a_ind % 5;
b_col = b_ind % 5;
if (a_row == b_row)
{
if (a_col == 4)
{
sb.Append(key[a_ind - 4]);
sb.Append(key[b_ind + 1]);
}
else if (b_col == 4)
{
sb.Append(key[a_ind + 1]);
sb.Append(key[b_ind - 4]);
}
else
{
sb.Append(key[a_ind + 1]);
sb.Append(key[b_ind + 1]);
}
}
else if (a_col == b_col)
{
if (a_row == 4)
{
sb.Append(key[a_ind - 20]);
sb.Append(key[b_ind + 5]);
}
else if (b_row == 4)
{
sb.Append(key[a_ind + 5]);
sb.Append(key[b_ind - 20]);
}
else
{
sb.Append(key[a_ind + 5]);
sb.Append(key[b_ind + 5]);
}
}
else
{
sb.Append(key[5 * a_row + b_col]);
sb.Append(key[5 * b_row + a_col]);
}
}
return sb.ToString();
}
Составление таблицы:
private static string prepare(string text)
{
int length = text.Length;
text = text.ToLower();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++)
{
char c = text[i];
if (c >= 97 && c <= 122)
{
if (sb.Length % 2 == 1 && sb[sb.Length - 1] == c)
{
sb.Append('x');
}
sb.Append(c);
}
}
if (sb.Length % 2 == 1)
{
sb.Append('x');
}
return sb.ToString();
}
Дешифровка:
public static string decrypt(string text, string key)
{
text = prepare(text);
int length = text.Length;
char a, b;
int a_ind, b_ind, a_row, b_row, a_col, b_col;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i += 2)
{
a = text[i];
b = text[i + 1];
a_ind = key.IndexOf(a);
b_ind = key.IndexOf(b);
a_row = a_ind / 5;
b_row = b_ind / 5;
a_col = a_ind % 5;
b_col = b_ind % 5;
if (a_row == b_row)
{
if (a_col == 0)
{
sb.Append(key[a_ind + 4]);
sb.Append(key[b_ind - 1]);
}
else if (b_col == 0)
{
sb.Append(key[a_ind - 1]);
sb.Append(key[b_ind + 4]);
}
else
{
sb.Append(key[a_ind - 1]);
sb.Append(key[b_ind - 1]);
}
}
else if (a_col == b_col)
{
if (a_row == 0)
{
sb.Append(key[a_ind + 20]);
sb.Append(key[b_ind - 5]);
}
else if (b_row == 0)
{
sb.Append(key[a_ind - 5]);
sb.Append(key[b_ind + 20]);
}
else
{
sb.Append(key[a_ind - 5]);
sb.Append(key[b_ind - 5]);
}
}
else
{
sb.Append(key[5 * a_row + b_col]);
sb.Append(key[5 * b_row + a_col]);
}
}
return sb.ToString();
}
Урок №5 Алгоритм Виженера
Генерируем таблицу символов:
private static char[,] genTable()
{
char[,] table = new char[26,26];
for (int i = 0; i < 26; i++)
{
int off = i;
for (int j = 0; j < 26; j++)
{
if (off == 26)
{
off = 0;
}
table[i, j] = (char)(97 + off);
off++;
}
}
return table;
}
Шифратор:
public static string encrypt(string text, string key)
{
string ntext =null;
int k;
int t;
char[,] table = genTable();
for (int i = 0,j = 0; i < text.Length; i++,j++)
{
if (j == key.Length) { j = 0; }
k = (int)key[j] - 97;
t = (int)text[i] - 97;
ntext += Convert.ToString(table[t,k]);
}
return ntext;
}
Дешифратор
public static string decrypt(string text,string key) {
string ntext=null;
int k;
int t;
char[,] table = genTable();
for (int i = 0,j = 0; i < text.Length; i++,j++)
{
if (j == key.Length) { j = 0; }
k = (int)key[j] - 97;
t = (int)text[i] - 97;
for (int y = 0; y < 26; y++)
{
if (table[y, k] == text[i]) { ntext += table[y,0]; break; }
}
}
return ntext;
}
Подписаться на:
Комментарии (Atom)