Advertisement
Guest User

safenote

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