110,571
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Text;
using System.Security.Cryptography;
namespace CommonLib
{
/// <summary>
/// clsEncrypt 的摘要说明。
/// </summary>
public class clsEncrypt
{
private clsEncrypt()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region "加密解密函数"
//***********************************************************
//* 函数名称:Encrypt
//* 功能描述:加密字符串
//* 参 数:
//* strText --- 待加密的字符串
//* strKey --- 加密密钥
//* eProviderType --- 加密服务提供程序类型
//* 返 回 值:返回加密后的字符串
//* 作 者:
//* 创建日期:2004-07-19
//***********************************************************
public static string Encrypt(string strText,string strKey)
{
return Encrypt(strText,strKey,EnumProviderType.DES);
}
public static string Encrypt(string strText, string strKey ,EnumProviderType eProvider )
{
ICryptoTransform IEncryptor;
string s=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strKey, "MD5").Substring(0,8);
byte []key= ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strKey, "MD5").Substring(0,8));
try
{
if ( eProvider == EnumProviderType.DES )
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key =key;
des.IV = key;
IEncryptor = des.CreateEncryptor();
}
else
{
RC2CryptoServiceProvider des = new RC2CryptoServiceProvider();
des.Key = key;
des.IV = key;
IEncryptor = des.CreateEncryptor();
}
Byte []inputByteArray = Encoding.UTF8.GetBytes(strText);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, IEncryptor, CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
catch
{
return "";
}
}
//***********************************************************
//* 函数名称:Decrypt
//* 功能描述:解密字符串
//* 参 数:
//* strText --- 待解密的字符串
//* strKey --- 解密密钥
//* eProviderType --- 加密服务提供程序类型
//* 返 回 值:返回解密后的字符串
//* 作 者:
//* 创建日期:2004-07-19
//***********************************************************
public static string Decrypt(string strText,string strKey)
{
return Decrypt(strText,strKey,EnumProviderType.DES);
}
public static string Decrypt( string strText, string strKey , EnumProviderType eProvider)
{
ICryptoTransform IDecryptor;
byte []key= ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strKey, "MD5").Substring(0,8));
try
{
if ( eProvider == EnumProviderType.DES )
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key =key;
des.IV = key;
IDecryptor = des.CreateDecryptor();
}
else
{
RC2CryptoServiceProvider des = new RC2CryptoServiceProvider();
des.Key = key;
des.IV = key;
IDecryptor = des.CreateDecryptor();
}
int len =strText.Length / 2 - 1;
byte []inputByteArray=new byte[len+1];
int x,i;
for ( x = 0; x <= len;x++)
{
i = Convert.ToInt32(strText.Substring(x * 2, 2), 16);
inputByteArray[x] = Convert.ToByte(i);
} //
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, IDecryptor, CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.UTF8.GetString(ms.ToArray());
}
catch
{
return "";
}
}
#endregion
}
#region "EnumProviderType --- 加密服务提供程序类型"
public enum EnumProviderType
{
DES = 0, //DES 算法的加密服务提供程序 (CSP) 版本的包装对象。
RC2 = 1 //RC2 算法的加密服务提供程序 (CSP) 实现的包装对象
}
#endregion
}