c# 实现MD5,SHA1,SHA256,SHA512等常用加密算法
2014-12-05 00:00
429 查看
![](http://static.oschina.net/uploads/img/201412/05092715_KRVF.gif)
using
System;
![](http://static.oschina.net/uploads/img/201412/05092715_KRVF.gif)
using
System.IO;
![](http://static.oschina.net/uploads/img/201412/05092715_KRVF.gif)
using
System.Data;
![](http://static.oschina.net/uploads/img/201412/05092715_KRVF.gif)
using
System.Text;
![](http://static.oschina.net/uploads/img/201412/05092715_KRVF.gif)
using
System.Diagnostics;
![](http://static.oschina.net/uploads/img/201412/05092715_KRVF.gif)
using
System.Security;
![](http://static.oschina.net/uploads/img/201412/05092715_KRVF.gif)
using
System.Security.Cryptography;
![](http://static.oschina.net/uploads/img/201412/05092715_KRVF.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_41KY.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_noF2.gif)
/**/
/*
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
* .Net框架由于拥有CLR提供的丰富库支持,只需很少的代码即可实现先前使用C等旧式语言很难实现的加密算法。本类实现一些常用机密算法,供参考。其中MD5算法返回Int的ToString字串。返回数字字母型结果的算法参见之前Blog文章
![](http://static.oschina.net/uploads/img/201412/05092715_KcwM.gif)
*/
![](http://static.oschina.net/uploads/img/201412/05092715_KRVF.gif)
namespace
档案数字化加工
![](http://static.oschina.net/uploads/img/201412/05092715_41KY.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_noF2.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
/**//// <summary>
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// 类名:HashEncrypt
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// 作用:对传入的字符串进行Hash运算,返回通过Hash算法加密过的字串。
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// 属性:[无]
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// 构造函数额参数:
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// IsReturnNum:是否返回为加密后字符的Byte代码
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// IsCaseSensitive:是否区分大小写。
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// 方法:此类提供MD5,SHA1,SHA256,SHA512等四种算法,加密字串的长度依次增大。
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
/// </summary>
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
public class HashEncrypt
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
//private string strIN;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
private bool isReturnNum;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
private bool isCaseSensitive;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
/**//// <summary>
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// 类初始化,此类提供MD5,SHA1,SHA256,SHA512等四种算法,加密字串的长度依次增大。
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// </summary>
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// <param name="IsCaseSensitive">是否区分大小写</param>
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
/// <param name="IsReturnNum">是否返回为加密后字符的Byte代码</param>
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
public HashEncrypt(bool IsCaseSensitive, bool IsReturnNum)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
this.isReturnNum = IsReturnNum;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
this.isCaseSensitive = IsCaseSensitive;
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
private string getstrIN(string strIN)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
//string strIN = strIN;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
if (strIN.Length == 0)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
strIN = "~NULL~";
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
if (isCaseSensitive == false)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
strIN = strIN.ToUpper();
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
return strIN;
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
public string MD5Encrypt(string strIN)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
//string strIN = getstrIN(strIN);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
byte[] tmpByte;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
MD5 md5 = new MD5CryptoServiceProvider();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
tmpByte = md5.ComputeHash(GetKeyByteArray(getstrIN(strIN)));
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
md5.Clear();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
return GetStringValue(tmpByte);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
public string SHA1Encrypt(string strIN)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
//string strIN = getstrIN(strIN);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
byte[] tmpByte;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
SHA1 sha1 = new SHA1CryptoServiceProvider();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
tmpByte = sha1.ComputeHash(GetKeyByteArray(strIN));
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
sha1.Clear();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
return GetStringValue(tmpByte);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
public string SHA256Encrypt(string strIN)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
//string strIN = getstrIN(strIN);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
byte[] tmpByte;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
SHA256 sha256 = new SHA256Managed();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
tmpByte = sha256.ComputeHash(GetKeyByteArray(strIN));
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
sha256.Clear();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
return GetStringValue(tmpByte);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
public string SHA512Encrypt(string strIN)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
//string strIN = getstrIN(strIN);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
byte[] tmpByte;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
SHA512 sha512 = new SHA512Managed();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
tmpByte = sha512.ComputeHash(GetKeyByteArray(strIN));
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
sha512.Clear();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
return GetStringValue(tmpByte);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
/**//// <summary>
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// 使用DES加密(Added by niehl 2005-4-6)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// </summary>
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// <param name="originalValue">待加密的字符串</param>
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// <param name="key">密钥(最大长度8)</param>
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// <param name="IV">初始化向量(最大长度8)</param>
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
/// <returns>加密后的字符串</returns>
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
public string DESEncrypt(string originalValue, string key, string IV)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
//将key和IV处理成8个字符
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
key += "12345678";
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
IV += "12345678";
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
key = key.Substring(0, 8);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
IV = IV.Substring(0, 8);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
SymmetricAlgorithm sa;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
ICryptoTransform ct;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
MemoryStream ms;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
CryptoStream cs;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
byte[] byt;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
sa = new DESCryptoServiceProvider();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
sa.Key = Encoding.UTF8.GetBytes(key);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
sa.IV = Encoding.UTF8.GetBytes(IV);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
ct = sa.CreateEncryptor();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
byt = Encoding.UTF8.GetBytes(originalValue);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
ms = new MemoryStream();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
cs.Write(byt, 0, byt.Length);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
cs.FlushFinalBlock();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
cs.Close();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
return Convert.ToBase64String(ms.ToArray());
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
public string DESEncrypt(string originalValue, string key)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
return DESEncrypt(originalValue, key, key);
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
/**//// <summary>
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// 使用DES解密(Added by niehl 2005-4-6)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// </summary>
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// <param name="encryptedValue">待解密的字符串</param>
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// <param name="key">密钥(最大长度8)</param>
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
/// <param name="IV">m初始化向量(最大长度8)</param>
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
/// <returns>解密后的字符串</returns>
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
public string DESDecrypt(string encryptedValue, string key, string IV)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
//将key和IV处理成8个字符
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
key += "12345678";
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
IV += "12345678";
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
key = key.Substring(0, 8);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
IV = IV.Substring(0, 8);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
SymmetricAlgorithm sa;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
ICryptoTransform ct;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
MemoryStream ms;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
CryptoStream cs;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
byte[] byt;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
sa = new DESCryptoServiceProvider();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
sa.Key = Encoding.UTF8.GetBytes(key);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
sa.IV = Encoding.UTF8.GetBytes(IV);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
ct = sa.CreateDecryptor();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
byt = Convert.FromBase64String(encryptedValue);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
ms = new MemoryStream();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
cs.Write(byt, 0, byt.Length);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
cs.FlushFinalBlock();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
cs.Close();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
return Encoding.UTF8.GetString(ms.ToArray());
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
public string DESDecrypt(string encryptedValue, string key)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
return DESDecrypt(encryptedValue, key, key);
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
private string GetStringValue(byte[] Byte)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
string tmpString = "";
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
if (this.isReturnNum == false)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
ASCIIEncoding Asc = new ASCIIEncoding();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
tmpString = Asc.GetString(Byte);
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
else
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
int iCounter;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
for (iCounter = 0; iCounter < Byte.Length; iCounter++)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
tmpString = tmpString + Byte[iCounter].ToString();
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
return tmpString;
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
private byte[] GetKeyByteArray(string strKey)
![](http://static.oschina.net/uploads/img/201412/05092715_JLO5.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_cR0l.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_9OR2.gif)
{
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
ASCIIEncoding Asc = new ASCIIEncoding();
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
int tmpStrLen = strKey.Length;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
byte[] tmpByte = new byte[tmpStrLen - 1];
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
tmpByte = Asc.GetBytes(strKey);
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
return tmpByte;
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_QlVX.gif)
![](http://static.oschina.net/uploads/img/201412/05092715_CjZV.gif)
}
![](http://static.oschina.net/uploads/img/201412/05092715_KcwM.gif)
}
相关文章推荐
- c# 实现MD5,SHA1,SHA256,SHA512等常用加密算法
- c# 实现MD5,SHA1,SHA256,SHA512等常用加密算法
- c# 实现MD5,SHA1,SHA256,SHA512等常用加密算法
- C# MD5 SHA1 SHA256 SHA384 SHA512 示例 标准版 专业版 旗舰版
- 如何利用openssl来计算md4, md5, sha1, sha256, sha384, sha512等常用哈希值?
- C#实现md5加密 sha1加密 sha256加密 sha384加密 sha512加密 des加密解密
- C# MD5 SHA1 SHA256 SHA384 SHA512 示例 标准版 专业版 旗舰版
- 如何利用openssl来计算md4, md5, sha1, sha256, sha384, sha512等常用哈希值?
- C# MD5 SHA1 SHA256 SHA384 SHA512 示例 标准版 专业版 旗舰版
- MD5, SHA1, SHA256 and SHA512 Hash Algorithm Functions in C#
- .net实现md5加密 sha1加密 sha256加密 sha384加密 sha512加密 des加密解密
- 加密算法比较:SHA1,SHA256,MD5
- 摘要算法CRC8、CRC16、CRC32,MD2 、MD4、MD5,SHA1、SHA256、SHA384、SHA512,RIPEMD、PANAMA、TIGER、ADLER32
- Objective-C的字符串MD5、SHA1、SHA256、AES加密的实现
- 完整加密类(提供MD5,SHA1,SHA256,SHA512,DES加密和解密)
- 浅谈C#加密中MD5和SHA1加密实现
- 几种加密算法的java实现包括MD5、RSA、SHA256
- 文件 MD5 SHA1 SHA256 SHA512 校验码生成工具 V1.3
- 【转】asp.net(c#)加密解密算法之sha1、md5、des、aes实现源码详解