1

I am trying to make a dictionary from 1 digit to 3 digits long containing a,b,c,d,...7,8,9 ; aa,bb,cc,dd,...,77,88,99 & aaa,bbb,...888,999 MD5 Hashed functions and store it in the SQL. I tried using 3 for nested loops to fill my sql columns -palintext and MD5 hashes columns- I am quite new to C# and this is what i tested ;i have serious problems with this code. How should the correct form look like and how can i edit it?

I tried:

private void button1_Click(object sender, EventArgs e)
{

  var list = "abcdefghijklmnopqrstuvwxyz0123456789".ToString();

  for ( int i = 0 ; i < list.Length ; i++ )
  {

    com.Connection = con;
    com.CommandText = "insert into CryptographyTable(PlainText,MD5Hash) values(@PlainText,@MD5Hash)";
    com.Parameters.AddWithValue("@PlainText", List);
    com.Parameters.AddWithValue("@MD5Hash", md[List]);
    MessageBox.Show(com.ExecuteNonQuery().ToString());

    for (int j = 0; j < list.Length; j++)
    {

      com.Connection = con;
      com.CommandText = "insert into CryptographyTable(PlainText,MD5Hash) values(@PlainText,@MD5Hash)";
      com.Parameters.AddWithValue("@PlainText", List);
      com.Parameters.AddWithValue("@MD5Hash", md[List]);
      MessageBox.Show(com.ExecuteNonQuery().ToString());

      for (int k = 0; k < list.Length; k++)
      {
        com.Connection = con;
        com.CommandText = "insert into CryptographyTable(PlainText,MD5Hash) values(@PlainText,@MD5Hash)";
        com.Parameters.AddWithValue("@PlainText", List);
        com.Parameters.AddWithValue("@MD5Hash", md[List]);
        MessageBox.Show(com.ExecuteNonQuery().ToString());
      }
    }
  }
}

public string md(string input)
{
  MD5 md5 = MD5.Create();
  byte[] inputBytes = Encoding.UTF8.GetBytes(input);
  byte[] hash = md5.ComputeHash(inputBytes);  

  string sb;
  sb = "";

  for (int i = 0; i < hash.Length; i++)
  {
    sb  += hash[i].ToString("X2");
  }
  return sb;
}
}
}
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
Parisa
  • 29
  • 1
  • 6
  • Try simple things before trying everything at once. For example, you are not executing any command. – usr Jan 17 '14 at 21:07
  • Actually i am learning ,up to this session i have succeeded to compile every tasks assigned to me.This one is my problem ,any help would be appreciated. – Parisa Jan 17 '14 at 21:18
  • 1
    sounds like you're making some Rainbow tables – Eluvatar Jan 17 '14 at 23:42

2 Answers2

0

this code wouldn't even compile.

What is (var[]List)? Did you mean list?

This statement: var[] list = "abcdefghijklmnopqrstuvwxyz0123456789".ToString(); should be var list = "abcdefghijklmnopqrstuvwxyz0123456789".ToString();

This statement com.CommandText = "insert into CryptographyTable(PlainText,MD5Hash) has no closing double quote or semicolon.

BanksySan
  • 27,362
  • 33
  • 117
  • 216
  • yeah , i meant list. i edited these two points you mentioned.Now how do you think this can run ? Sorry for being new to C# ,but this one is essential to me. – Parisa Jan 17 '14 at 18:02
  • I think he meant `char[] list = "abcdefghijklmnopqrstuvwxyz0123456789".ToArray();`. – Steven Jan 17 '14 at 22:29
0

Building a rainbow table, are you?

You need to learn about the concept of problem decomposition. Breaking problems up into smaller, simpler pieces makes things much simpler.

First, you'll need a method to insert data into your rainbow table:

static void InsertIntoDb( string key , string value )
{
  string connectString = "your-connect-string-here" ;
  Func<string,string> qt = s => s.Replace("'" , "''") ;
  using( SqlConnection sql = new SqlConnection(connectString) )
  using ( SqlCommand    cmd = sql.CreateCommand() )
  {
    cmd.CommandText = string.Format( @"insert dbo.RainbowTable( md5 , text ) values ( '{0}' , '{1}' )" , qt(key) , qt(value) ) ;
    cmd.CommandType = CommandType.Text;
    sql.Open();
    cmd.ExecuteNonQuery() ;
    sql.Close() ;
  }
  return ;
}

Then you need a method to compute the MD5 hash for a piece of text and return a string containing its hex representation:

static string ComputeMd5Hash( string s , Encoding e )
{
  MD5 md5       = MD5.Create() ;
  byte[] octets = e.GetBytes(s) ;
  byte[] hash   = md5.ComputeHash(octets) ;
  string hex    = "0x" + hash.Aggregate( new StringBuilder("0x") , (sb,x) => sb.Append(x) ).ToString() ;
  return hex ;
}

Finally, you'll need a method to generate the strings you need (**hint: the concept of recursion might be useful here.)

static IEnumerable<string> GetStringsToBeHashed()
{
  // your string generation code here
}

Each of these pieces does just one thing and is easy to build and easy to test.

Once you've got all the pieces in place, then its easy to put it all together:

foreach ( string s in GetStringsToBeHashed() )
{
  string hash = ComputeMd5Hash(s,Encoding.UTF8) ;
  InsertIntoDb( hash, s ) ;
}
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
  • which part is creating 3 digits of letters and numbers dear mate? – Parisa Jan 18 '14 at 10:09
  • That would be the `// your string generation code here` part. You want to read up on permuting a set: how to generate **permutations* of M things taken N at a time. A trip to the CS books will help you. Or this question, regarding *combinations* (the difference is that combinations are order-insensitive, but permutation are: http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n, and this question: http://stackoverflow.com/questions/11208446/generating-permutations-of-a-set-most-efficiently. – Nicholas Carey Jan 20 '14 at 19:20