Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. namespace TranspositionCipher
  2. {
  3. partial class Form1
  4. {
  5. /// <summary>
  6. /// Обязательная переменная конструктора.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// Освободить все используемые ресурсы.
  12. /// </summary>
  13. /// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22.  
  23.  
  24.  
  25.  
  26. using System;
  27. using System.Collections.Generic;
  28. using System.ComponentModel;
  29. using System.Data;
  30. using System.Drawing;
  31. using System.Text;
  32. using System.Windows.Forms;
  33.  
  34. namespace TranspositionCipher
  35. {
  36. public partial class Form1 : Form
  37. {
  38. private int SHIFT; // Ширина таблицы
  39.  
  40. public Form1()
  41. {
  42. InitializeComponent();
  43. SHIFT = 2;
  44. }
  45.  
  46. // Проверка на правильность размера таблицы
  47. private void textBoxShift_Leave(object sender, EventArgs e)
  48. {
  49. bool success = Int32.TryParse(textBoxShift.Text, out SHIFT);
  50. if (!success || SHIFT<2)
  51. {
  52. SHIFT = 2;
  53. textBoxShift.Text = "2";
  54. }
  55. }
  56.  
  57. // Событие на нажатие любой кнопки
  58. private void button_Click(object sender, EventArgs e)
  59. {
  60. string result = "", input = textBoxIn.Text;
  61. // shift - сдвиг, с которым мы ходим по строке, move - текущее положение в строке
  62. int shift, move = 0;
  63.  
  64. Button B = sender as Button;
  65. if (B.Text == "Зашифровать")
  66. shift = SHIFT; // при шифровании сдиг - ширина
  67. else
  68. {
  69. shift = textBoxIn.Text.Length / SHIFT; // при дешифровании - высота
  70. if (textBoxIn.Text.Length % SHIFT != 0) // Если есть остаток - к целому числу +1
  71. shift++;
  72. }
  73.  
  74. while (input.Length % shift != 0) // Заполдняем остаток таблицы пробелами
  75. input += ' ';
  76.  
  77. // Шифрование
  78. for (int i = 0; i < input.Length; i++)
  79. {
  80. result += input[move]; // сохраняем символ
  81. move += shift; // ходим по строке
  82. if (move >= input.Length) // если выходим за пределы строки...
  83. move = (move + 1) % input.Length; // возвращаемся назад со сдвигом +1,
  84. // то есть, на навый ряд/столбец в таблице
  85. }
  86.  
  87. result = result.TrimEnd(); // Удаление пробелов
  88. textBoxOut.Text = result;
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement