版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011484013/article/details/53888506
MD5的全称是message-digest algorithm 5(信息-摘要算法,在90年代初由mit laboratory for computer science和rsa data security inc的ronald l. rivest开发出来, 经md2、md3和md4发展而来。
MD5具有很好的安全性(因为它具有不可逆的特征,加过密的密文经过解密后和加密前的东东相同的可能性极小)
sharp demo
using System;
using System.Security.Cryptography;
using System.Text;
namespace CSharpTest1
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.WriteLine(GetMd5Str1("hello"));//BC4B2A76B9719D91
Console.WriteLine(UserMd5("hello"));
}
/**//// <summary>
/// MD5 16位加密 加密后密码为大写
/// </summary>
/// <param name="ConvertString"></param>
/// <returns></returns>
public static string GetMd5Str1(string ConvertString)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(ConvertString)), 4, 8);
t2 = t2.Replace("-", "");
return t2;
}
/**//// <summary>
/// MD5 32位加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
static string UserMd5(string str)
{
string cl = str;
StringBuilder pwd = new StringBuilder();
MD5 md5 = MD5.Create();//实例化一个md5对像
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
for (int i = 0; i < s.Length; i++)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
pwd.Append(s[i].ToString("X2"));
//pwd = pwd + s[i].ToString("X");
}
return pwd.ToString();
}
}
}
unity demo
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Md5Test : MonoBehaviour {
public string fileName="1.txt";//streamingAssets 文件夹下面的文件名称
public string testSting = "this is test string";
/// <summary>
/// 将一个文件转换为md5字符串,并保存
/// </summary>
/// <param name="fileName">File name.</param>
void PaseFile(string fileName){
string filePath = System.IO.Path.Combine (Application.streamingAssetsPath, fileName);//文件路径
string fileMd5Path=System.IO.Path.Combine (Application.streamingAssetsPath, "md5_"+fileName);//md5 存储路径
if(System.IO.File.Exists(filePath)){
using(System.IO.FileStream stream=System.IO.File.Open(filePath,System.IO.FileMode.Open)){
stream.Position = 0;//从文件首部开始
string md5 = GetMd5FromStream (stream);//获取文件对应的md5数据
System.IO.FileStream fs = new System.IO.FileStream (fileMd5Path,System.IO.FileMode.Create,System.IO.FileAccess.Write,System.IO.FileShare.Write);
byte[] buff = System.Text.Encoding.UTF8.GetBytes (md5);
fs.Write (buff,0,buff.Length);//保存生成的md5信息
fs.Close ();
Debug.Log ("<color=#ff0000> change over</color>");
}
}
}
/// <summary>
///
/// 通过数据流获取对应的md5文件
/// </summary>
/// <returns>The md5.</returns>
/// <param name="stream">Stream.</param>
string GetMd5FromStream(System.IO.FileStream stream){
byte[] buff;
using(System.Security.Cryptography.MD5 md5=new System.Security.Cryptography.MD5CryptoServiceProvider()){
buff = md5.ComputeHash (stream);
}
System.Text.StringBuilder builder = new System.Text.StringBuilder ();
foreach (var item in buff) {
builder.Append (item.ToString ("x2").ToLower());//把二进制的字节,转换为16进制的数字形式的字符串
}
string res = builder.ToString ();
Debug.Log (res);
return res;
}
/// <summary>
/// 通过字节数组获取md5字符串
/// </summary>
/// <returns>The md5 from bytes.</returns>
/// <param name="data">Data.</param>
string GetMd5FromBytes(byte[] data){
byte[] buff;
using(System.Security.Cryptography.MD5 md5=new System.Security.Cryptography.MD5CryptoServiceProvider()){
buff = md5.ComputeHash (data);
}
System.Text.StringBuilder builder = new System.Text.StringBuilder ();
foreach (var item in buff) {
builder.Append (item.ToString ("x2").ToLower());//把二进制的字节,转换为16进制的数字形式的字符串
}
string res = builder.ToString ();
Debug.Log (res);
return res;
}
void OnGUI(){
if(GUI.Button(new Rect(30,30,100,30),"file md5")){
PaseFile (fileName); //将一个文件转换为md5字符串,并保存
}
if(GUI.Button(new Rect(30,80,100,30),"string md5")){
byte[] data = System.Text.Encoding.UTF8.GetBytes (testSting);//将字符串转换为字节数组
string md5=GetMd5FromBytes (data);//通过字节数组获取对应的md5字符串
Debug.Log ("<color=#ff0000> change over</color>");
}
}
}