MD5加密简单算法

public partial class MD5运用 : Form
{
  public MD5运用()
  {
    InitializeComponent();
  }

  private void btnChange_Click(object sender, EventArgs e)
  {
    string password = txtPassword.Text.Trim();
    //string MD5Password= getMD5(password);
    MD5 md5 = MD5.Create();//MD5抽象类无法实例化 实例化该方法
    byte[] buffer = Encoding.Default.GetBytes(password);//将字符串转换为字节数组
    byte[] mdbuffer = md5.ComputeHash(buffer);//调用ComputeHash方法把数组传进去
    //将字节数组中每个元素ToString();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < mdbuffer.Length; i++)
    {
      sb.Append(mdbuffer[i].ToString("x2"));////ToString得到十进制字符串 ToString("x")十六进制字符串 ToString("x2")对齐
    }

    //第二次加密
    //string newpassword = sb.ToString();
    //buffer = Encoding.Default.GetBytes(newpassword);
    //mdbuffer = md5.ComputeHash(buffer);
    //for (int i = 0; i < mdbuffer.Length; i++)
    //{
      // sb.Append(mdbuffer[i].ToString("x2"));
    //}
    txtMD5.Text = sb.ToString();//将值赋给name为txtMD5的控件
    //第一次return:47f4bd95b8fba149a1495a4a35a821b1
    //第二次return:47f4bd95b8fba149a1495a4a35a821b16ac20533c9d8a9ac1ae738aefb6ba9c8
  }
}

posted @ 2015-08-06 14:47  如此低调的男人  阅读(313)  评论(0编辑  收藏  举报