C#中使用SHA1和MD5加密字符串

SHA1和MD5加密均为不可逆加密。代码如下:

using System.Security.Cryptography;  //添加Using

static void Main(string[] args)
        {
            string password = "0123456789";

            //SHA1加密方法
            var sha1 = new SHA1CryptoServiceProvider();
            byte[] str01 = Encoding.Default.GetBytes(password);
            byte[] str02 = sha1.ComputeHash(str01);
            var result = BitConverter.ToString(str02).Replace("-", "");

            //MD5加密方法
            var md5 = new MD5CryptoServiceProvider();
            byte[] str1 = Encoding.Default.GetBytes(password);    
            byte[] str2 = md5.ComputeHash(str1);
            var rethash = BitConverter.ToString(str2).Replace("-", "");

            Console.WriteLine(rethash);
            Console.WriteLine(result);
            Console.Read();
        }

 

posted @ 2016-03-10 17:10  Halo茫  阅读(2623)  评论(2编辑  收藏  举报