Advertisement
Guest User

safenote

a guest
Apr 4th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows.Forms;
  6.  
  7. namespace Szyfrownik
  8. {
  9.     static class Program
  10.     {
  11.         /// <summary>
  12.         /// Główny punkt wejścia dla aplikacji.
  13.         /// </summary>
  14.         [STAThread]
  15.         static void Main()
  16.         {
  17.             Application.EnableVisualStyles();
  18.             Application.SetCompatibleTextRenderingDefault(false);
  19.             //Application.Run(new Form1());
  20.  
  21.             logowanie l = new logowanie();
  22.             l.ShowDialog();
  23.  
  24.             if (l.log == true)
  25.             {
  26.  
  27.                 Form1 f = new Form1();
  28.                 f.ShowDialog();
  29.             }
  30.  
  31.         }
  32.  
  33.  
  34.         static void Generuj_klucz(ref int[][] tablica)
  35.         {
  36.             int wylosowano = 0;
  37.             Random losowa = new Random();
  38.  
  39.             for (int i = 0; i < 3; i++)
  40.             {
  41.                 while (wylosowano < 116)
  42.                 {
  43.                     int Losowa = losowa.Next(100, 999);
  44.  
  45.                     if ((Array.IndexOf(tablica[i], Losowa) == -1))
  46.                     {
  47.                         tablica[i][wylosowano] = Losowa;
  48.                         wylosowano++;
  49.                     }
  50.                 }
  51.                 wylosowano = 0;
  52.             }
  53.             int u = 0;
  54.             // generowanie kluczy spec...
  55.             wylosowano = 116;
  56.  
  57.             while (wylosowano < 119)
  58.             {
  59.                 int Losowa = losowa.Next(100, 999);
  60.  
  61.                 if ((Array.IndexOf(tablica[0], Losowa) == -1) && (Array.IndexOf(tablica[1], Losowa) == -1) && (Array.IndexOf(tablica[2], Losowa) == -1))
  62.                 {
  63.                     tablica[u][116] = Losowa;
  64.                     wylosowano++;
  65.                     u++;
  66.                 }
  67.             }
  68.         }
  69.  
  70.  
  71.         public static string Szyfruj(string zdanieIN)
  72.         {          
  73.             int[][] tablica = new int[][]
  74.             {
  75.             new int [117],
  76.             new int [117],
  77.             new int [117]
  78.             };
  79.  
  80.             Generuj_klucz(ref tablica);
  81.  
  82.             char[] alfabet = ("`~1234567890-=!@#$%^&*( )_+qwertyuiopasdfghjklzxcvbnm" +
  83.                 "[]{}\\|;:'\",.<>/?QWERTYUIOPASDFGHJKLZXCVBNMąĄćĆĘꣳŃńÓ󌜯żŹź\r\n\t").ToCharArray();
  84.  
  85.             List<int> zdanie_sz = new List<int>();
  86.             List<char> zdanie = new List<char>();
  87.  
  88.             foreach (char x in zdanieIN)
  89.             {
  90.                 zdanie.Add(x);
  91.             }
  92.  
  93.             while (zdanie.Count % 3 != 0)
  94.             {
  95.                 zdanie.Add('`');              
  96.             }
  97.  
  98.             Random los = new Random();
  99.  
  100.             int Losowa=0;
  101.  
  102.             int i = 0, j=0, c=0;
  103.  
  104.             try
  105.             {              
  106.                 while (i < zdanie.Count)
  107.                 {
  108.                     Losowa = los.Next(0,3);
  109.                     zdanie_sz.Add(tablica[Losowa][116]);
  110.                     while(c<3)
  111.                     {
  112.                         if (zdanie[i] == alfabet[j])
  113.                         {
  114.                             i++;
  115.                             c++;
  116.                             zdanie_sz.Add(tablica[Losowa][j]);
  117.                             j = 0;
  118.                         }
  119.                         else j++;
  120.                     }
  121.                     c = 0;
  122.                 }
  123.             }
  124.             catch
  125.             {
  126.                 MessageBox.Show("Błąd szyfrowania");
  127.             }
  128.             // łączenie intów w string...
  129.             string separator = ",";
  130.             string separator2 = "!";
  131.             string zdanie2 = String.Join(separator, zdanie_sz);
  132.             string klucz1 = String.Join(separator, tablica[0]);
  133.             string klucz2 = String.Join(separator, tablica[1]);
  134.             string klucz3 = String.Join(separator, tablica[2]);
  135.             string joined = String.Join(separator2, klucz1, zdanie2, klucz2, klucz3);
  136.  
  137.             return joined;
  138.         }
  139.  
  140.  
  141.         public static string Deszyfruj(string joined)
  142.         {
  143.             int[][] tablica = new int[][]
  144.             {
  145.             new int [117],
  146.             new int [117],
  147.             new int [117]
  148.             };
  149.  
  150.             // do deszyfrowania, rozdzielenie na tablice...
  151.             List<string> numbers = new List<string>(joined.Split('!'));
  152.  
  153.             List<int> klucz1 = new List<int>(Array.ConvertAll(numbers[0].Split(','), int.Parse));
  154.             List<int> zdanie_sz = new List<int>(Array.ConvertAll(numbers[1].Split(','), int.Parse));
  155.             List<int> klucz2 = new List<int>(Array.ConvertAll(numbers[2].Split(','), int.Parse));
  156.             List<int> klucz3 = new List<int>(Array.ConvertAll(numbers[3].Split(','), int.Parse));
  157.  
  158.             List<char> zdanie_n = new List<char>();
  159.             tablica[0] = klucz1.ToArray();
  160.             tablica[1] = klucz2.ToArray();
  161.             tablica[2] = klucz3.ToArray();
  162.  
  163.             char[] alfabet = ("`~1234567890-=!@#$%^&*( )_+qwertyuiopasdfghjklzxcvbnm" +
  164.                "[]{}\\|;:'\",.<>/?QWERTYUIOPASDFGHJKLZXCVBNMąĄćĆĘꣳŃńÓ󌜯żŹź\r\n\t").ToCharArray();
  165.  
  166.             int i = 0, j = 0, c = 0, g = 0;
  167.             try
  168.             {
  169.                 while (i < zdanie_sz.Count)
  170.                 {
  171.                     if (zdanie_sz[i] == tablica[j][116])
  172.                     {
  173.                         i++;
  174.                         while (g < 3)
  175.                         {
  176.                             if (zdanie_sz[i] == tablica[j][c])
  177.                             {
  178.                                 i++;
  179.                                 zdanie_n.Add(alfabet[c]);
  180.                                 g++;
  181.                                 c = 0;
  182.                             }
  183.                             else
  184.                             {
  185.                                 c++;
  186.                             }
  187.                         }
  188.                         j = 0;
  189.                         g = 0;
  190.                     }
  191.                     else
  192.                     {
  193.                         j++;
  194.                     }
  195.                 }
  196.             }
  197.             catch
  198.             {
  199.                 MessageBox.Show("Błąd deszyfrowania!");
  200.             }
  201.             string zdKoniec = new string(zdanie_n.ToArray());
  202.  
  203.             return zdKoniec;
  204.         }
  205.  
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement