using
System;
using
System.IO;
using
System.Data;
using
System.Text;
using
System.Diagnostics;
using
System.Security;
using
System.Security.Cryptography;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*
* .Net框架由于拥有CLR提供的丰富库支持,只需很少的代码即可实现先前使用C等旧式语言很难实现的加密算法。本类实现一些常用机密算法,供参考。其中MD5算法返回Int的ToString字串。返回数字字母型结果的算法参见之前Blog文章
*/
namespace
档案数字化加工
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 类名:HashEncrypt
/// 作用:对传入的字符串进行Hash运算,返回通过Hash算法加密过的字串。
/// 属性:[无]
/// 构造函数额参数:
/// IsReturnNum:是否返回为加密后字符的Byte代码
/// IsCaseSensitive:是否区分大小写。
/// 方法:此类提供MD5,SHA1,SHA256,SHA512等四种算法,加密字串的长度依次增大。
/// </summary>
public class HashEncrypt
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//private string strIN;
private bool isReturnNum;
private bool isCaseSensitive;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 类初始化,此类提供MD5,SHA1,SHA256,SHA512等四种算法,加密字串的长度依次增大。
/// </summary>
/// <param name="IsCaseSensitive">是否区分大小写</param>
/// <param name="IsReturnNum">是否返回为加密后字符的Byte代码</param>
public HashEncrypt(bool IsCaseSensitive, bool IsReturnNum)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.isReturnNum = IsReturnNum;
this.isCaseSensitive = IsCaseSensitive;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string getstrIN(string strIN)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//string strIN = strIN;
if (strIN.Length == 0)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strIN = "~NULL~";
}
if (isCaseSensitive == false)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strIN = strIN.ToUpper();
}
return strIN;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string MD5Encrypt(string strIN)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//string strIN = getstrIN(strIN);
byte[] tmpByte;
MD5 md5 = new MD5CryptoServiceProvider();
tmpByte = md5.ComputeHash(GetKeyByteArray(getstrIN(strIN)));
md5.Clear();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return GetStringValue(tmpByte);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string SHA1Encrypt(string strIN)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//string strIN = getstrIN(strIN);
byte[] tmpByte;
SHA1 sha1 = new SHA1CryptoServiceProvider();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
tmpByte = sha1.ComputeHash(GetKeyByteArray(strIN));
sha1.Clear();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return GetStringValue(tmpByte);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string SHA256Encrypt(string strIN)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//string strIN = getstrIN(strIN);
byte[] tmpByte;
SHA256 sha256 = new SHA256Managed();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
tmpByte = sha256.ComputeHash(GetKeyByteArray(strIN));
sha256.Clear();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return GetStringValue(tmpByte);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string SHA512Encrypt(string strIN)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//string strIN = getstrIN(strIN);
byte[] tmpByte;
SHA512 sha512 = new SHA512Managed();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
tmpByte = sha512.ComputeHash(GetKeyByteArray(strIN));
sha512.Clear();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return GetStringValue(tmpByte);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 使用DES加密(Added by niehl 2005-4-6)
/// </summary>
/// <param name="originalValue">待加密的字符串</param>
/// <param name="key">密钥(最大长度8)</param>
/// <param name="IV">初始化向量(最大长度8)</param>
/// <returns>加密后的字符串</returns>
public string DESEncrypt(string originalValue, string key, string IV)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//将key和IV处理成8个字符
key += "12345678";
IV += "12345678";
key = key.Substring(0, 8);
IV = IV.Substring(0, 8);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SymmetricAlgorithm sa;
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
sa = new DESCryptoServiceProvider();
sa.Key = Encoding.UTF8.GetBytes(key);
sa.IV = Encoding.UTF8.GetBytes(IV);
ct = sa.CreateEncryptor();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
byt = Encoding.UTF8.GetBytes(originalValue);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cs.Close();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return Convert.ToBase64String(ms.ToArray());
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string DESEncrypt(string originalValue, string key)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DESEncrypt(originalValue, key, key);
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 使用DES解密(Added by niehl 2005-4-6)
/// </summary>
/// <param name="encryptedValue">待解密的字符串</param>
/// <param name="key">密钥(最大长度8)</param>
/// <param name="IV">m初始化向量(最大长度8)</param>
/// <returns>解密后的字符串</returns>
public string DESDecrypt(string encryptedValue, string key, string IV)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//将key和IV处理成8个字符
key += "12345678";
IV += "12345678";
key = key.Substring(0, 8);
IV = IV.Substring(0, 8);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SymmetricAlgorithm sa;
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
sa = new DESCryptoServiceProvider();
sa.Key = Encoding.UTF8.GetBytes(key);
sa.IV = Encoding.UTF8.GetBytes(IV);
ct = sa.CreateDecryptor();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
byt = Convert.FromBase64String(encryptedValue);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cs.Close();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return Encoding.UTF8.GetString(ms.ToArray());
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string DESDecrypt(string encryptedValue, string key)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DESDecrypt(encryptedValue, key, key);
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string GetStringValue(byte[] Byte)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string tmpString = "";
if (this.isReturnNum == false)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ASCIIEncoding Asc = new ASCIIEncoding();
tmpString = Asc.GetString(Byte);
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int iCounter;
for (iCounter = 0; iCounter < Byte.Length; iCounter++)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tmpString = tmpString + Byte[iCounter].ToString();
}
}
return tmpString;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private byte[] GetKeyByteArray(string strKey)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ASCIIEncoding Asc = new ASCIIEncoding();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int tmpStrLen = strKey.Length;
byte[] tmpByte = new byte[tmpStrLen - 1];
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
tmpByte = Asc.GetBytes(strKey);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return tmpByte;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
转载于:https://www.cnblogs.com/dreign/archive/2007/05/18/751089.html