如果你有查閱MSDN文件 MD5CryptoServiceProvider 類別的用法,
就有完整範例了,範例如下:
// Hash an input string and return the md5 hash as a 32 character hexadecimal
// string.
public string GetMd5Hash( string pInput )
{
// Create a new instance of the MD5CryptoServiceProvider object.
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes( pInput ));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for( int i = 0; i < data.Length; i++ )
{
sBuilder.Append( data[ i ].ToString( "x2" ) );
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
// Verify a md5 hash against a string.
public bool VerifyMd5Hash( string pInput, string pHash )
{
// Hash the input.
string hashOfInput = GetMd5Hash( pInput );
// Create a StringComparer an compare the hashes.
StringComparer comparer = StringComparer.OrdinalIgnoreCase;
if( 0 == comparer.Compare( hashOfInput, pHash ) )
{
return true;
}
else
{
return false;
}
}
※ 引述《jimpop (popmij)》之銘言:
: 最近在網路上找到一個MD5編碼的函數
: 內容如下:
: public string UserMd5(string str)//MD5編碼
: {
: string pwd = "";
: MD5 md5 = MD5.Create();
: byte[] s = md5.ComputeHash(Encoding.Default.GetBytes(str));
: for (int i = 0; i < s.Length; i++)
: {
: pwd = pwd + s[i].ToString("X");
: }
: return pwd;
: 這樣雖然可以正常編碼
: 但跟php中的md5編碼總是會少一個0
: 譬如:
: 要加密的字串是: asd
: PHP加密後是: 7815696ECBF1C96E6894B779456D330E
: C#的加密後是: 7815696ECBF1C96E6894B779456D33E
: 怎麼會這樣呢?
: 我也有試過線上JAVA加密結果如同PHP的啊~~
: 究竟是哪裡出問題了呢??
--
對於已經無法擁有的
唯一能做的是
不要忘記
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 125.230.128.63