Advertisement
Guest User

SAFEnote

a guest
Apr 4th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.45 KB | None | 0 0
  1.  
  2.         public static string Szyfruj(string zdanieIN)
  3.         {          
  4.             int[][] tablica = new int[][]
  5.             {
  6.             new int [117],
  7.             new int [117],
  8.             new int [117]
  9.             };
  10.             Generuj_klucz(ref tablica);
  11.             char[] alfabet = ("`~1234567890-=!@#$%^&*( )_+qwertyuiopasdfghjklzxcvbnm" +
  12.                 "[]{}\\|;:'\",.<>/?QWERTYUIOPASDFGHJKLZXCVBNMąĄćĆĘꣳŃńÓ󌜯żŹź\r\n\t").ToCharArray();
  13.             List<int> zdanie_sz = new List<int>();
  14.             List<char> zdanie = new List<char>();
  15.             foreach (char x in zdanieIN)
  16.             {
  17.                 zdanie.Add(x);
  18.             }
  19.             while (zdanie.Count % 3 != 0)
  20.             {
  21.                 zdanie.Add('`');              
  22.             }
  23.             Random los = new Random();
  24.             int Losowa=0;
  25.             int i = 0, j=0, c=0;
  26.             try
  27.             {              
  28.                 while (i < zdanie.Count)
  29.                 {
  30.                     Losowa = los.Next(0,3);
  31.                     zdanie_sz.Add(tablica[Losowa][116]);
  32.                     while(c<3)
  33.                     {
  34.                         if (zdanie[i] == alfabet[j])
  35.                         {
  36.                             i++;
  37.                             c++;
  38.                             zdanie_sz.Add(tablica[Losowa][j]);
  39.                             j = 0;
  40.                         }
  41.                         else j++;
  42.                     }
  43.                     c = 0;
  44.                 }
  45.             }
  46.             catch
  47.             {
  48.                 MessageBox.Show("Błąd szyfrowania");
  49.             }
  50.             // łączenie intów w string...
  51.             string separator = ",";
  52.             string separator2 = "!";
  53.             string zdanie2 = String.Join(separator, zdanie_sz);
  54.             string klucz1 = String.Join(separator, tablica[0]);
  55.             string klucz2 = String.Join(separator, tablica[1]);
  56.             string klucz3 = String.Join(separator, tablica[2]);
  57.             string joined = String.Join(separator2, klucz1, zdanie2, klucz2, klucz3);
  58.             return joined;
  59.         }
  60.  
  61.         public static string Deszyfruj(string joined)
  62.         {
  63.             int[][] tablica = new int[][]
  64.             {
  65.             new int [117],
  66.             new int [117],
  67.             new int [117]
  68.             };
  69.             // do deszyfrowania, rozdzielenie na tablice...
  70.             List<string> numbers = new List<string>(joined.Split('!'));
  71.             List<int> klucz1 = new List<int>(Array.ConvertAll(numbers[0].Split(','), int.Parse));
  72.             List<int> zdanie_sz = new List<int>(Array.ConvertAll(numbers[1].Split(','), int.Parse));
  73.             List<int> klucz2 = new List<int>(Array.ConvertAll(numbers[2].Split(','), int.Parse));
  74.             List<int> klucz3 = new List<int>(Array.ConvertAll(numbers[3].Split(','), int.Parse));
  75.             List<char> zdanie_n = new List<char>();
  76.             tablica[0] = klucz1.ToArray();
  77.             tablica[1] = klucz2.ToArray();
  78.             tablica[2] = klucz3.ToArray();
  79.             char[] alfabet = ("`~1234567890-=!@#$%^&*( )_+qwertyuiopasdfghjklzxcvbnm" +
  80.                "[]{}\\|;:'\",.<>/?QWERTYUIOPASDFGHJKLZXCVBNMąĄćĆĘꣳŃńÓ󌜯żŹź\r\n\t").ToCharArray();
  81.             int i = 0, j = 0, c = 0, g = 0;
  82.             try
  83.             {
  84.                 while (i < zdanie_sz.Count)
  85.                 {
  86.                     if (zdanie_sz[i] == tablica[j][116])
  87.                     {
  88.                         i++;
  89.                         while (g < 3)
  90.                         {
  91.                             if (zdanie_sz[i] == tablica[j][c])
  92.                             {
  93.                                 i++;
  94.                                 zdanie_n.Add(alfabet[c]);
  95.                                 g++;
  96.                                 c = 0;
  97.                             }
  98.                             else
  99.                             {
  100.                                 c++;
  101.                             }
  102.                         }
  103.                         j = 0;
  104.                         g = 0;
  105.                     }
  106.                     else
  107.                     {
  108.                         j++;
  109.                     }
  110.                 }
  111.             }
  112.             catch
  113.             {
  114.                 MessageBox.Show("Błąd deszyfrowania!");
  115.             }
  116.             string zdKoniec = new string(zdanie_n.ToArray());
  117.             return zdKoniec;
  118.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement