MD5

MD5

计算文件的MD5

        static string Md5Encrypt(string path)

        {

            using (FileStream fs = File.OpenRead(path))

            {

                MD5 md5 = MD5.Create();

                byte[] byts = md5.ComputeHash(fs);

                md5.Clear();

                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < byts.Length; i++)

                {

                    sb.Append(byts[i].ToString("x2"));

                }

                return sb.ToString();

            }

}

 

计算字符串的MD5

        public static string getMD5(string str)

        {

 

            StringBuilder sb = new StringBuilder();

            MD5 md5 = MD5.Create();

            byte[] byts = System.Text.Encoding.UTF8.GetBytes(str);

            byte[] md5byts = md5.ComputeHash(byts);

            for (int i = 0; i < md5byts.Length; i++)

            {

                sb.Append(md5byts[i].ToString("x2"));

            }

            md5.Clear();

            return sb.ToString();

}

阅读更多
文章标签: string byte path
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