rbulph Posted August 19, 2008 Posted August 19, 2008 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? Quote
Administrators PlausiblyDamp Posted August 19, 2008 Administrators Posted August 19, 2008 (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 August 19, 2008 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
rbulph Posted August 19, 2008 Author Posted August 19, 2008 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. Quote
Nate Bross Posted August 19, 2008 Posted August 19, 2008 (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 August 19, 2008 by Nate Bross Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.