Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace TranspositionCipher
- {
- partial class Form1
- {
- /// <summary>
- /// Обязательная переменная конструктора.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Освободить все используемые ресурсы.
- /// </summary>
- /// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace TranspositionCipher
- {
- public partial class Form1 : Form
- {
- private int SHIFT; // Ширина таблицы
- public Form1()
- {
- InitializeComponent();
- SHIFT = 2;
- }
- // Проверка на правильность размера таблицы
- private void textBoxShift_Leave(object sender, EventArgs e)
- {
- bool success = Int32.TryParse(textBoxShift.Text, out SHIFT);
- if (!success || SHIFT<2)
- {
- SHIFT = 2;
- textBoxShift.Text = "2";
- }
- }
- // Событие на нажатие любой кнопки
- private void button_Click(object sender, EventArgs e)
- {
- string result = "", input = textBoxIn.Text;
- // shift - сдвиг, с которым мы ходим по строке, move - текущее положение в строке
- int shift, move = 0;
- Button B = sender as Button;
- if (B.Text == "Зашифровать")
- shift = SHIFT; // при шифровании сдиг - ширина
- else
- {
- shift = textBoxIn.Text.Length / SHIFT; // при дешифровании - высота
- if (textBoxIn.Text.Length % SHIFT != 0) // Если есть остаток - к целому числу +1
- shift++;
- }
- while (input.Length % shift != 0) // Заполдняем остаток таблицы пробелами
- input += ' ';
- // Шифрование
- for (int i = 0; i < input.Length; i++)
- {
- result += input[move]; // сохраняем символ
- move += shift; // ходим по строке
- if (move >= input.Length) // если выходим за пределы строки...
- move = (move + 1) % input.Length; // возвращаемся назад со сдвигом +1,
- // то есть, на навый ряд/столбец в таблице
- }
- result = result.TrimEnd(); // Удаление пробелов
- textBoxOut.Text = result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement