Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Following on from here, but getting to be a different topic really, so I'll start a new thread.

 

Is there any method provided by VB for obtaining an eight byte hashcode from a string? The String's GetHashCode method seems to return a hashcode of 9, 10 or 11 bytes so is not quite what I'm after. Or should I design my own?

  • Administrators
Posted (edited)

As a console app the following should work

       static void Main(string[] args)
       {
           EncryptString("Is there any method provided by VB for obtaining an eight byte hashcode from a string? The String's GetHashCode method seems to return a hashcode of 9, 10 or 11 bytes so is not quite what I'm after. Or should I design my own?", "password");
       }

       private static void EncryptString(string text, string pass)
       {

           MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();


           DESCryptoServiceProvider des = new DESCryptoServiceProvider();
         
           byte[] hash = hashMD5.ComputeHash(Encoding.UTF8.GetBytes(pass));
           byte[] key = new byte[8];
           Array.Copy(hash, key, 8);
           des.IV = new byte[] {1,2,3,4,5,6,7,8};  
           des.Key = key;
           string res = Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(Encoding.UTF8.GetBytes(text), 0, text.Length));
           Console.WriteLine(res);
           Console.ReadLine();
       }

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
Thanks, that works fine. In converting it to VB I need to declare Key as Key(7) rather than Key(8) but presumably that's just a difference in the language.
Posted (edited)

Yes, that is because in C# you declare an array with the number of total elements.

 

This makes an array of length 8

myClass[] c = new myClass[8];

however, when you try to access the array, you do this

c[0] = First;
c[1] = Second;
...
c[7] = Last;

 

In Visual Basic, you define an array with the maximum [zero] based value; whereas in C# you define an array with the maximum [one] based value.

 

In short

 

myClass[] c = new myClass[8];
//'' is equal to
dim c as myClass() = New myClass(7)

 

Because C# starts with the length of the array, where as VB starts with the upperbounds of the array.

 

HTH

Edited by Nate Bross

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...