using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using CYQ.Data;
namespace CYQ.Data.Tool
{
///
/// 密码加密类【含Hash算法】
///
public static class EncryptHelper
{
#region 对外公开的两个Hash接口(和内部的加密没啥关系)
///
/// 【用于分布式】根据字符串获取相应的HashKey
///
/// 字符串
///
public static string GetHashKey(string key)
{
return StaticTool.GetHashKey(key);
}
///
/// 【用于分布式】根据字符串获取相应的HashCode
///
/// 字符串
///
public static uint GetHashCode(string key)
{
return HashCreator.CreateCode(key);
}
#endregion
internal static byte[] GetHash(string key)
{
using (MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider())
{
return hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
}
}
private static byte[] _DefaultHashKey;
internal static byte[] DefaultHashKey
{
get
{
if (_DefaultHashKey == null)
{
_DefaultHashKey = GetHash("!1@2#3$4%5^6");
}
return _DefaultHashKey;
}
}
/////
///// 预留的二次加密
/////
//internal static string EncryptKey
//{
// get
// {
// return AppConfig.GetApp("EncryptKey", "");
// }
//}
#region
///
/// 加密
///
/// 加密的内容
///
public static string Encrypt(string text)
{
return Encrypt(text, "");
}
///
/// 加密
///
/// 加密的内容
/// 指定加密的key
public static string Encrypt(string text, string key)
{
string result = Encrypt(text, DefaultHashKey);
if (string.IsNullOrEmpty(key))
{
key = AppConfig.Tool.EncryptKey;
}
if (!string.IsNullOrEmpty(key))
{
result = Encrypt(result, GetHash(key)) + "$2";//设置二级加密标识
}
return result;
}
///
/// 3des加密字符串
///
/// 要加密的字符串
/// 密钥
/// 加密后并经base64编码的字符串
/// 静态方法,采用默认ascii编码
private static string Encrypt(string text, byte[] hashKey)
{
string result = string.Empty;
if (!string.IsNullOrEmpty(text))
{
using (TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider())
{
DES.Key = hashKey;
DES.Mode = CipherMode.ECB;
ICryptoTransform DESEncrypt = DES.CreateEncryptor();
byte[] Buffer = ASCIIEncoding.UTF8.GetBytes(text);
string pass = Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
result = pass.Replace('=', '$').Replace("+", "-").Replace("/", "_");
}
}
return result;
}//end method
///
/// 解密
///
/// 要解密的字符串
/// 解密后的字符串
public static string Decrypt(string text)
{
return Decrypt(text, "");
}
/// 指定加密的key
public static string Decrypt(string text, string key)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}
else
{
text = text.Trim().Replace(' ', '+');//处理Request的+号变空格问题。
if (string.IsNullOrEmpty(key))
{
key = AppConfig.GetApp("EncryptKey", "");
}
if (!string.IsNullOrEmpty(key) && (text.EndsWith("=2") || text.EndsWith("$2")))
{
text = Decrypt(text.Substring(0, text.Length - 2), GetHash(key));//先解一次Key
}
return Decrypt(text, DefaultHashKey);
}
}
///
/// 3des解密字符串
///
/// 要解密的字符串
/// 密钥
/// 解密后的字符串
/// 密钥错误
/// 静态方法,采用默认ascii编码
private static string Decrypt(string text, byte[] hashKey)
{
string result = "";
text = text.Replace('#', '=').Replace("$", "=").Replace("-", "+").Replace("_", "/");
if (text.Length % 4 == 0)
{
using (TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider())
{
DES.Key = hashKey;
DES.Mode = CipherMode.ECB;
ICryptoTransform DESDecrypt = DES.CreateDecryptor();
try
{
byte[] Buffer = Convert.FromBase64String(text);
result = ASCIIEncoding.UTF8.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}
catch
{
return string.Empty;
}
}
}
return result;
}
internal static bool HashKeyIsValid()
{
string acKey = Decrypt(AppConst.ACKey, AppConst.Host);
acKey = AppConfig.GetConn(acKey);
if (!string.IsNullOrEmpty(acKey))
{
string alKey = Decrypt(AppConst.ALKey, AppConst.Host);
if (!string.IsNullOrEmpty(alKey))
{
string code = AppConfig.GetApp(alKey);
if (!string.IsNullOrEmpty(code))
{
string[] items = Decrypt(code.Substring(4), code.Substring(0, 4)).Split(',');
DateTime d;
if ((DateTime.TryParse(items[0], out d) && d > DateTime.Now) || (items.Length > 1 && items[1] == AppConst.HNKey))
{
AppConfig.SetApp(alKey + AppConst.Result, "1");
return true;
}
}
}
AppConfig.SetApp(alKey + AppConst.Result, "0");
return false;
}
return true;
}
#endregion
}
}