The Vigener cipher

Hello, there is a problem with the implementation of the Vigener cipher. It is not possible to access the table elements and write them to the string literal.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btn_shifr_Click(object sender, EventArgs e)
    {
        Schifrovanie();
    }

    private void Schifrovanie()
    {
        string key = txbx_key.ToString();
        string x = "";
        string alf = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
        string tmp;
        string rez = "";
        int p = 0;
        for(int k = 0; k < key.Length; k++)
        {
            p = alf.IndexOf(key.Substring(k, 1));
            for (int j = 0; j < alf.Length; j++)
            {
             //Составление таблицы Виженера
                x = alf.Substring((j + p) % alf.Length, 1);
            //шифрование открытого текста
                //tmp = alf.Substring(txbx_openText.Text.Substring(j, 1)% txbx_key.Text.Length);
                rez += tmp;
            }
        }                      
    }
}
Author: rfl36, 2011-08-21

2 answers

Educational, but quite a working example.

/* Кодирование с помощью таблиц Вижинера,
получает символ таблицы на пересечении элементов */
unsigned char GetChar( int yp, int xp )
{
   long  nRes = (((yp - xp) < 0) ? ((yp - xp) + 256) : (yp - xp));
   return (unsigned char)nRes;
}

/* кодирует строку:
out_str - массив выходной строки,
in_str - массив входной строки,
password - массив символов с паролем */
void CodeStr( char *out_str, char *in_str, char *password )
{
   unsigned char pass_index, pass_len, str_len, x;
   pass_len = strlen( password );
   str_len = strlen( in_str );
   for( pass_index=x=0; x<str_len; x++ )
   {
      out_str[x] = GetChar( in_str[x], password[ pass_index ] );
      pass_index++;
      if ( pass_index >= pass_len ) pass_index = 0;
   }
   out_str[x] = '\0';
}

/* декодирует строку */
void DecodeStr( char *out_str, char *in_str, int str_len, char *password )
{
   unsigned char pass_index, pass_len;
   int k, x;
   pass_len = strlen( password );

   for( pass_index=k=0; k<str_len; k++ )
   {
      for( x=0; x<=255; x++ )
         if ( GetChar( x,password[ pass_index ] ) == in_str[k] ) break;

      out_str[k] = GetChar( x,0 );
      pass_index++;
      if ( pass_index>=pass_len ) pass_index=0;
   }
   out_str[k] = '\0';
}
 1
Author: novex, 2011-08-22 11:10:05

I do not know C#, but in general the algorithm is clear.

In fact, the result of nested loops is an array of k cyclically rearranged alf strings. (A vector of pointers to strings? In C, I would call it that.)

Next, you look through the encrypted text character by character and make a replacement. change the i-th character (similar to how p was calculated) to a character from the string, with the index i%k (say, tab[i%k]), where k is the length of the key.

You have in the function Schifrovanie() written (not completely, I would call it only the creation of the Vizhiner table (I think it's called that), and the encryption itself is omitted altogether. The part in the comment //tmp = alf.Substring(txbx\_openText.Text.Substring(j, 1)% txbx_key.Text.Length); is obviously out of place.

Apparently you have to write {[4] in the inner loop]} Before the internal res cycle = ""; And after the inner loop tab[k] = res. I don't know how to make tab[] the size of key.Length elements in C#.

 0
Author: avp, 2013-04-30 15:43:48