GetHashCode of string return different values on different OS
Posted on 2011-04-30
I have a vb.net 3.5 desktop application. This uses couple of .net dll which are also written in vb.net 3.5. These dll have common funtions. Now I wanted that these dlls should not be used by other people where this desktop app is installed. So whenever I create instance of this dll, I pass a hard-coded string as "MyApp_Protected_2000". This string is passed from my win form to the constructor of the dll. The constructor of the dll uses .GetHashCode to get the hashcode of this string. I compare this hashcode return value which I have pre-generated in development environment (XP with 32-bit OS) and compare it like this:
========= Class Code ===================
If strValidate.Trim.GetHashCode <> -5999234 then
Throw new exception "Unauthorized User"
This way I ensured that noone can use this dll.
But now I find that if this app is used on Windows 7 (64-bit), the GetHashCode returns different value other than -5999234 for the same string "MyApp_Protected_2000".
a. Why is this so? And how can I resolve this issue? What can I do to ensure that the string returns same value no matter what CLR version and OS versiion?
b. Or should I find the encrypted value of "MyApp_Protected_2000" in design-time and compare with that? The only problem I see here is that the encrypted value would have some special chars and I am afraid that might break the app. Also now I have doubt that even the encryption function may return different values on 32-bit and 64-bit OS.