Расшифровка autokey vigenere в С#

Это мой код для расшифровки алгоритма шифрования автоключа Виженера.

string cipherText = "zicvtwqngkzeiigasxstslvvwla";
string key = "deceptive";

key = key.ToLower();
cipherText = cipherText.ToLower();

int klength = key.Length;
int kslength = (int)(cipherText.Length - key.Length);

string pl = string.Empty;
char[] newpl = new char[cipherText.Length];
int a = Convert.ToInt32('a');
for (int i = 0; i < cipherText.Length - kslength; i++)
{
    int c = Convert.ToInt32(cipherText[i]) - a;
    if (c< 0) c += 26;
    int k = Convert.ToInt32(key[i]) - a;
    if (k < 0) k += 26;
    int p = (c - k);
    p %= 26;
    if (p < 0) p += 26;
    p += a;
    char temp = Convert.ToChar(p);
    newpl[i] = temp;

}

char[] NewKey = new char[cipherText.Length];
char[] ciphertext = new char[cipherText.Length];
char[] chars = new char[cipherText.Length];
int count =0;
for (int i = 0; i < key.Length; i++)
{
    NewKey[i] = key[i];
    count++;
}
int j = 0;
for (int i = count; i < cipherText.Length; i++)
{
    NewKey[i] = newpl[j];
    j++;
}

Console.WriteLine(NewKey);

for (int i = klength; i < cipherText.Length; i++)
{
    int c = Convert.ToInt32(cipherText[i]) - a;
    int k = Convert.ToInt32(NewKey[i]) - a;
    int p = (c - k);
    p %= 26;
    if (p < 0) p += 26;
    p += a;

    char temp = Convert.ToChar(p);
    newpl[i] = temp;
}
pl = new string(newpl);
Console.WriteLine(pl);

это дает мне вывод:

Обманчивый диск

savlmleoopet

в то время как правильный вывод должен быть:

Объявления

носить

первая строка вывода относится к автоматически сгенерированному ключу

вторая строка относится к расшифрованному тексту.


person Eslam Yasser    schedule 13.03.2019    source источник


Ответы (1)


В вашем коде есть несколько ошибок:

1) Посмотрите на эту строку:

for (int i = 0; i < cipherText.Length - kslength; i++)

kslength = cipherText.Length - key.Length так что ваш код

for (int i = 0; i < key.Length; i++)

Длина ключа меньше длины текста, поэтому расшифровка заканчивается слишком рано.

2)

char temp = Convert.ToChar(p);
newpl[i] = temp;

Вы расшифровали символ, но при расшифровке автоматического ключа вы должны добавить расшифрованный символ к своему ключу.

3)

for (int i = 0; i < key.Length; i++)

Вместо этого должно быть NewKey.Length, потому что key длиннее, чем нам действительно нужно после исправления #2.

Фиксированный код:

string cipherText = "zicvtwqngkzeiigasxstslvvwla";
string key = "deceptive";

key = key.ToLower();
cipherText = cipherText.ToLower();

int klength = key.Length;

string pl = string.Empty;
char[] newpl = new char[cipherText.Length];
int a = Convert.ToInt32('a');
for (int i = 0; i < cipherText.Length; i++)
{
    int c = Convert.ToInt32(cipherText[i]) - a;
    if (c < 0) c += 26;
    int k = Convert.ToInt32(key[i]) - a;
    if (k < 0) k += 26;
    int p = (c - k);
    p %= 26;
    if (p < 0) p += 26;
    p += a;
    char temp = Convert.ToChar(p);
    key += temp;
    newpl[i] = temp;

}

char[] NewKey = new char[cipherText.Length];
int count = 0;
for (int i = 0; i < NewKey.Length; i++)
{
    NewKey[i] = key[i];
    count++;
}
int j = 0;
for (int i = count; i < cipherText.Length; i++)
{
    NewKey[i] = newpl[j];
    j++;
}

Console.WriteLine(NewKey);

for (int i = klength; i < cipherText.Length; i++)
{
    int c = Convert.ToInt32(cipherText[i]) - a;
    int k = Convert.ToInt32(NewKey[i]) - a;
    int p = (c - k);
    p %= 26;
    if (p < 0) p += 26;
    p += a;

    char temp = Convert.ToChar(p);
    newpl[i] = temp;
}
pl = new string(newpl);
Console.WriteLine(pl);

Вывод:

Объявления

носить

person ingvar    schedule 13.03.2019