|
发表于 2014-5-28 09:03:39
|
显示全部楼层
using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Web;
using System.Security.Cryptography;
using System.Text;
namespace Common
{
/// <summary>
/// DES 的摘要说明
/// </summary>
public class DES
{
DESCryptoServiceProvider des;
static string desDefaultKey, desDefaultIV;
#region 构造方法。初始化 DES 新实例
/// <summary>
/// 初始化 DES 新实例
/// </summary>
public DES()
{
this.des = new DESCryptoServiceProvider();
}
/// <summary>
/// 初始化 DES 新实例
/// </summary>
/// <param name="key">密钥</param>
/// <param name="iv">向量</param>
public DES(string key, string iv)
: this()
{
this.Key = key;
this.IV = iv;
}
#endregion
#region Key、IV 属性。获取或设置数据加密的 Key 和 IV
/// <summary>
/// 获取或设置数据加密标准 (System.Security.Cryptography.DES) 算法的机密密钥
/// </summary>
public string Key
{
get { return System.Text.Encoding.Default.GetString(this.des.Key); }
set
{
if (value == null)
value = "";
byte[] buffer = System.Text.Encoding.Default.GetBytes(value);
byte[] keybuffer = new byte[8];
Array.Copy(buffer, keybuffer, buffer.Length > 8 ? 8 : buffer.Length);
this.des.Key = keybuffer;
}
}
/// <summary>
/// 获取或设置对称算法的初始化向量 (System.Security.Cryptography.SymmetricAlgorithm.IV)
/// </summary>
public string IV
{
get { return System.Text.Encoding.Default.GetString(this.des.IV); }
set
{
if (value == null)
value = "";
byte[] buffer = System.Text.Encoding.Default.GetBytes(value);
byte[] ivBuffer = new byte[8];
Array.Copy(buffer, ivBuffer, buffer.Length > 8 ? 8 : buffer.Length);
this.des.IV = ivBuffer;
}
}
#endregion
#region Encrypt、Decrypt 方法。加解密字符串
/// <summary>
/// 加密字符串
/// </summary>
/// <param name="input">待加密的源字符串</param>
/// <returns>加密后的结果字符串</returns>
public string Encrypt(string input)
{
MemoryStream ms = new MemoryStream();
ICryptoTransform transform = this.des.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write);
byte[] buffer = System.Text.Encoding.Default.GetBytes(input);
cs.Write(buffer, 0, buffer.Length);
cs.FlushFinalBlock();
byte[] result = ms.ToArray();
return Convert.ToBase64String(result);
}
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="input">待解密字符串</param>
/// <returns>解密后的源字符串</returns>
public string Decrypt(string input)
{
MemoryStream ms = new MemoryStream();
ICryptoTransform transform = this.des.CreateDecryptor();
try
{
byte[] buffer = Convert.FromBase64String(input);
CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write);
cs.Write(buffer, 0, buffer.Length);
cs.FlushFinalBlock();
}
catch (Exception ex)
{
Log.LogHelper.Error("DES", "Descrypt", ex.Message);
return string.Empty;
}
byte[] result = ms.ToArray();
return System.Text.Encoding.Default.GetString(result);
}
#endregion
public static string DesDefaultKey
{
get
{
if (DES.desDefaultKey == null)
DES.desDefaultKey = "tf2014.04.14pwd";
return DES.desDefaultKey;
}
}
public static string DesDefaultIV
{
get
{
if (DES.desDefaultIV == null)
DES.desDefaultIV = "tf2014.04.14exp";
return DES.desDefaultIV;
}
}
public static string DesEncrypt(string input)
{
DES des = new DES(DES.DesDefaultKey, DES.DesDefaultIV);
return des.Encrypt(input);
}
public static string DesDecrypt(string input)
{
DES des = new DES(DES.DesDefaultKey, DES.DesDefaultIV);
return des.Decrypt(input);
}
}
public class RSA
{
/// <summary>
/// RSA加密
/// </summary>
/// <param name="publickey"></param>
/// <param name="content"></param>
/// <returns></returns>
public static string RSAEncrypt(string publickey, string content)
{
publickey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromXmlString(publickey);
cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
return Convert.ToBase64String(cipherbytes);
}
/// <summary>
/// RSA解密
/// </summary>
/// <param name="privatekey"></param>
/// <param name="content"></param>
/// <returns></returns>
public static string RSADecrypt(string privatekey, string content)
{
privatekey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent><P>/hf2dnK7rNfl3lbqghWcpFdu778hUpIEBixCDL5WiBtpkZdpSw90aERmHJYaW2RGvGRi6zSftLh00KHsPcNUMw==</P><Q>6Cn/jOLrPapDTEp1Fkq+uz++1Do0eeX7HYqi9rY29CqShzCeI7LEYOoSwYuAJ3xA/DuCdQENPSoJ9KFbO4Wsow==</Q><DP>ga1rHIJro8e/yhxjrKYo/nqc5ICQGhrpMNlPkD9n3CjZVPOISkWF7FzUHEzDANeJfkZhcZa21z24aG3rKo5Qnw==</DP><DQ>MNGsCB8rYlMsRZ2ek2pyQwO7h/sZT8y5ilO9wu08Dwnot/7UMiOEQfDWstY3w5XQQHnvC9WFyCfP4h4QBissyw==</DQ><InverseQ>EG02S7SADhH1EVT9DD0Z62Y0uY7gIYvxX/uq+IzKSCwB8M2G7Qv9xgZQaQlLpCaeKbux3Y59hHM+KpamGL19Kg==</InverseQ><D>vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZaa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30=</D></RSAKeyValue>";
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromXmlString(privatekey);
cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);
return Encoding.UTF8.GetString(cipherbytes);
}
}
/// <summary>
/// MD5 的摘要说明
/// </summary>
public class MD5
{
public static string Compute(string value)
{
// 创建 MD5 加密提供程序实例
MD5CryptoServiceProvider md5 = (MD5CryptoServiceProvider)MD5CryptoServiceProvider.Create();
byte[] buffer = Encoding.Default.GetBytes(value); // 将字符串转换成二进制数组(byte 数组,通常称作为二进制数组)
byte[] result = md5.ComputeHash(buffer); // 生成 MD5 哈希代码(也是二进制数组)。
// 将生成的 MD5 哈希代码,转换为字符串显示
string hash = BitConverter.ToString(result).Replace("-", string.Empty);
return hash;
}
public static string Compute(string value, MD5Type md5Typ)
{
System.Security.Cryptography.MD5CryptoServiceProvider MD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] b = MD5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(value));
System.Text.StringBuilder StrB = new System.Text.StringBuilder();
for (int i = 0; i < b.Length; i++)
StrB.Append(b[i].ToString("x").PadLeft(2, '0'));
if (md5Typ == MD5Type.MD5_16)
return StrB.ToString(8, 16);
else
return StrB.ToString();
}
}
public enum MD5Type
{
//16位MD5码
MD5_16 = 16,
//32位MD5码
MD5_32 = 32
}
} |
|