C#MD5加密

1.生成的MD5字符串,这些字母表示的是16进制数,因此不区分大小写,所以大写小写都可以

2.item.ToString("x2"))    中的x2 --> x表示小写,X表示大写,2表示用两位进行格式化

如果两个数10和26,正常情况十六进制显示0xA、0x1A,这样看起来不整齐,为了好看,我们可以指定X2,这样显示出来就是:0x0A、0x1A。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;

 

        #region 获得一个文件的MD5  
        public static string GetFileMD5(string filepath) 
        { 
            StringBuilder sb = new StringBuilder(); 
            using (MD5 md5=MD5.Create()) 
            { 
                using (FileStream fs=File.OpenRead(filepath)) 
                { 
                    byte[] newB = md5.ComputeHash(fs); 
                    foreach (byte item in newB) 
                    { 
                        sb.Append(item.ToString("x2")); 
                    } 
                } 
            } 
 
            return sb.ToString(); 
        } 
        #endregion 


        #region 获得字符串的md5值  
        public static string GetMD5(string msg)
        { 
            StringBuilder sb = new StringBuilder(); 
 
            using (MD5 md5=MD5.Create()) 
            { 
                byte[] buffer = Encoding.UTF8.GetBytes(msg); 
                byte[] newB = md5.ComputeHash(buffer); 
 
                foreach (byte item in newB) 
                { 
                    sb.Append(item.ToString("x2")); 
                } 
            } 
 
            return sb.ToString(); 
        } 
        #endregion 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值