using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace GummingCommon
{
public class Encryption
{
///
/// 使用缺省密钥字符串加密
///
/// 明文
/// 密文
public static string Encrypt(string original)
{
return Encrypt(original, "KennethYao");
}
///
/// 使用缺省密钥解密
///
/// 密文
/// 明文
public static string Decrypt(string original)
{
return Decrypt(original, "KennethYao", System.Text.Encoding.Default);
}
///
/// 使用给定密钥解密
///
/// 密文
/// 密钥
/// 明文
public static string Decrypt(string original, string key)
{
return Decrypt(original, key, System.Text.Encoding.Default);
}
///
/// 使用缺省密钥解密,返回指定编码方式明文
///
/// 密文
/// 编码方式
/// 明文
public static string Decrypt(string original, System.Text.Encoding encoding)
{
return Decrypt(original, "KennethYao", encoding);
}
///
/// 使用给定密钥加密
///
/// 原始文字
/// 密钥
/// 字符编码方案
/// 密文
public static string Encrypt(string original, string key)
{
byte[] buff = System.Text.Encoding.Default.GetBytes(original);
byte[] kb = System.Text.Encoding.Default.GetBytes(key);
return Convert.ToBase64String(Encrypt(buff, kb));
}
///
/// 使用给定密钥解密
///
/// 密文
/// 密钥
/// 字符编码方案
/// 明文
public static string Decrypt(string encrypted, string key, System.Text.Encoding encoding)
{
try
{
byte[] buff = Convert.FromBase64String(encrypted);
byte[] kb = System.Text.Encoding.Default.GetBytes(key);
return encoding.GetString(Decrypt(buff, kb));
}
catch
{
return string.Empty;
}
}
///
/// 生成MD5摘要
///
/// 数据源
/// 摘要
public static byte[] MakeMD5(byte[] original)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
byte[] keyhash = hashmd5.ComputeHash(original);
hashmd5 = null;
return keyhash;
}
///
/// 使用给定密钥加密
///
/// 明文
/// 密钥
/// 密文
public static byte[] Encrypt(byte[] original, byte[] key)
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = MakeMD5(key);
des.Mode = CipherMode.ECB;
return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
}
///
/// 使用给定密钥解密数据
///
/// 密文
/// 密钥
/// 明文
public static byte[] Decrypt(byte[] encrypted, byte[] key)
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = MakeMD5(key);
des.Mode = CipherMode.ECB;
return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
}
///
/// 使用给定密钥加密
///
/// 原始数据
/// 密钥
/// 密文
public static byte[] Encrypt(byte[] original)
{
byte[] key = System.Text.Encoding.Default.GetBytes("KennethYao");
return Encrypt(original, key);
}
///
/// 使用缺省密钥解密数据
///
/// 密文
/// 密钥
/// 明文
public static byte[] Decrypt(byte[] encrypted)
{
byte[] key = System.Text.Encoding.Default.GetBytes("KennethYao");
return Decrypt(encrypted, key);
}
public static string GetMD5(string encypStr, string charset)
{
string retStr;
MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider();
//创建md5对象
byte[] inputBye;
byte[] outputBye;
//使用GB2312编码方式把字符串转化为字节数组.
try
{
inputBye = Encoding.GetEncoding(charset).GetBytes(encypStr);
}
catch (Exception ex)
{
inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr);
}
outputBye = m5.ComputeHash(inputBye);
retStr = System.BitConverter.ToString(outputBye);
retStr = retStr.Replace("-", "").ToUpper();
return retStr;
}
}
}