C#实现MD5算法

using UnityEngine;
using System.Collections;
using System.Security.Cryptography;

public class GetMD5 : MonoBehaviour {

    public string GetMD5Str(string str)
    {
        MD5 md5 = new MD5CryptoServiceProvider();
        //将字符串转换为字节数组
        byte[] fromData = System.Text.Encoding.Unicode.GetBytes(str);
        //计算字节数组的哈希值
        byte[] toData = md5.ComputeHash(fromData);
        string byteStr = "";
        for (int i = 0; i < toData.Length; i++)
        {
            byteStr += toData[i].ToString("x");
        }
        return byteStr.Substring(0, 8);
    }
}

posted on 2015-03-18 23:12  王亮1  阅读(368)  评论(0编辑  收藏  举报

导航