using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Web.Security;
//工程需要引用system.web.dll
/**//*
* 通用的安全密码类
*
* 本类库支持DES,RC2,Rijndael, TripleDes,RSA, MD5, SHA1等六种加密格式
*
* 注意:MD5, SHA1加密是不可逆的,所以不支持他们的解密;
*
* 支持生成Key/公钥,Iv/私钥及简单的随机字符;
*
* SmartJavaer
*
* SmartJavaer@163.com
*
* 2006/10/26
*
*/
namespace ManagePassword
{
//枚举加密格式
#region 枚举加密格式
/** <summary>
/// 枚举加密的格式
///
/// 对称加密:DES,RC2,RIJNDAEL,TripleDes
/// 非对称加密:DSA,RSA
/// 哈西加密:MD5,SHA1
///
/// </summary>
public enum CryptogramFormat { Des = 1, Rc2, Rijndael, TripleDes,RSA,MD5, SHA1 }
#endregion
//通用密码类
#region 通用密码类
/** <summary>
/// 一个通用的加密类
/// 支持DES,RC2,Rijndael, TripleDes,RSA, MD5, SHA1等六种加密格式
/// 注意:MD5, SHA1加密是不可逆的,所以不支持他们的解密;
/// 支持生成Key/公钥,Iv/私钥及简单的随机字符;
/// </summary>
public class Cryptogram
{
private Cryptogram() { }
//加密,解密内部处理
#region 加密,解密内部处理
/** <summary>
/// 生成Des,Rc2,Rijndael,TripleDes的处理对象
/// </summary>
/// <param name="encryptionFormat">加密格式</param>
/// <param name="key">Key</param>
/// <param name="iv">IV</param>
/// <returns>返回处理对象,若返回null,则表示生成失败;</returns>
private static SymmetricAlgorithm GetCSP(CryptogramFormat encryptionFormat,string key,string iv)
{
SymmetricAlgorithm mCSP;
switch (encryptionFormat)
{
case CryptogramFormat.Des:
{
mCSP = new DESCryptoServiceProvider();
break;
}
case CryptogramFormat.TripleDes:
{
mCSP = new TripleDESCryptoServiceProvider();
break;
}
case CryptogramFormat.Rc2:
{
mCSP = new RC2CryptoServiceProvider();
break;
}
case CryptogramFormat.Rijndael:
{
mCSP = new RijndaelManaged();
break;
}
default:
mCSP = null;
break;
}
if (mCSP == null)
return null;
if(key.Length==0)
// if(String.IsNullOrEmpty(key))
{
//mCSP.GenerateKey();
return null;
}
else
{
try
{
mCSP.Key = Convert.FromBase64String(key);
}
catch
{
//mCSP.GenerateKey();
return null;
}
}
if (iv.Length==0)
//if (String.IsNullOrEmpty(iv))
{
//mCSP.GenerateIV();
return null;
}
else
{
try
{
mCSP.IV = Convert.FromBase64String(iv);
}
catch
{
//mCSP.GenerateIV();
return null;
}
}
return mCSP;
}
/** <summary>
/// Des,Rc2,Rijndael,TripleDes加密
/// </summary>
/// <param name="encryptionFormat">加密格式</param>
/// <param name="srcString">欲加密的字符串</param>
/// <param name="key">Key</param>
/// <param name="iv">IV</param>
/// <returns>返回加密后的字符串,若返回null,则表示加密失败;</returns>
private static string CSPEncode(CryptogramFormat encryptionFormat, string srcString, string key, string iv)
{
SymmetricAlgorithm mCSP;
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP = GetCSP(encryptionFormat, key, iv);
if (mCSP == null)
return null;
try
{
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
byt = Convert.FromBase64String(srcString);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
catch
{
return null;
}
}
/** <summary>
/// Des,Rc2,Rijndael,TripleDes解密
/// </summary>
/// <param name="encryptionFormat"></param>
/// <param name="srcString"></param>
/// <param name="key"></param>
/// <param name="iv"></param>
/// <returns></returns>
private static string CSPDecode(CryptogramFormat encryptionFormat, string srcString, string key, string iv)
{
SymmetricAlgorithm mCSP;
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP = GetCSP(encryptionFormat, key, iv);
if (mCSP == null)
return null;
try
{
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
byt = Convert.FromBase64String(srcString);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
catch
{
return null;
}
}
/** <summary>
/// RSA加密
/// </summary>
/// <param name="encryptionFormat"></param>
/// <param name="srcString"></param>
/// <param name="key"></param>
/// <param name="iv"></param>
/// <returns></returns>
private static string RSAEncode(CryptogramFormat encryptionFormat, string srcString, string key, string iv)
{
RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider();
byte[] toEncryptData = null;
byte[] encryptedData = null;
try
{
rsa1.FromXmlString(key);
}
catch
{
return null;
}
try
{
rsa1.FromXmlString(iv);
}
catch
{
return null;
}
toEncryptData = Convert.FromBase64String(srcString);
encryptedData = rsa1.Encrypt(toEncryptData, false);//加密后的数据
return Convert.ToBase64String(encryptedData);
}
/** <summary>
/// RSA解密
/// </summary>
/// <param name="encryptionFormat"></param>
/// <param name="srcString"></param>
/// <param name="key"></param>
/// <param name="iv"></param>
/// <returns></returns>
private static string RSADecode(CryptogramFormat encryptionFormat, string srcString, string key, string iv)
{
RSACryptoServiceProvider rsaD = new RSACryptoServiceProvider();
byte[] encryptedData = null ;
byte[] decryptedData = null ;
try
{
rsaD.FromXmlString(key);
}
catch
{
return null;
}
try
{
rsaD.FromXmlString(iv);
}
catch
{
return null;
}
encryptedData = Convert.FromBase64String(srcString);
decryptedData = rsaD.Decrypt(encryptedData, false);//解密后的数据
return Convert.ToBase64String(decryptedData);
}
/** <summary>
/// 生成对称加密格式的Key和IV
/// </summary>
/// <param name="encryptionFormat">加密格式</param>
/// <param name="key"></param>
/// <param name="iv"></param>
private static void KeyAndIV(CryptogramFormat encryptionFormat, ref string key, ref string iv)
{
SymmetricAlgorithm mCSP;
switch (encryptionFormat)
{
case CryptogramFormat.Des:
{
mCSP = new DESCryptoServiceProvider();
break;
}
case CryptogramFormat.TripleDes:
{
mCSP = new TripleDESCryptoServiceProvider();
break;
}
case CryptogramFormat.Rc2:
{
mCSP = new RC2CryptoServiceProvider();
break;
}
case CryptogramFormat.Rijndael:
{
mCSP = new RijndaelManaged();
break;
}
default:
mCSP = null;
break;
}
mCSP.GenerateKey();
mCSP.GenerateIV();
key = Convert.ToBase64String(mCSP.Key);
iv = Convert.ToBase64String(mCSP.IV);
}
#endregion
//公共加密解密方法
#region 公共加密解密方法
/** <summary>
/// 加密方法
/// </summary>
/// <param name="encryptionFormat">加密格式</param>
/// <param name="srcString">欲加密字符串</param>
/// <returns>返回加密后的字符串;若返回null,表示失败</returns>
public static string Encrypt(CryptogramFormat encryptionFormat, string srcString)
{
try
{
switch (encryptionFormat)
{
case CryptogramFormat.MD5:
return FormsAuthentication.HashPasswordForStoringInConfigFile(srcString, "MD5");
case CryptogramFormat.SHA1:
return FormsAuthentication.HashPasswordForStoringInConfigFile(srcString, "SHA1");
case CryptogramFormat.Des:
case CryptogramFormat.Rc2:
case CryptogramFormat.Rijndael:
case CryptogramFormat.TripleDes:
case CryptogramFormat.RSA:
return "缺少参数";
default:
{
return "不支持该格式的加密";
}
}
}
catch (Exception e)
{
return "加密异常:" + e.Message;
}
}
/** <summary>
/// 加密方法
/// </summary>
/// <param name="encryptionFormat">加密格式</param>
/// <param name="srcString">欲加密字符串</param>
/// <param name="key">指定加密的Key</param>
/// <param name="iv">指定加密的IV</param>
/// <returns>返回加密后的字符串;若返回null,表示失败</returns>
public static string Encrypt(CryptogramFormat encryptionFormat, string srcString,string key,string iv)
{
try
{
switch (encryptionFormat)
{
case CryptogramFormat.MD5:
return FormsAuthentication.HashPasswordForStoringInConfigFile(srcString, "MD5");
case CryptogramFormat.SHA1:
return FormsAuthentication.HashPasswordForStoringInConfigFile(srcString, "SHA1");
case CryptogramFormat.Des:
case CryptogramFormat.Rc2:
case CryptogramFormat.Rijndael:
case CryptogramFormat.TripleDes:
return CSPEncode(encryptionFormat, srcString, key, iv);
//case CryptogramFormat.DSA:
// return DSAEncode(encryptionFormat, srcString, key, iv);
case CryptogramFormat.RSA:
return RSAEncode(encryptionFormat, srcString, key, iv);
default:
{
return null;
}
}
}
catch (Exception e)
{
return "加密异常:" + e.Message;
}
}
/** <summary>
/// 解密方法
/// </summary>
/// <param name="encryptionFormat">加密格式</param>
/// <param name="srcString">欲解密字符串</param>
/// <param name="key">指定解密的Key</param>
/// <param name="iv">指定解密的IV</param>
/// <returns>返回解密后的字符串;若返回null,表示失败</returns>
public static string Decrypt(CryptogramFormat encryptionFormat, string srcString, string key, string iv)
{
try
{
switch (encryptionFormat)
{
case CryptogramFormat.Des:
case CryptogramFormat.Rc2:
case CryptogramFormat.Rijndael:
case CryptogramFormat.TripleDes:
return CSPDecode(encryptionFormat, srcString, key, iv);
case CryptogramFormat.RSA:
return RSADecode(encryptionFormat, srcString, key, iv);
default:
{
return "不支持该种解密";
}
}
}
catch(Exception e)
{
return "解密异常:"+e.Message;
}
}
/** <summary>
/// 生成Key和IV
/// </summary>
/// <param name="encryptionFormat">加密格式</param>
/// <param name="key">Key</param>
/// <param name="iv">Iv</param>
/// <returns>返回是否成功生成Key和IV</returns>
public static bool GenerateKeyAndIV(CryptogramFormat encryptionFormat, ref string key, ref string iv)
{
try
{
switch (encryptionFormat)
{
case CryptogramFormat.MD5:
case CryptogramFormat.SHA1:
key = "不支持";
iv = "不支持";
break;
case CryptogramFormat.Des:
case CryptogramFormat.Rc2:
case CryptogramFormat.Rijndael:
case CryptogramFormat.TripleDes:
KeyAndIV(encryptionFormat,ref key,ref iv);
break;
case CryptogramFormat.RSA:
RSACryptoServiceProvider rsaD = new RSACryptoServiceProvider();
key = rsaD.ToXmlString(false);
iv = rsaD.ToXmlString(true);
break;
default:
{
return false;
}
}
}
catch
{
return false;
}
return true;
}
#endregion
//生成随机密码
#region 生成随机密码
/** <summary>
/// 生成随机密码
/// </summary>
/// <returns></returns>
public static string GenerateString()
{
return GenerateString(8);
}
/** <summary>
/// 生成随机密码
/// </summary>
/// <param name="keylength"></param>
/// <returns></returns>
public static string GenerateString(int keylength)
{
try
{
if (keylength <= 1)
return "SmartJavaer";
byte[] buff = new Byte[keylength/2];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(buff);
//return Convert.ToBase64String(buff);
StringBuilder sb = new StringBuilder(keylength);
int i;
for (i = 0; i < buff.Length; i++)
{
sb.Append(String.Format("{0:X2}", buff[i]));
}
return sb.ToString();
}
catch
{
return "SmartJavaer";
}
}
#endregion
}
#endregion
}