Reputation: 25
I'm trying to convert String to md5 that what i did :
public static string MD5Hash(string input)
{
StringBuilder hash = new StringBuilder();
MD5CryptoServiceProvider md5provider = new MD5CryptoServiceProvider();
byte[] bytes = md5provider.ComputeHash(new UTF8Encoding().GetBytes(input));
for (int i = 0; i < bytes.Length; i++)
{
hash.Append(bytes[i].ToString("x2"));
}
return hash.ToString();
}
The code above whene i tested in MD5Hash("MyCode") its returned : 6403df8223bf310152ad235731e79902 (32 char)
but the md5 on my database is 0x6403DF8223BF310152AD235731E79902000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 (64 char)
it's missing the "0X" in the begginig and the "zeros" in the ending
Upvotes: 1
Views: 7078
Reputation:
According to your examples, what generates the 64-Chars strings in your database is something like:
public static string MD5Hash(string input)
{
using (var md5 = new MD5CryptoServiceProvider())
return string.Concat("0x",
BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(input)))
.Replace("-", string.Empty).PadRight(62, '0'));
}
I wouldn't name the function MS5Hash
in this case and as you can see, the database strings are concatenated strings and the MD5 hash is just the middle part of each.
Upvotes: 0
Reputation: 11
try this
public static string Md5Hash(string text)
{
using (var md5 = new MD5CryptoServiceProvider())
{
using (var tds = new TripleDESCryptoServiceProvider())
{
tds.Key = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(encryptionKey));
tds.Mode = CipherMode.ECB;
tds.Padding = PaddingMode.PKCS7;
using (var transform = tds.CreateEncryptor())
{
byte[] textBytes = UTF8Encoding.UTF8.GetBytes(text);
byte[] bytes = transform.TransformFinalBlock(textBytes, 0, textBytes.Length);
return Convert.ToBase64String(bytes, 0, bytes.Length);
}
}
}
}
Upvotes: 1
Reputation: 31
This is Working for me -
public static string MD5Hash(string text)
{
MD5 md5 = new MD5CryptoServiceProvider();
//compute hash from the bytes of text
md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));
//get hash result after compute it
byte[] result = md5.Hash;
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
//change it into 2 hexadecimal digits
//for each byte
strBuilder.Append(result[i].ToString("x2"));
}
return strBuilder.ToString();
}
Upvotes: 3
Reputation: 1
The hash size for the MD5 algorithm is 128 bits.
The ComputeHash methods of the MD5 class return the hash as an array of 16 bytes. Note that some MD5 implementations produce a 32-character, hexadecimal-formatted hash.
So you have to check in the database why are you getting extra characters?
and for your information, the correct MD5 value for MyCode
will be 8e995273698f53088fe2ad4a0888d213
Code to generate MD5 below.
public static string GenerateMD5(string input)
{
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
}
Upvotes: 1