Advertisement
VMike

Untitled

May 28th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace XOR
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         string text, gamma;
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.         public const string alphabet = "абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ 0123456789.,-:;!?=+-";
  21.         class Encryption
  22.         {
  23.             public int[] TpluseG;
  24.             string C, G1;
  25.             public Encryption(string _T, string _G)
  26.             {
  27.                 int[] T, G, modN;
  28.                 int count = 0, k = 0;
  29.                 T = new int [_T.Length];
  30.                 G = new int[_T.Length];
  31.                 TpluseG = new int[_T.Length];
  32.                 modN = new int[_T.Length];
  33.                 for (int i = 0; i < _T.Length; i++)
  34.                 {
  35.                     for (int j = 0; j < alphabet.Length; j++)
  36.                     {
  37.                         if (_T[i]==alphabet[j])
  38.                         {
  39.                             T[i] = j + 1;
  40.                         }
  41.                     }
  42.                 }
  43.                 while (count != _T.Length)
  44.                 {
  45.                     G1 += _G[k];
  46.                     k++;
  47.                     count++;
  48.                     if (k == _G.Length)
  49.                     {
  50.                         k = 0;
  51.                     }
  52.                 }
  53.                 for (int i = 0; i < _T.Length; i++)
  54.                 {
  55.                     for (int j = 0; j < alphabet.Length; j++)
  56.                     {
  57.                         if (G1[i] == alphabet[j])
  58.                         {
  59.                             G[i] = j + 1;
  60.                         }
  61.                     }
  62.                 }
  63.                 for (int i = 0; i < _T.Length; i++)
  64.                 {
  65.                     TpluseG[i] = T[i] + G[i];
  66.                 }
  67.                 for (int i = 0; i < _T.Length; i++)
  68.                 {
  69.                     modN[i] = TpluseG[i] % alphabet.Length;
  70.                 }
  71.                 for (int i = 0; i < _T.Length; i++)
  72.                 {
  73.                     if (modN[i] == 0)
  74.                     {
  75.                         modN[i] = alphabet.Length;
  76.                     }
  77.                 }
  78.                 for (int i = 0; i < _T.Length; i++)
  79.                 {
  80.                     modN[i] -= 1;
  81.                 }
  82.                 for (int i = 0; i < _T.Length; i++)
  83.                 {
  84.                     C += alphabet[modN[i]];
  85.                 }
  86.             }
  87.             public string output()
  88.             {
  89.                 return C;
  90.             }
  91.         }
  92.         class Decryption
  93.         {
  94.             int[] CminusG, CminusGpluseN, C, G, modN;
  95.             string T, G1;
  96.             public Decryption(string _C, string _G)
  97.             {
  98.                 int count = 0, k = 0;
  99.                 C = new int[_C.Length];
  100.                 G = new int[_C.Length];
  101.                 CminusG = new int[_C.Length];
  102.                 modN = new int[_C.Length];
  103.                 CminusGpluseN = new int[_C.Length];
  104.                 for (int i = 0; i < _C.Length; i++)
  105.                 {
  106.                     for (int j = 0; j < alphabet.Length; j++)
  107.                     {
  108.                         if (_C[i] == alphabet[j])
  109.                         {
  110.                             C[i] = j + 1;
  111.                         }
  112.                     }
  113.                 }
  114.                 while (count != _C.Length)
  115.                 {
  116.                     G1 += _G[k];
  117.                     k++;
  118.                     count++;
  119.                     if (k == _G.Length)
  120.                     {
  121.                         k = 0;
  122.                     }
  123.                 }
  124.                 for (int i = 0; i < _C.Length; i++)
  125.                 {
  126.                     for (int j = 0; j < alphabet.Length; j++)
  127.                     {
  128.                         if (G1[i] == alphabet[j])
  129.                         {
  130.                             G[i] = j + 1;
  131.                         }
  132.                     }
  133.                 }
  134.                 for (int i = 0; i < _C.Length; i++)
  135.                 {
  136.                     CminusG[i] = C[i] - G[i];
  137.                 }
  138.                 for (int i = 0; i < _C.Length; i++)
  139.                 {
  140.                     CminusGpluseN[i] = CminusG[i] + alphabet.Length;
  141.                 }
  142.                 for (int i = 0; i < _C.Length; i++)
  143.                 {
  144.                     modN[i] = CminusGpluseN[i] % alphabet.Length;
  145.                 }
  146.                 for (int i = 0; i < _C.Length; i++)
  147.                 {
  148.                     if (modN[i] == 0)
  149.                     {
  150.                         modN[i] = alphabet.Length;
  151.                     }
  152.                 }
  153.                 for (int i = 0; i < _C.Length; i++)
  154.                 {
  155.                     modN[i] -= 1;
  156.                 }
  157.                 for (int i = 0; i < _C.Length; i++)
  158.                 {
  159.                     T += alphabet[modN[i]];
  160.                 }
  161.             }
  162.             public string output()
  163.             {
  164.                 return T;
  165.             }
  166.         }
  167.  
  168.         private void button1_Click(object sender, EventArgs e)
  169.         {
  170.             richTextBox2.Text = "";
  171.             text = richTextBox1.Text;
  172.             gamma = textBox1.Text;
  173.             Encryption line = new Encryption(text, gamma);
  174.             richTextBox2.Text += "" + line.output();
  175.         }
  176.  
  177.         private void button2_Click(object sender, EventArgs e)
  178.         {
  179.             richTextBox2.Text = "";
  180.             text = richTextBox1.Text;
  181.             gamma = textBox1.Text;
  182.             Decryption line = new Decryption(text, gamma);
  183.             richTextBox2.Text += "" + line.output();
  184.         }
  185.  
  186.         private void button3_Click(object sender, EventArgs e)
  187.         {
  188.             richTextBox1.Text = "";
  189.         }
  190.  
  191.         private void button4_Click(object sender, EventArgs e)
  192.         {
  193.             richTextBox2.Text = "";
  194.         }
  195.  
  196.         private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  197.         {
  198.             char ch=e.KeyChar;
  199.             if (ch >= 'а' && ch <= 'я' || Char.IsControl(e.KeyChar))
  200.             {
  201.                 return;
  202.             }
  203.             else
  204.             {
  205.                 e.Handled = true;
  206.             }
  207.         }
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement