Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Md5 Calculator - Md5BaseReport.cs

Md5BaseReport.cs

Caricato da: A_butta
Scarica il programma completo

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6.  
  7. namespace Md5_Calculator
  8. {
  9.     class Md5BaseReport
  10.     {
  11.         private static int blockCount = 0;
  12.  
  13.         uint WordA = 0x67452301,
  14.              WordB = 0xefcdab89,
  15.              WordC = 0x98badcfe,
  16.              WordD = 0x10325476;
  17.  
  18.         uint[] Table = new uint[64];
  19.         uint[] X = new uint[16];
  20.  
  21.         uint F(uint x, uint y, uint z) { return ((x & y) | ((~x) & z));}
  22.         uint G(uint x, uint y, uint z) { return (x & z) | ((~z) & y); }
  23.         uint H(uint x, uint y, uint z) { return x ^ y ^ z; }
  24.         uint I(uint x, uint y, uint z) { return (y ^ ((~z) | x)); }
  25.         uint RotateLeft(uint x, int s) { return (x << s) | (x >> (32 - s)); }
  26.  
  27.         System.IO.StreamWriter Writer;
  28.  
  29.  
  30.         uint Round1(uint a, uint b, uint c, uint d, byte k, byte s, byte i)
  31.         {
  32.             Writer.Write("FF: ");
  33.  
  34.             Writer.WriteLine("\ta = " + a.ToString("X") + " b = " + b.ToString("X") + " c = " + c.ToString("X") + " d = " + d.ToString("X") + " x = " + X[k].ToString("X") + " s = " + s.ToString("") + " i = " + Table[i - 1].ToString("X"));
  35.  
  36.             Writer.WriteLine("\tF (b,c,d) = " + F(b, c, d).ToString("X"));
  37.  
  38.             Writer.WriteLine("\ta + F + xk + ti = " + (F(b, c, d) + a + X[k] + Table[i - 1]).ToString("X"));
  39.  
  40.             Writer.WriteLine("\tb + (... <<< 7 ) = " + (b + RotateLeft((a + F(b, c, d) + X[k] + Table[i - 1]), s)).ToString("X") + "\n");
  41.            
  42.             return b + RotateLeft((a + F(b, c, d) + X[k] + Table[i - 1]), s);
  43.         }
  44.         uint Round2(uint a, uint b, uint c, uint d, byte k, byte s, byte i)
  45.         {
  46.  
  47.             Writer.Write("GG: ");
  48.  
  49.             Writer.WriteLine("\ta = " + a.ToString("X") + " b = " + b.ToString("X") + " c = " + c.ToString("X") + " d = " + d.ToString("X") + " x = " + X[k].ToString("X") + " s = " + s.ToString("") + " i = " + Table[i - 1].ToString("X"));
  50.  
  51.             Writer.WriteLine("\tG (b,c,d) = " + G(b, c, d).ToString("X"));
  52.  
  53.             Writer.WriteLine("\ta + G + xk + ti = " + (G(b, c, d) + a + X[k] + Table[i - 1]).ToString("X"));
  54.  
  55.             Writer.WriteLine("\tb + (... <<< 7 ) = " + (b + RotateLeft((a + G(b, c, d) + X[k] + Table[i - 1]), s)).ToString("X") + "\n");
  56.  
  57.             return b + RotateLeft((a + G(b, c, d) + X[k] + Table[i - 1]), s);
  58.         }
  59.         uint Round3(uint a, uint b, uint c, uint d, byte k, byte s, byte i)
  60.         {
  61.  
  62.             Writer.Write("HH: ");
  63.  
  64.             Writer.WriteLine("\ta = " + a.ToString("X") + " b = " + b.ToString("X") + " c = " + c.ToString("X") + " d = " + d.ToString("X") + " x = " + X[k].ToString("X") + " s = " + s.ToString("") + " i = " + Table[i - 1].ToString("X"));
  65.  
  66.             Writer.WriteLine("\tH (b,c,d) = " + H(b, c, d).ToString("X"));
  67.  
  68.             Writer.WriteLine("\ta + H + xk + ti = " + (H(b, c, d) + a + X[k] + Table[i - 1]).ToString("X"));
  69.  
  70.             Writer.WriteLine("\tb + (... <<< 7 ) = " + (b + RotateLeft((a + H(b, c, d) + X[k] + Table[i - 1]), s)).ToString("X") + "\n");
  71.  
  72.  
  73.             return b + RotateLeft((a + H(b, c, d) + X[k] + Table[i - 1]), s);
  74.         }
  75.         uint Round4(uint a, uint b, uint c, uint d, byte k, byte s, byte i)
  76.         {
  77.  
  78.             Writer.Write("II: ");
  79.  
  80.             Writer.WriteLine("\ta = " + a.ToString("X") + " b = " + b.ToString("X") + " c = " + c.ToString("X") + " d = " + d.ToString("X") + " x = " + X[k].ToString("X") + " s = " + s.ToString("") + " i = " + Table[i - 1].ToString("X"));
  81.  
  82.             Writer.WriteLine("\tI (b,c,d) = " + I(b, c, d).ToString("X"));
  83.  
  84.             Writer.WriteLine("\ta + I + xk + ti = " + (I(b, c, d) + a + X[k] + Table[i - 1]).ToString("X"));
  85.  
  86.             Writer.WriteLine("\tb + (... <<< 7 ) = " + (b + RotateLeft((a + I(b, c, d) + X[k] + Table[i - 1]), s)).ToString("X") + "\n");
  87.  
  88.  
  89.             return b + RotateLeft((a + I(b, c, d) + X[k] + Table[i - 1]), s);
  90.         }
  91.  
  92.         public void ProcessBlock(uint[] block)
  93.         {
  94.             Array.Copy(block, X, (long) 16);
  95.             Writer  = new System.IO.StreamWriter("Report File.txt", true);
  96.             Writer.WriteLine("--- Blocco " + blockCount.ToString());
  97.  
  98.             // Report del blocck in processo;
  99.             int k = 0;
  100.             foreach (uint val in X)
  101.             {
  102.                 Writer.WriteLine("X[" + k.ToString() + "] = " + X[k].ToString("X8"));
  103.                 k++;
  104.             }
  105.  
  106.             uint A = WordA;
  107.             uint B = WordB;
  108.             uint C = WordC;
  109.             uint D = WordD;
  110.  
  111.             #region Round1
  112.             A = Round1(A, B, C, D, 0, 7, 1);
  113.             D = Round1(D, A, B, C, 1, 12, 2);
  114.             C = Round1(C, D, A, B, 2, 17, 3);
  115.             B = Round1(B, C, D, A, 3, 22, 4);
  116.  
  117.  
  118.             A = Round1(A, B, C, D, 4, 7, 5);
  119.             D = Round1(D, A, B, C, 5, 12, 6);
  120.             C = Round1(C, D, A, B, 6, 17, 7);
  121.             B = Round1(B, C, D, A, 7, 22, 8);
  122.  
  123.             A = Round1(A, B, C, D, 8, 7, 9);
  124.             D = Round1(D, A, B, C, 9, 12, 10);
  125.             C = Round1(C, D, A, B, 10, 17, 11);
  126.             B = Round1(B, C, D, A, 11, 22, 12);
  127.  
  128.             A = Round1(A, B, C, D, 12, 7, 13);
  129.             D = Round1(D, A, B, C, 13, 12, 14);
  130.             C = Round1(C, D, A, B, 14, 17, 15);
  131.             B = Round1(B, C, D, A, 15, 22, 16);
  132.             #endregion
  133.  
  134.             #region Round2
  135.             A = Round2(A, B, C, D, 1, 5, 17);
  136.             D = Round2(D, A, B, C, 6, 9, 18);
  137.             C = Round2(C, D, A, B, 11, 14, 19);
  138.             B = Round2(B, C, D, A, 0, 20, 20);
  139.  
  140.             A = Round2(A, B, C, D, 5, 5, 21);
  141.             D = Round2(D, A, B, C, 10, 9, 22);
  142.             C = Round2(C, D, A, B, 15, 14, 23);
  143.             B = Round2(B, C, D, A, 4, 20, 24);
  144.  
  145.             A = Round2(A, B, C, D, 9, 5, 25);
  146.             D = Round2(D, A, B, C, 14, 9, 26);
  147.             C = Round2(C, D, A, B, 3, 14, 27);
  148.             B = Round2(B, C, D, A, 8, 20, 28);
  149.  
  150.             A = Round2(A, B, C, D, 13, 5, 29);
  151.             D = Round2(D, A, B, C, 2, 9, 30);
  152.             C = Round2(C, D, A, B, 7, 14, 31);
  153.             B = Round2(B, C, D, A, 12, 20, 32);
  154. #endregion
  155.  
  156.             #region Round3
  157.             A = Round3(A, B, C, D, 5, 4, 33);
  158.             D = Round3(D, A, B, C, 8, 11, 34);
  159.             C = Round3(C, D, A, B, 11, 16, 35);
  160.             B = Round3(B, C, D, A, 14, 23, 36);
  161.  
  162.             A = Round3(A, B, C, D, 1, 4, 37);
  163.             D = Round3(D, A, B, C, 4, 11, 38);
  164.             C = Round3(C, D, A, B, 7, 16, 39);
  165.             B = Round3(B, C, D, A, 10, 23, 40);
  166.  
  167.             A = Round3(A, B, C, D, 13, 4, 41);
  168.             D = Round3(D, A, B, C, 0, 11, 42);
  169.             C = Round3(C, D, A, B, 3, 16, 43);
  170.             B = Round3(B, C, D, A, 6, 23, 44);
  171.  
  172.             A = Round3(A, B, C, D, 9, 4, 45);
  173.             D = Round3(D, A, B, C, 12, 11, 46);
  174.             C = Round3(C, D, A, B, 15, 16, 47);
  175.             B = Round3(B, C, D, A, 2, 23, 48);
  176.             #endregion
  177.  
  178.             #region Round4
  179.             A = Round4(A, B, C, D, 0, 6, 49);
  180.             D = Round4(D, A, B, C, 7, 10, 50);
  181.             C = Round4(C, D, A, B, 14, 15, 51);
  182.             B = Round4(B, C, D, A, 5, 21, 52);
  183.  
  184.             A = Round4(A, B, C, D, 12, 6, 53);
  185.             D = Round4(D, A, B, C, 3, 10, 54);
  186.             C = Round4(C, D, A, B, 10, 15, 55);
  187.             B = Round4(B, C, D, A, 1, 21, 56);
  188.  
  189.             A = Round4(A, B, C, D, 8, 6, 57);
  190.             D = Round4(D, A, B, C, 15, 10, 58);
  191.             C = Round4(C, D, A, B, 6, 15, 59);
  192.             B = Round4(B, C, D, A, 13, 21, 60);
  193.  
  194.             A = Round4(A, B, C, D, 4, 6, 61);
  195.             D = Round4(D, A, B, C, 11, 10, 62);
  196.             C = Round4(C, D, A, B, 2, 15, 63);
  197.             B = Round4(B, C, D, A, 9, 21, 64);
  198.             #endregion
  199.             WordA += A;
  200.             WordB += B;
  201.             WordC += C;
  202.             WordD += D;
  203.  
  204.             blockCount++;
  205.  
  206.  
  207.             Writer.Close();
  208.         }
  209.  
  210.         public Md5BaseReport()
  211.         {
  212.             // Costruzione Tabella
  213.             for (int i = 1; i <= 64; i++)
  214.                 Table[i - 1] = (uint)(0x100000000 * (double)Math.Abs(Math.Sin((double)i)));
  215.  
  216.         }
  217.  
  218.         public String returnDigest()
  219.         {
  220.             //Process String Rappresentation
  221.             string wA = null, wB = null, wC = null, wD = null;
  222.             for (int j = 0; j < 32; j += 8)
  223.             {
  224.                 wA += ((byte)(WordA >> j)).ToString("X2");
  225.                 wB += ((byte)(WordB >> j)).ToString("X2");
  226.                 wC += ((byte)(WordC >> j)).ToString("X2");
  227.                 wD += ((byte)(WordD >> j)).ToString("X2");
  228.             }
  229.  
  230.             return wA + wB + wC + wD;
  231.         }
  232.     }
  233. }