Search code examples
c#algorithmencryptionvigenereautokey

autokey vigenere decryption in c#


This is my code to decrypt autokey vigenere cipher algorithm

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);

it gives me output:

deceptivewearedisc

wearediscoveredsavlmleoopet

while the right output should be:

deceptivewearediscoveredsav

wearediscoveredsaveyourself

first line of the output refers to the auto generated key

second line refers to the decrypted text.


Solution

  • There are few errors in your code:

    1) Look at this line:

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

    kslength = cipherText.Length - key.Length so your code is

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

    Key length is < than text length so you finish decryption too early.

    2)

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

    You decrypted symbol but with autokey decryption you should add decrypted symbol to your key.

    3)

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

    Should be NewKey.Length instead, because key is longer that we really need after fixing #2.

    Fixed code:

    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);
    

    Output:

    deceptivewearediscoveredsav

    wearediscoveredsaveyourself