数据加密技术之MD5加密

                                                数据加密之MD5加密

          最近帮老师整理一个ASP.Net项目,其中的用户登录注册需要对用户的密码进行数据加密,我用的是MD5加密,首先在用户注册时把其密码加密保存到数据库,验证登录密码时只需要再次将用户输入的密码加密与其注册时保存到数据库的密码对比,如果一样,则登录成功,好了废话不多说,直接看项目代码吧!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Web;
namespace MyMD5
{
    public class MyMD5
    {
        public static string Encrypt(string str)  //32位加密;
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str); //采用Utf8将要加密的字符串编码为byte数组;
            bytes = md5.ComputeHash(bytes);  //采用MD5再次编码
            md5.Clear();
            string ret = "";
            for (int i = 0; i < bytes.Length; i++)
            {
                ret += Convert.ToString(bytes[i], 16).PadLeft(2,'0');//将每个字符转化为16进制;
            }
            return ret.PadLeft(32,'0');
        }
        //16位加密略
    }
}
使用时可以像这样:string result = MyMD5.MyMD5.Encrypt(pwdTxt.Text);
result即为加密后的值;
  public static string GetMD5(string str)
        {
            MD5 md5 = MD5.Create();  //创建MD5对象;
            byte[] buffer = System.Text.Encoding.GetEncoding("GBK").GetBytes(str);
            byte[] md5Buffer = md5.ComputeHash(buffer);
            string strNew = "";
            for (int i = 0; i < md5Buffer.Length; i++)
            {
                strNew += md5Buffer[i].ToString("x2");
            }
            return strNew;
        }

以上是我对MD5加密的使用,不足之处望指出哦,大家共同进步!!! 得意
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值