浅看C# md5加密

参考网上代码写了个MD5加密函数如下

首先使用如下命名空间

using System;

using System.Security.Cryptography;

函数部分:

public static string MD5Encode(string strText)
    {
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] result = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strText));
        return BitConverter.ToString(result).Replace("-", "").ToLower();
    }

//strText为与加密字段

----------------------------说明------------------------------

System.Text.Encoding.UTF8.GetBytes(strText)中UTF8为编码方式,.net中默认为UTF8,即与

System.Text.Encoding.Default.GetBytes(strText)相同,除此之外还有UTF7、Unicode等等,对于

不同编码,加密后的结果会不相同,使用时要注意。

在php中调用md5()加密后的字符串中字母全为小写出现,但是C#中直接

return BitConverter.ToString(result).Replace("-", "")输出加密字符串中字母却全为大写,故用

ToLower()转换下

至于其他加密方式,还在学习中。

PS:每几天写篇博文当做对自己的勉励,坚持写下去,表明自己一直在学习,记录下来学习过程。

原文地址:https://www.cnblogs.com/vvwang/p/3114068.html