MD5加密方法

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{ string str1 = Console.ReadLine();

string sr = Clas.MD5Encrypt(str1);
Console.WriteLine(sr);

string f = Clas.MD5(sr);
Console.WriteLine(f);
Console.ReadLine();

}

 

static string myMD5(string str)
{
string pwd = "";
MD5 md5 = new MD5CryptoServiceProvider();
Byte[] byt = md5.ComputeHash(Encoding.Unicode.GetBytes(str));
for (int i = 0; i < byt.Length; i++)
{
pwd += byt[i].ToString("x");
}
return pwd;
}
/// <summary>
/// MD5 加密(不可逆加密)
/// </summary>
/// <param name="pass">要加密的原始字串</param>
/// <returns></returns>
public static string MD5Encrypt(string pass)
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bytResult = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(pass));
md5.Clear();
string strResult = BitConverter.ToString(bytResult);
strResult = strResult.Replace("-", "");
return strResult;
}

 

 

 

public static string MD5 (this string text)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile (text, "md5");
}


[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "SHA")]
public static string SHA1 (this string text)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile (text, "sha1");
}

posted @ 2014-04-23 13:46  醉孟子  阅读(333)  评论(0编辑  收藏  举报