wsd_djy/WebSiteCode/Cmdjy/Cmdjy/Bll/DesHelper.cs

144 lines
5.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web.Mvc;
using Cmdjy.Dal;
using System.Linq;
namespace Cmdjy.Bll
{
/// <summary>
/// 标记Action需要对输出进行加密
/// </summary>
[AttributeUsage(AttributeTargets.Method,AllowMultiple = false,Inherited = false)]
[Obsolete("未完成",true)]
public class DesAttribute:ActionFilterAttribute, IResultFilter
{
public Lazy<DjyDbContext> Db { get; set; }
// var acceptEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
// if(string.IsNullOrEmpty(acceptEncoding)) return;
// var response = filterContext.HttpContext.Response;
// if(response.Filter == null) return;
// acceptEncoding = acceptEncoding.ToLower();
// if(acceptEncoding.Contains("gzip")) {
// response.AppendHeader("Content-encoding", "gzip");
// response.Filter = new GZipStream(response.Filter,CompressionMode.Compress);
// }
// else if(acceptEncoding.Contains("deflate")) {
// response.AppendHeader("Content-encoding", "deflate");
// response.Filter = new DeflateStream(response.Filter,CompressionMode.Compress);
//}
public override void OnResultExecuting(ResultExecutingContext filterContext) {
//var disableDes = filterContext.HttpContext.Request.Form.HasKey ?? false;
var request = filterContext.HttpContext.Request;
var db = this.Db.Value;
var companyCode = request["CompanyCode"];
var icompanycode = 0;
if(!int.TryParse(companyCode,out var iicc)) {
}
var conpanyQu = db.CompanyInfos.Where(m => m.Id == iicc);
var response = filterContext.HttpContext.Response;
base.OnResultExecuting(filterContext);
}
public override void OnResultExecuted(ResultExecutedContext filterContext) {
base.OnResultExecuted(filterContext);
}
}
/// <summary>
/// 加密帮助类
/// </summary>
public class DesHelper
{
/// <summary>
/// 补位填充方式
/// </summary>
public PaddingMode Padding { get; set; }
/// <summary>
/// 加密算法模式
/// </summary>
public CipherMode Cipher { get; set; }
/// <summary>
/// 初始化向量
/// </summary>
public byte[] IV { get; set; }
/// <summary>
/// 加密字符串获取Base64密文
/// </summary>
/// <param name="str">要加密的字符串</param>
public string Encrypty(string key,string str) {
if(key.Length < 8) {
throw new Exception("加密用的key长度为8位");
}
byte[] bKey = Encoding.UTF8.GetBytes(key.Substring(0,8));
byte[] bStr = Encoding.UTF8.GetBytes(str);
var desc = createProvider();
using(var mStream = new MemoryStream()) {
using(var cStream = new CryptoStream(mStream,desc.CreateEncryptor(bKey,IV),CryptoStreamMode.Write)) {
cStream.Write(bStr,0,bStr.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
}
}
/// <summary>
/// 从Base64密文解密获取字符串
/// </summary>
/// <param name="str">密文</param>
public string DesCrypty(string key,string str) {
if(key.Length < 8) {
throw new Exception("加密用的key长度为8位");
}
var bStr = Convert.FromBase64String(str);
byte[] bKey = Encoding.UTF8.GetBytes(key.Substring(0,8));
var desc = createProvider();
using(var mStream = new MemoryStream()) {
using(var cStream = new CryptoStream(mStream,desc.CreateDecryptor(bKey,IV),CryptoStreamMode.Write)) {
cStream.Write(bStr,0,bStr.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
}
}
private DESCryptoServiceProvider createProvider() {
var desc = new DESCryptoServiceProvider();
desc.Padding = this.Padding;
desc.Mode = this.Cipher;
return desc;
}
private static DesHelper _deshelper { get; set; }
private DesHelper() { }
/// <summary>
/// 重用模式获取默认加密帮助器
/// </summary>
public static DesHelper GetHelper() {
if(_deshelper == null) {
byte[] IV = { 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08 };
_deshelper = GetHelper(PaddingMode.PKCS7,CipherMode.CBC,IV);
}
return _deshelper;
}
/// <summary>
/// 生成新的加密帮助器
/// </summary>
/// <param name="padding">补位方式</param>
/// <param name="cipher">加密算法模式</param>
/// <param name="IV">初始化向量</param>
/// <param name="key">加密关键字</param>
public static DesHelper GetHelper(PaddingMode padding,CipherMode cipher,byte[] IV) {
return new DesHelper {
Cipher = cipher,IV = IV,Padding = padding,
};
}
}
}