MD5加密

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

namespace MD51X
{
    class MD
    {
        private string sString;
        public MD(string text)
        {
            sString=text;
        }
        /// <summary>
        /// MD5算法算出的字符串
        /// </summary>
        /// <returns>算出的字符串</returns>
        public string ToString()
        {
            return getMd5Hash(sString);
        }      
        private string getMd5Hash(string input)
        {
            MD5 md5Hasher = MD5.Create();

            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }
    }
}

上面的是MD5加密的类(一定要加这个命名空间:using System.Security.Cryptography;)

下面是主程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MD51X
{
    class Program
    {
        static void Main(string[] args)
        {
           
            Console.WriteLine("请输入,加密字符串");
            string pwd = Console.ReadLine();
            string Md5password = (new MD(pwd)).ToString();
            Console.WriteLine("输出加密后的字符");
            Console.WriteLine(Md5password);
            Console.ReadLine();
        }
    }
}

下面试运算结果

posted @ 2012-09-28 16:41  忘-语  阅读(193)  评论(1编辑  收藏  举报