Hello, there is a problem with the Vigenere cipher implementation. It is not possible to access the elements of the table and write 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; } } } } 
  • It is not completely clear what exactly is failing. Do you want to insert a part of a line into another line by some rule? - Nicolas Chabanovsky

2 answers 2

Training, 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'; } 
  • Thanks for the help, everything worked out. - rfl36

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

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

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

In your Schifrovanie() function it is written (not to the end, I would call it a sketch) only the creation of the Viginer table (I think it is called that), and the encryption itself is generally omitted. The part in the comment //tmp = alf.Substring(txbx\_openText.Text.Substring(j, 1)% txbx_key.Text.Length); obviously out of place.

Apparently you need to write res += x; in the inner loop res += x; Before the inner loop res = ""; And after the internal loop tab[k] = res . How to make tab [] the size of the key.Length elements in C # I don’t know.

  • In this line // tmp = alf.Substring (txbx_openText.Text.Substring (j, 1)% txbx_key.Text.Length); I tried to replace plaintext with encrypted, but an error occurred. The variable p contains the characters of the table. I can't do the plaintext encryption myself, i.e. replace the characters of open (openText) text with a key (key variable) with ciphertext. - rfl36
  • Well, I wrote. In the part (2 nested loops) that you have written, only the encryption table is built. Encryption itself must be written in a loop after these 2 loops. By the way, any characters of the input text (and key) must somehow be displayed on alf. You have listed only the main Russian. And what to do with others (numbers, Latin ...)? - avp
  • Here, another algorithm came to mind right now. The key (K length) sets offsets for the characters of the text to be encrypted (in arithmetic for one byte). Simply add the corresponding key symbol (modulo) to each incoming symbol. When the key ends we return to its beginning. And when decrypting we subtract. I do not know, of course, from the point of view of cryptanalysis, is this the same as the Vigineer's cipher? - avp