最简单的MD5算法

#region MD5算法(md5不可解密,加密后判断是否相等)
public string md5(string str, int code)
{
if (code == 16) //16位MD5加密(取32位加密的9~25字符)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 16);
}
if (code == 32) //32位加密
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
}
return "00000000000000000000000000000000";
}
#endregion

        //加密
        public static string EnCode(string s)
        {
            string encodestr = "";
            byte[] bytes = System.Text.Encoding.GetEncoding(0).GetBytes(s);
            try
            {
                encodestr = Convert.ToBase64String(bytes);
            }
            catch
            {
                encodestr = s;
            }
            return encodestr;
        }

         //解密
        public static string DeCode(string s)
        {
            string decodestr = "";
            byte[] bytes = Convert.FromBase64String(s);
            try
            {
                decodestr = System.Text.Encoding.GetEncoding(0).GetString(bytes);
            }
            catch
            {
                decodestr = s;
            }
            return decodestr;
        }

 

posted @ 2009-01-08 11:06  xumingming  阅读(813)  评论(1编辑  收藏  举报