<span style="font-family: Arial, Helvetica, sans-serif;">public static string GetMD5(string source)</span>
{
MD5 md5 = MD5.Create();
byte[] sourcebytes=Encoding.Default.GetBytes(source);
byte[] md5bytes=md5.ComputeHash(sourcebytes);
string md5string = "";
for (int i = 0; i < md5bytes.Length; i++)
{
md5string += md5bytes[i].ToString("x2");
}
return md5string;
}
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif;">using System.Linq;</span>
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.Security.Cryptography;
namespace 练习控制台
{
class Program
{
static void Main(string[] args)
{
string str = GetMD5("123");
Console.WriteLine(str);
}
public static string GetMD5(string str)
{
MD5 md5= MD5.Create();
byte[] bts = Encoding.Default.GetBytes(str);
byte[] md5Buffer=md5.ComputeHash(bts);
return Encoding.Default.GetString(md5Buffer);
}
}
}
注意用 MD5.Create()创建对象。
得到2进制对象,必须解析成16进制;
得到10进制对象:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.Security.Cryptography;
namespace 练习控制台
{
class Program
{
static void Main(string[] args)
{
string str = GetMD5("123");
Console.WriteLine(str);
}
public static string GetMD5(string str)
{
MD5 md5= MD5.Create();
byte[] bts = Encoding.Default.GetBytes(str);
byte[] md5Buffer=md5.ComputeHash(bts);
string strs = "";
for (int i = 0; i < md5Buffer.Length; i++)
strs += md5Buffer[i].ToString();
return strs;
}
}
}
得到16进制的md5:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.Security.Cryptography;
namespace 练习控制台
{
class Program
{
static void Main(string[] args)
{
string str = GetMD5("123");
Console.WriteLine(str);
}
public static string GetMD5(string str)
{
MD5 md5= MD5.Create();
byte[] bts = Encoding.Default.GetBytes(str);
byte[] md5Buffer=md5.ComputeHash(bts);
string strs = "";
for (int i = 0; i < md5Buffer.Length; i++)
strs += md5Buffer[i].ToString("x2"); //16进制格式输出
return strs;
}
}
}