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;
}
}
}
//------------------------------------------------------------------------------
// 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;
}
}
}
相关文章推荐
- java MD5 加密实现
- php结合md5实现的加密解密方法
- php md5加密16位32位的实现代码
- 应用Cryptopp库实现AES加密【转】
- 关于sentinel LDK加密war包实现应用加密的使用方法
- (转)Oracle、SQL Server下MD5+Base64密码加密实现
- 使用java.security.MessageDigest类来实现MD5、SHA加密
- 非对称加密RSA的应用及在C#中的实现
- highcharts 高级应用—动态饼图的实现
- highcharts 高级应用—动态曲线的实现
- highcharts 高级应用—动态柱状图的实现
- java加密用户登录密码--不用MD5实现
- 在Java中利用Apache Commons Codec API实现常见的加密解密算法,如:md5,sha256
- MD5实现加密工具
- [转载]基于ssl加密通信的rmi应用实现
- 加密、解密、openssl的基本应用及CA的实现过程
- mfc实现哈希Hash算法(MD5和SHA1)加密
- 一个可应用在ASP 标记加密文件的MD5的DLL组件 {81K}
- 用SHA1或MD5 算法加密数据(示例:对用户身份验证的简单实现)
- MD5加密原理和Java实现