Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace XOR
- {
- public partial class Form1 : Form
- {
- string text, gamma;
- public Form1()
- {
- InitializeComponent();
- }
- public const string alphabet = "абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ 0123456789.,-:;!?=+-";
- class Encryption
- {
- public int[] TpluseG;
- string C, G1;
- public Encryption(string _T, string _G)
- {
- int[] T, G, modN;
- int count = 0, k = 0;
- T = new int [_T.Length];
- G = new int[_T.Length];
- TpluseG = new int[_T.Length];
- modN = new int[_T.Length];
- for (int i = 0; i < _T.Length; i++)
- {
- for (int j = 0; j < alphabet.Length; j++)
- {
- if (_T[i]==alphabet[j])
- {
- T[i] = j + 1;
- }
- }
- }
- while (count != _T.Length)
- {
- G1 += _G[k];
- k++;
- count++;
- if (k == _G.Length)
- {
- k = 0;
- }
- }
- for (int i = 0; i < _T.Length; i++)
- {
- for (int j = 0; j < alphabet.Length; j++)
- {
- if (G1[i] == alphabet[j])
- {
- G[i] = j + 1;
- }
- }
- }
- for (int i = 0; i < _T.Length; i++)
- {
- TpluseG[i] = T[i] + G[i];
- }
- for (int i = 0; i < _T.Length; i++)
- {
- modN[i] = TpluseG[i] % alphabet.Length;
- }
- for (int i = 0; i < _T.Length; i++)
- {
- if (modN[i] == 0)
- {
- modN[i] = alphabet.Length;
- }
- }
- for (int i = 0; i < _T.Length; i++)
- {
- modN[i] -= 1;
- }
- for (int i = 0; i < _T.Length; i++)
- {
- C += alphabet[modN[i]];
- }
- }
- public string output()
- {
- return C;
- }
- }
- class Decryption
- {
- int[] CminusG, CminusGpluseN, C, G, modN;
- string T, G1;
- public Decryption(string _C, string _G)
- {
- int count = 0, k = 0;
- C = new int[_C.Length];
- G = new int[_C.Length];
- CminusG = new int[_C.Length];
- modN = new int[_C.Length];
- CminusGpluseN = new int[_C.Length];
- for (int i = 0; i < _C.Length; i++)
- {
- for (int j = 0; j < alphabet.Length; j++)
- {
- if (_C[i] == alphabet[j])
- {
- C[i] = j + 1;
- }
- }
- }
- while (count != _C.Length)
- {
- G1 += _G[k];
- k++;
- count++;
- if (k == _G.Length)
- {
- k = 0;
- }
- }
- for (int i = 0; i < _C.Length; i++)
- {
- for (int j = 0; j < alphabet.Length; j++)
- {
- if (G1[i] == alphabet[j])
- {
- G[i] = j + 1;
- }
- }
- }
- for (int i = 0; i < _C.Length; i++)
- {
- CminusG[i] = C[i] - G[i];
- }
- for (int i = 0; i < _C.Length; i++)
- {
- CminusGpluseN[i] = CminusG[i] + alphabet.Length;
- }
- for (int i = 0; i < _C.Length; i++)
- {
- modN[i] = CminusGpluseN[i] % alphabet.Length;
- }
- for (int i = 0; i < _C.Length; i++)
- {
- if (modN[i] == 0)
- {
- modN[i] = alphabet.Length;
- }
- }
- for (int i = 0; i < _C.Length; i++)
- {
- modN[i] -= 1;
- }
- for (int i = 0; i < _C.Length; i++)
- {
- T += alphabet[modN[i]];
- }
- }
- public string output()
- {
- return T;
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- richTextBox2.Text = "";
- text = richTextBox1.Text;
- gamma = textBox1.Text;
- Encryption line = new Encryption(text, gamma);
- richTextBox2.Text += "" + line.output();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- richTextBox2.Text = "";
- text = richTextBox1.Text;
- gamma = textBox1.Text;
- Decryption line = new Decryption(text, gamma);
- richTextBox2.Text += "" + line.output();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- richTextBox1.Text = "";
- }
- private void button4_Click(object sender, EventArgs e)
- {
- richTextBox2.Text = "";
- }
- private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
- {
- char ch=e.KeyChar;
- if (ch >= 'а' && ch <= 'я' || Char.IsControl(e.KeyChar))
- {
- return;
- }
- else
- {
- e.Handled = true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement