csharp 计算MD5-Hash
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 计算MD5-Hash相关的知识,希望对你有一定的参考价值。
<span style="color: #008080; font-style: italic;">/// <summary></span>
<span style="color: #008080; font-style: italic;">/// Calculates a MD5 hash from the given string and uses the given</span>
<span style="color: #008080; font-style: italic;">/// encoding.</span>
<span style="color: #008080; font-style: italic;">/// </summary></span>
<span style="color: #008080; font-style: italic;">/// <param name="Input">Input string</param></span>
<span style="color: #008080; font-style: italic;">/// <param name="UseEncoding">Encoding method</param></span>
<span style="color: #008080; font-style: italic;">/// <returns>MD5 computed string</returns></span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> CalculateMD5<span style="color: #000000;">(</span><span style="color: #FF0000;">string</span> Input, Encoding UseEncoding<span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
<span style="color: #000000;">System</span>.<span style="color: #0000FF;">Security</span>.<span style="color: #0000FF;">Cryptography</span>.<span style="color: #0000FF;">MD5CryptoServiceProvider</span> CryptoService;
CryptoService = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Security</span>.<span style="color: #0000FF;">Cryptography</span>.<span style="color: #0000FF;">MD5CryptoServiceProvider</span><span style="color: #000000;">(</span><span style="color: #000000;">)</span>;
<span style="color: #FF0000;">byte</span><span style="color: #000000;">[</span><span style="color: #000000;">]</span> InputBytes = UseEncoding.<span style="color: #0000FF;">GetBytes</span><span style="color: #000000;">(</span>Input<span style="color: #000000;">)</span>;
InputBytes = CryptoService.<span style="color: #0000FF;">ComputeHash</span><span style="color: #000000;">(</span>InputBytes<span style="color: #000000;">)</span>;
<span style="color: #0600FF;">return</span> BitConverter.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">(</span>InputBytes<span style="color: #000000;">)</span>.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">(</span><span style="color: #808080;">"-"</span>, <span style="color: #808080;">""</span><span style="color: #000000;">)</span>;
<span style="color: #000000;">}</span>
<span style="color: #008080; font-style: italic;">/// <summary></span>
<span style="color: #008080; font-style: italic;">/// Calculates a MD5 hash from the given string. </span>
<span style="color: #008080; font-style: italic;">/// (By using the default encoding)</span>
<span style="color: #008080; font-style: italic;">/// </summary></span>
<span style="color: #008080; font-style: italic;">/// <param name="Input">Input string</param></span>
<span style="color: #008080; font-style: italic;">/// <returns>MD5 computed string</returns></span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> CalculateMD5<span style="color: #000000;">(</span><span style="color: #FF0000;">string</span> Input<span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
<span style="color: #008080; font-style: italic;">// That's just a shortcut to the base method</span>
<span style="color: #0600FF;">return</span> CalculateMD5<span style="color: #000000;">(</span>Input, <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Text</span>.<span style="color: #0000FF;">Encoding</span>.<span style="color: #0600FF;">Default</span><span style="color: #000000;">)</span>;
<span style="color: #000000;">}</span>
以上是关于csharp 计算MD5-Hash的主要内容,如果未能解决你的问题,请参考以下文章