In permutation ciphers, the order of the characters changes. In a simple permutation cipher, a permutation of numbers from 0 to n is selected as the key. For example, for n = 7, you can use a permutation of 3, 2, 5, 7, 4, 6, 1. Next, the text is written into n columns, which are then rearranged in accordance with the order specified by the permutation. Below is an example:
ΠΊΠ°ΠΊ Π²ΠΈΠ΄
Π½ΠΎ, ΡΠΎΠ²
Result:
Π΄Π°ΠΊΠ²ΠΊΠΈ
Π²ΠΎΠ½Ρ,ΠΎ
That is, in the key 3, 2, 5, 7, 4, 6, 1 it is indicated in which place the letter will stand. The first letter "k", it corresponds to the number 3, etc.
Help, please implement.
public string Encrypt(string msg, char[] key) { string result = string.Empty; string[] msgInArray = new string[(msg.Length / key.Length) + 1]; for (int i = 0; i < (msg.Length / key.Length) + 1; i++) { if (msg.Length <= key.Length) { msgInArray[i] = msg; break; } else { msgInArray[i] = msg.Substring(i * key.Length, key.Length); } } int[] a = new int[key.Length]; for (int i = 0; i < a.Length; i++) { a[i] = key[i]-48; } List<char> msgl = new List<char>(); for (int i = 0; i < msgInArray.Length; i++) { if (msgInArray[i] != null) { msgl = msgInArray[i].ToList<char>(); for (int j = 0; j < key.Length; j++) { result += msgl[a[j]]; } msgl.Clear(); } else { break; } } return result; } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(Encrypt(textBox1.Text, textBox2.Text.ToCharArray())); }
Gives an error message
ΠΠ½Π΄Π΅ΠΊΡ ΠΈ Π΄Π»ΠΈΠ½Π° Π΄ΠΎΠ»ΠΆΠ½Ρ ΡΠΊΠ°Π·ΡΠ²Π°ΡΡ Π½Π° ΠΏΠΎΠ·ΠΈΡΠΈΡ Π² ΡΡΡΠΎΠΊΠ΅. ΠΠΌΡ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°: length
in line
msgInArray[i] = msg.Substring(i * key.Length, key.Length);
key.Length
- Specter