WebApi 如何 优雅的 对 输入输出 解密加密
这不是变态的想法, 这只是对现实需求的转化.
因为有密文, 所以本文不适用于浏览器到服务端的数据交换;
只适用于服务端到服务端的数据传输.
用传统的方法对输入输出做加解密, 无非就是在入口处做操作. 但是 WebApi 的参数如果接收的是一串加密字符串, 那基本上等于和 WebApi 强大的模型绑定 Say baybay 了.
要加解密, 又想利用 WebApi 的便利, 有没有什么好的方法呢? 用 ActionFilter ? ModelBinder ?? 好像不能很好的解决(没有仔细的研究).
参考了 Microsoft.AspNet.WebApi.MessageHandlers.Compression 的写法, 我写了个简单的实现..
将返回结果加密
声明 ActionFilter
用以指示后续的处理程序, 哪些Action结果是要密的; 如果需要加密输出, 则在 Response 的 Header 的 ContentType 里加上 encrypt 参数
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class EncryptAttribute : ActionFilterAttribute
{
public bool Ignore { get; set; }
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
{
await base.OnActionExecutedAsync(actionExecutedContext, cancellationToken);
if (!this.Ignore)
{
actionExecutedContext.Response.Content.Headers.ContentType.Parameters.Add(new System.Net.Http.Headers.NameValueHeaderValue("encrypt", ""));
}
}
}
属性: Ignore , 如果值为 true , 则告诉处理程序, 结果不需要加密;
注意 AllowMutltiple 一定是 false, 避免 Controller 和 Action 上的 Filter 交叉.
使用 EncryptAttribute
[Encrypt]
public class TestController : ApiController
{
public Test Get()
{
return new Test()
{
ID = 1,
Name = "xling"
};
}
[Encrypt(Ignore = true)]
public Test Post(Test test)
{
return test;
}
}
派生 DelegatingHandler
重写 SendAsync 方法
public class CryptoHandler : DelegatingHandler
{
...
...
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var response = await base.SendAsync(request, cancellationToken);
return await this.HandleResponse(request, response, cancellationToken);
}
...
...
private async Task<HttpResponseMessage> HandleResponse(HttpRequestMessage request, HttpResponseMessage response, CancellationToken cancellationToken)
{
if (response.Content != null && response.Content.Headers.ContentType.Parameters.Any(p => string.Equals(p.Name, "encrypt", StringComparison.OrdinalIgnoreCase)))
{
var inputBytes = await response.Content.ReadAsByteArrayAsync();
var encryptByte = AesHelper.Encrypt(inputBytes, KEY);
var base64 = Convert.ToBase64String(encryptByte);
var encryptedContent = new StringContent(base64);
encryptedContent.Headers.Clear();
response.Content.Headers.CopyTo(encryptedContent.Headers);
response.Content = encryptedContent;
return response;
}
return response;
}
}
在 HandleResponse 方法里, 首先判断 Response Header 的 ContentType 里是否包含 encrypt 这个参数.
跟据生命周期, 这里的 encrypt 就是上面的 ActionFilter 写进来的.
紧接着就是把返回内容当作字符串加密,并转化为 Base64 格式, 写进 StringContent 里.
然后把原始的 Response Header 覆盖到新的 StringContent 里去.
使用 CryptoHandler
修改 Global 的 Application_Start 方法, 在最后面加上:
GlobalConfiguration.Configuration.MessageHandlers.Insert(0, new CryptoHandler());
看一下输出的 Response Header

解密收到的请求
对CryptoHandler扩展
上面的 CryptoHandler 只对 Response 做了处理. 这里我们要修改 SendAsync 方法, 使它能够将传入的加密数据还原成可以被 WebApi 的 ModelBinder 识别的数据.
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request = await this.HandleRequest(request, cancellationToken);
var response = await base.SendAsync(request, cancellationToken);
return await this.HandleResponse(request, response, cancellationToken);
}
private async Task<HttpRequestMessage> HandleRequest(HttpRequestMessage request, CancellationToken cancellation)
{
if (request.Content?.Headers.ContentType?.Parameters?.Any(y => string.Equals(y.Name, "encrypt", StringComparison.OrdinalIgnoreCase)) == true)
{
var input = await request.Content.ReadAsStringAsync();
var inputBytes = Convert.FromBase64String(input);
var decryptedBytes = AesHelper.Decrypt(inputBytes, KEY);
//var str = Encoding.UTF8.GetString(decryptedBytes);
var stm = new MemoryStream(decryptedBytes);
var decryptedContent = new StreamContent(stm);
request.Content.Headers.CopyTo(decryptedContent.Headers);
request.Content = decryptedContent;
return request;
}
return request;
}
跟加密一样, 解密的第一步也是判断 ContentType 里是否包含参数 encrypt.
接着就是把请求的内容按 string 取出, 并用 base64 解码(因为上一步产生的结果, 我们用 base64 转义了.)
然后对结果解密, 并写入 StreamContent, 替换 request 的 Content.
在运行下去, 就到 Action 里去了.
看一下请求示例


