您的位置:首页 > 编程语言 > C#

C#类实现MD5的加密——另一高级应用

2007-05-06 12:05 337 查看
#region Code Descriptions V20061219
//------------------------------------------------------------------------------
// ClassName: JKLib.Security.MyMD5
// Function : MD5 加密算法algorithm implemented in C#
// Computes the hash value for the specified byte array.
//
// Edit Date: 2006-12-21
//
// Reedit by:
// Edit Note:
// Edit Date:
//------------------------------------------------------------------------------
#endregion

using System;
using System.Security.Cryptography;

namespace JKLib.Security
{
/// <summary>
/// MD5 加密算法 algorithm implemented in C#
/// Computes the hash value for the specified byte array.
/// </summary>
public class MyMD5
{
//返回任意字符串,长度32
//本程序的数据库中Salt字段长度32
private static string GetSalt()
{
Random rnd = new Random();
Byte[] b = new Byte[32];
rnd.NextBytes(b);
return MD5ToHexString(b);
}

/// <summary>
/// 计算密码
/// </summary>
/// <param name="strPassword">用户输入的密码,可能空</param>
/// <param name="salt">salt值</param>
/// <returns>返回MD5加密后的密码</returns>
/// <remarks>
/// 这里主要定义了从salt值以什么方式什么次序计算密码
/// </remarks>
public static string Encrypt(string strPassword, string salt)
{
if (strPassword == null) strPassword = "";
if (salt == null) salt = "";

return MD5ToHexString(strPassword + salt);
}
/// <summary>
/// 加密
/// </summary>
/// <param name="strPassword"></param>
/// <returns></returns>
public static string Encrypt(string strPassword)
{
return Encrypt(strPassword, null);
}
/// <summary>
/// MD5 加密,byte[]型
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] MD5_Byte(byte[] data)
{
MD5 md5 = new MD5CryptoServiceProvider();
return md5.ComputeHash(data);
}
/// <summary>
/// MD5 加密,byte[]型加密为string
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string MD5ToHexString(byte[] data)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(data);
string t = "";
string tTemp = "";
for (int i = 0; i < result.Length; i++)
{
tTemp = Convert.ToString(result[i], 16);
if (tTemp.Length != 2)
{
switch (tTemp.Length)
{
case 0: tTemp = "00"; break;
case 1: tTemp = "0" + tTemp; break;
default: tTemp = tTemp.Substring(0, 2); break;
}
}
t += tTemp;
}
return t;
}
/// <summary>
/// 加密实现
/// </summary>
/// <param name="strText"></param>
/// <returns></returns>
private static string MD5ToHexString(string strText)
{
byte[] data = System.Text.ASCIIEncoding.Unicode.GetBytes(strText);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(data);
string t = "";
for (int i = 0; i < result.Length; i++)
{
t += Convert.ToString(result[i], 16);
}
return t;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: