使用MD5加密字符串

Posted on 2019-06-04 21:47  努力成长静待花开  阅读(140)  评论(0编辑  收藏  举报

实现效果:

  

实现代码:

static void Main(string[] args)
        {
            Console.WriteLine("Enter a string to MD5 encryption...\n");
            while (true)
            {
                Console.WriteLine(GetMD5(Console.ReadLine()));   
            }
        }
        static string GetMD5(string str)
        {
            byte[] bts1 = Encoding.Default.GetBytes(str);
            MD5 md5 = MD5.Create();
            StringBuilder result = new StringBuilder();
            byte[] bts2 = md5.ComputeHash(bts1);
            foreach (byte item in bts2)
            {
                result.Append(item.ToString("x2"));
            }
            return result.ToString();
        }