添加一个公共类MD

编写MD类,其中要引用MD5加密算法的命名空间

using System.Security.Cryptography;

写入MD的公共类代码

完整代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Security.Cryptography;  //MD5加密算法引用命名空间
    using System.Text;

    /// <summary>
    ///MD 的摘要说明
    /// </summary>
    public class MD
    {
    public MD()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    /// <summary>
    /// 对密码进行MD5加密
    /// </summary>
    /// <param name="strPwd"></param>
    /// <returns></returns>
    public string GetMD5(string strPwd)
    {
        string pwd = "";
        MD5 md5 = MD5.Create(); //实例化一个md5对象

        byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(strPwd));  //将密码转换成utf8编码的字节码
        //将字节码进行md5加密,算出hash值
        s.Reverse();  //将结果进行翻转
        for (int i = 3; i < s.Length - 1; i++)
        {
            pwd = pwd + (s[i] < 198 ? s[i] + 28 : s[i]).ToString("X");
        }
        return pwd;
     }
    }

网页中运行示例

jiami.png

Last modification:September 21st, 2019 at 04:17 pm
If you think my article is useful to you, please feel free to appreciate