C#中采用SHA1或MD5加密字符串的函数

#region 获取由SHA1加密的字符串
public string EncryptToSHA1(string str)
{
            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
            byte[] str1 = Encoding.UTF8.GetBytes(str);
            byte[] str2 = sha1.ComputeHash(str1);
            sha1.Clear();
            (sha1 as IDisposable).Dispose();
            return Convert.ToBase64String(str2);
}
#endregion
#region 获取由MD5加密的字符串
public string EncryptToMD5(string str)
{
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] str1 = Encoding.UTF8.GetBytes(str);
            byte[] str2 = md5.ComputeHash(str1, 0, str1.Length);
            md5.Clear();
            (md5 as IDisposable).Dispose();
            return Convert.ToBase64String(str2);
}
#endregion

 

posted @ 2013-03-30 09:47  摩根船长  阅读(11394)  评论(0编辑  收藏  举报