MD5加密

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Security.Cryptography;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 
 9 namespace ConsoleApplication6
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             //张三 123
16             string str = "123";
17             string md5 = GetMd5(str);
18             Console.WriteLine(md5);
19             Console.ReadKey();
20         }
21         static string GetMd5(string str)
22         {
23             MD5 md5 = MD5.Create();//创建MD5实例对象
24             //开始加密
25             //将传入对象转换为字节数字
26             byte[] blist = Encoding.Default.GetBytes(str);
27             //将字节数组传入加密方法里面,返回一个字节数组
28             byte[] bmd5list = md5.ComputeHash(blist);
29             //将字节数组转换为string
30             StringBuilder sb = new StringBuilder();
31             for (int i = 0; i < bmd5list.Length; i++)
32             {
33                 sb.Append(bmd5list[i].ToString("x2"));//x表示将十进制转换为16进制。2表示每次都是两位数
34             }
35             return sb.ToString();
36         }
37     }
38 }

 md5加密不可逆。如果要进行比对,可以先进行加密,对比加密后的值是否想等。

posted @ 2017-02-12 15:43  冲天小肥牛  阅读(155)  评论(0编辑  收藏  举报