2011-11-09 22 views
0

我有下面的代码,由用户输入其散列密码,并随后将其存储在一个SQL Server数据库中ASP.NET哈希密码:使用MD5

Byte[] originalPassword; 
    Byte[] hashedPassword; 

    MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider(); 
    UTF8Encoding encoder = new UTF8Encoding(); 

    originalPassword = encoder.GetBytes(passwordBox.Text); 
    hashedPassword = md5Hasher.ComputeHash(originalPassword); 
    command.Parameters.Add(new SqlParameter("Password", hashedPassword)); 
    command.ExecuteNonQuery(); 

我的问题是,我已经在数据库中存储了一些明文密码。我究竟如何将它们修改为这种新的散列格式,因为它们显示为'0xA99ED ....'?

+0

好了,你必须使用MD5出于某种原因...你不能至少使用盐,或者甚至存储整个散列?无论如何,你似乎认为字符串和二进制文件是等价的 - 它们不是,需要编码。 – Aaronaught

+0

问题是什么?当更改为nvarchar时,您不能指望binary16列具有相同的结果。 –

+1

我认为你的问题在于这句话:“我手动输入一个字符串格式的MD5并将数据类型改回二进制(16)”。如果你输入了散列的十六进制表示,并且它通过在十六进制表示中获取每个字符的二进制值而转换为二进制值,这是正常的,结果是不一样的... –

回答

2

任何散列函数的输出都是字节的集合,而不是文本的集合。所以当你输入文本作为测试时,你可能会输入该字节数组的文本转换。只需将其转换为二进制文件(16)是不正确的,则需要进行适当的转换,这是SQL中无法做到的。这也解释了为什么更改列的数据类型也不起作用。

当散列表示为字符串时,通常通过每个字节的十六进制值或通过字符集编码器。为了它们之间进行切换,你需要找出哪一个正在使用中,无法通过SQL

2
try this out first create a Windows form with 2 buttons and 2 text boxes 
1st button label Encrypt 
2nd button label Validate 
**--- Hashing using the MD5 class ---** 

use the following code below 
/// <summary> 
/// take any string and encrypt it using MD5 then 
/// return the encrypted data 
/// </summary> 
/// <param name="data">input text you will enterd to encrypt it</param> 
/// <returns>return the encrypted text as hexadecimal string</returns> 
private string GetMD5HashData(string data) 
{ 
    //create new instance of md5 
    MD5 md5 = MD5.Create(); 

    //convert the input text to array of bytes 
    byte[] hashData = md5.ComputeHash(Encoding.Default.GetBytes(data)); 

    //create new instance of StringBuilder to save hashed data 
    StringBuilder returnValue = new StringBuilder(); 

    //loop for each byte and add it to StringBuilder 
    for (int i = 0; i < hashData.Length; i++) 
    { 
     returnValue.Append(hashData[i].ToString()); 
    } 

    // return hexadecimal string 
    return returnValue.ToString(); 

} 

/// <summary> 
/// encrypt input text using MD5 and compare it with 
/// the stored encrypted text 
/// </summary> 
/// <param name="inputData">input text you will enterd to encrypt it</param> 
/// <param name="storedHashData">the encrypted text 
///   stored on file or database ... etc</param> 
/// <returns>true or false depending on input validation</returns> 
private bool ValidateMD5HashData(string inputData, string storedHashData) 
{ 
    //hash input text and save it string variable 
    string getHashInputData = GetMD5HashData(inputData); 

    if (string.Compare(getHashInputData, storedHashData) == 0) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
1

这种方法的伟大工程的切换数据类型的代码执行转换,返回利用MD5哈希字符串LINQ。这适用于MailChimp API 3.0,而返回字节数组的前一个代码则没有。

public static string GetMd5HashData(string yourString) 
    { 
    return string.Join("", MD5.Create().ComputeHash(Encoding.ASCII.GetBytes(yourString)).Select(s => s.ToString("x2"))); 
    } 

这里找到:http://rion.io/2013/02/23/generating-an-md5-hash-from-a-string-using-linq/

0

下面是使用LINQ VB.NET的版本(对于那些谁仍在使用VB.NET):

Public Function GenerateMD5(ByVal plainText As String) As String 
     Return String.Join("", System.Security.Cryptography.MD5.Create().ComputeHash(System.Text.Encoding.ASCII.GetBytes(plainText)).Select(Function(x) x.ToString("x2"))) 
End Function