提交数据的时候, 必须告诉 Content-Type , 加密之前是什么格式的, 而且还要带上 encrypt .
上图示例, 我提交的数据在加密之前是 xml 数据.
其它
CryptoHandler.cs 完整代码
using XXX.Common;
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Extensions.Compression.Core.Extensions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace XXX.XXX.XXX
{
public class CryptoHandler : DelegatingHandler
{
private static string KEY = "FF545E10-EDB8-4086-861C-AADFAED015C3";
public static void Init(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(key);
KEY = key;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request = await this.HandleRequest(request, cancellationToken);
var response = await base.SendAsync(request, cancellationToken);
return await this.HandleResponse(request, response, cancellationToken);
}
private async Task<HttpRequestMessage> HandleRequest(HttpRequestMessage request, CancellationToken cancellation)
{
if (request.Content?.Headers.ContentType?.Parameters?.Any(y => string.Equals(y.Name, "encrypt", StringComparison.OrdinalIgnoreCase)) == true)
{
var input = await request.Content.ReadAsStringAsync();
var inputBytes = Convert.FromBase64String(input);
var decryptedBytes = AesHelper.Decrypt(inputBytes, KEY);
//var str = Encoding.UTF8.GetString(decryptedBytes);
var stm = new MemoryStream(decryptedBytes);
var decryptedContent = new StreamContent(stm);
request.Content.Headers.CopyTo(decryptedContent.Headers);
request.Content = decryptedContent;
return request;
}
return request;
}
private async Task<HttpResponseMessage> HandleResponse(HttpRequestMessage request, HttpResponseMessage response, CancellationToken cancellationToken)
{
if (response.Content != null && response.Content.Headers.ContentType.Parameters.Any(p => string.Equals(p.Name, "encrypt", StringComparison.OrdinalIgnoreCase)))
{
var inputBytes = await response.Content.ReadAsByteArrayAsync();
var encryptByte = AesHelper.Encrypt(inputBytes, KEY);
var base64 = Convert.ToBase64String(encryptByte);
var encryptedContent = new StringContent(base64);
encryptedContent.Headers.Clear();
response.Content.Headers.CopyTo(encryptedContent.Headers);
response.Content = encryptedContent;
return response;
}
return response;
}
}
}
WebApi 如何 优雅的 对 输入输出 解密加密的更多相关文章
- asar 如何解密加密?electron 的 asar 的具体用法
来源:https://newsn.net/say/electron-asar.html 在electron中,asar是个特殊的代码格式.asar包里面包含了程序猿编写的代码逻辑.默认情况下,这些代码 ...
- wireshark 解密加密报文
wireshark 解密IPSec加密后的报文 序言 wireshark作为一款非常优秀的抓包工具,支持了各种各样的网络协议,成为了网络开发中必不可少的工具之一.一般而言,对于普通的网络数据包,wir ...
- c# Base64解密加密
private static string base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ...
- AES加密解密 加密解密使用
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Sec ...
- javascript 实现des解密加密
//Paul Tero, July 2001 //http://www.tero.co.uk/des/ // //Optimised for performance with large blocks ...
- flask 第五章 WebSocket GeventWebsocket 单聊群聊 握手 解密 加密
1.WebSocket 首先我们来回顾一下,我们之前用socket学习过的项目有: 1.django 2.flask 3.FTP - 文件服务 HTTP - TCP (特点): 1.一次请求,一次响应 ...
- sqlserver解密加密的存储过程(图解)
来自博客:http://www.cnblogs.com/wghao/archive/2012/12/30/2837642.html
- JWT加密解密
如何保证WebAPI的安全?1.JWT加密解密.token2.使用https传输协议.3.把用户所有请求的参数信息加上一个只有服务器端知道的secret,做个散列运算,然后到了服务器端,服务器端也做一 ...
- KRPano资源分析工具使用说明(KRPano XML/JS解密 切片图批量下载 球面图还原 加密混淆JS还原美化)
软件交流群:571171251(软件免费版本在群内提供) krpano技术交流群:551278936(软件免费版本在群内提供) 最新博客地址:blog.turenlong.com 限时下载地址:htt ...
随机推荐
- CentOS 搭建dns服务器 解析任意域名
DNS服务器IP地址:192.168.1.219 服务器版本:centos6.6一:软件安装 [root@localhost ~]# yum -y install bind* 二:修改主配置文件 [r ...
- Windows 7旗舰版产品密钥
http://www.xitongcheng.com/jiaocheng/win7_article_45903.html 一.Windows 7旗舰版产品密钥 零售版: Windows 7 Ultim ...
- There was an unexpected error (type=Method Not Allowed, status=405). Request method 'POST' not supported
背景:点击提交按钮ajax请求接口时,报出错误[ Whitelabel Error Page This application has no explicit mapping for /error, ...
- add a characteristic in enovia PLM
Problem: add a new Char. name D_COI6 that the description is Injected coloration #7 (COI6) in the D_ ...
- js 实现图片懒加载
搬运自其他大神,因为找不到链接了就没放,找到了补上. 个人情况:页面超过一屏,下方是大量图片数据(后台传来的html数据) ,想做到一开始不加载下方图片,滚动到进入可视区再加载图片. html:(需先 ...
- 开启SSH 使用SSH登录工具连接虚拟机
修改sshd_config文件,命令为:vi /etc/ssh/sshd_config将#PermitRootLogin without-password注释去掉修改为PermitRootLogin ...
- pycharm 2020 激活码 破解教程
以下是安装完pycharm后进行破解!! 1.修改hosts,在hosts文件最后添加2行“0.0.0.0 account.jetbrains.com”和“0.0.0.0 www.jetbrain ...
- leetcood学习笔记-204-计算质数
题目描述: 第一次提交;(超时): class Solution: def countPrimes(self, n: int) -> int: count = 0 for i in range( ...
- js基础用法 ,基础语法
js用法: HTML 中的脚本必须位于 <script> 与 </script> 标签之间. 脚本可被放置在 HTML 页面的 <body> 和 <head& ...
- 阿里云重磅推出物联网安全运营中心Link SOC
阿里云IoT自主研发了新一代物联网安全平台Link Security,面向IoT设备全生命周期构建了一整套全链路多层次的安全防御体系,IoT物联网平台的业务在不同层面可以按需集成安全能力. 1.首先在 ...