using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; namespace _19_MD5加密 { class Program { static void Main(string[] args) { string exit = ""; do{ Console.WriteLine("请输入要加密的字符!"); string str = Console.ReadLine(); if (str != "q") { //调用方法GetMd5(str)对输入的字符串进行加密 Console.WriteLine(GetMd5(str)); } else { break; } }while(exit!="q");//在!=Q的时候,继续循环 Console.ReadKey(); } public static string GetMd5(string str) { //将字符串转成字符数组 byte[] butffer = Encoding.Default.GetBytes(str); MD5 md5pwd = MD5.Create(); //将加密码后的数据保存到一个字符数组中 byte [] output =md5pwd.ComputeHash(butffer); //定义一空字符串来接收数组里面数据 string bytestr = ""; //循环遍历数组,赋值给字符串 for (int i = 0; i <output.Length; i++) { //加ToString是因为字符数组里面存储的是十进制,需要转换为16进制, //ToString("x2")表示转换为完整的16进制 bytestr += output[i].ToString("x2"); } //返回字符串 return bytestr; } } }
原文:http://www.cnblogs.com/hobe6699/p/5159270.html