WebApi实现通讯加密
一. 场景介绍:
如题如何有效的,最少量的现有代码侵入从而实现客户端与服务器之间的数据交换加密呢?
二. 探究:
1.需求分析
webapi服务端 有如下接口:
public class ApiTestController : ApiController
{ // GET api/<controller>/5
public object Get(int id)
{
return "value" + id;
}
}
ApiTestController
无加密请求
GET /api/apitest?id=10
2.功能分析

//
// 摘要:
// A base type for handlers which only do some small processing of request and/or
// response messages.
public abstract class MessageProcessingHandler : DelegatingHandler
{
//
// 摘要:
// Creates an instance of a System.Net.Http.MessageProcessingHandler class.
protected MessageProcessingHandler();
//
// 摘要:
// Creates an instance of a System.Net.Http.MessageProcessingHandler class with
// a specific inner handler.
//
// 参数:
// innerHandler:
// The inner handler which is responsible for processing the HTTP response messages.
protected MessageProcessingHandler(HttpMessageHandler innerHandler); //
// 摘要:
// Performs processing on each request sent to the server.
//
// 参数:
// request:
// The HTTP request message to process.
//
// cancellationToken:
// A cancellation token that can be used by other objects or threads to receive
// notice of cancellation.
//
// 返回结果:
// Returns System.Net.Http.HttpRequestMessage.The HTTP request message that was
// processed.
protected abstract HttpRequestMessage ProcessRequest(HttpRequestMessage request, CancellationToken cancellationToken);
//
// 摘要:
// Perform processing on each response from the server.
//
// 参数:
// response:
// The HTTP response message to process.
//
// cancellationToken:
// A cancellation token that can be used by other objects or threads to receive
// notice of cancellation.
//
// 返回结果:
// Returns System.Net.Http.HttpResponseMessage.The HTTP response message that was
// processed.
protected abstract HttpResponseMessage ProcessResponse(HttpResponseMessage response, CancellationToken cancellationToken);
//
// 摘要:
// Sends an HTTP request to the inner handler to send to the server as an asynchronous
// operation.
//
// 参数:
// request:
// The HTTP request message to send to the server.
//
// cancellationToken:
// A cancellation token that can be used by other objects or threads to receive
// notice of cancellation.
//
// 返回结果:
// Returns System.Threading.Tasks.Task`1.The task object representing the asynchronous
// operation.
//
// 异常:
// T:System.ArgumentNullException:
// The request was null.
protected internal sealed override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken);
}
MessageProcessingHandler
三. 实践:
/// <summary>
/// 加密解密接口
/// </summary>
public interface IMessageEnCryption
{
/// <summary>
/// 加密
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
string Encode(string content);
/// <summary>
/// 解密
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
string Decode(string content);
}
IMessageEnCryption
编写版本1.0 base64加密解密
/// <summary>
/// 加解密 只做 base64
/// </summary>
public class MessageEncryptionVersion1_0 : IMessageEnCryption
{
public string Decode(string content)
{
return content?.DecryptBase64();
} public string Encode(string content)
{
return content.EncryptBase64();
}
}
MessageEncryptionVersion1_0
编写版本1.1 des加密解密
/// <summary>
/// 数据加解密 des
/// </summary>
public class MessageEncryptionVersion1_1 : IMessageEnCryption
{
public static readonly string KEY = "fHil/4]0";
public string Decode(string content)
{
return content.DecryptDES(KEY);
} public string Encode(string content)
{
return content.EncryptDES(KEY);
}
}
MessageEncryptionVersion1_1
附上加密解密的基本的一个封装类
public static class EncrypExtends
{ //默认密钥向量
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
internal static string Key = "*@&$(@#H"; //// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public static string EncryptDES(this string encryptString, string encryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(, ));
byte[] rgbIV = Keys;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, , inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
catch
{
return encryptString;
}
}
//// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public static string DecryptDES(this string decryptString, string key)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(key.Substring(, ));
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, , inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return decryptString;
}
}
public static string EncryptBase64(this string encryptString)
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(encryptString));
}
public static string DecryptBase64(this string encryptString)
{
return Encoding.UTF8.GetString(Convert.FromBase64String(encryptString));
}
public static string DecodeUrl(this string cryptString)
{
return System.Web.HttpUtility.UrlDecode(cryptString);
}
public static string EncodeUrl(this string cryptString)
{
return System.Web.HttpUtility.UrlEncode(cryptString);
}
}
EncrypExtends
OK! 到此我们前题工作已经完成了80%,开始进行HTTP请求的 消息进和出的加密解密功能的实现.
我们暂时将加密的版本信息定义为 HTTP header头中 以 api_version 的value 来判别分别是用何种方式加密解密
header例:
api_version: 1.0
api_version: 1.1
/// <summary>
/// API消息请求处理
/// </summary>
public class JoyMessageHandler : MessageProcessingHandler
{ /// <summary>
/// 接收到request时 处理
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
protected override HttpRequestMessage ProcessRequest(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.Content.IsMimeMultipartContent())
return request;
// 获取请求头中 api_version版本号
var ver = System.Web.HttpContext.Current.Request.Headers.GetValues("api_version")?.FirstOrDefault();
// 根据api_version版本号获取加密对象, 如果为null 则不需要加密
var encrypt = MessageEncryptionCreator.GetInstance(ver); if (encrypt != null)
{
// 读取请求body中的数据
string baseContent = request.Content.ReadAsStringAsync().Result;
// 获取加密的信息
// 兼容 body: 加密数据 和 body: code=加密数据
baseContent = baseContent.Match("(code=)*(?<code>[\\S]+)", );
// URL解码数据
baseContent = baseContent.DecodeUrl();
// 用加密对象解密数据
baseContent = encrypt.Decode(baseContent); string baseQuery = string.Empty;
if (!request.RequestUri.Query.IsNullOrEmpty())
{
// 同 body
// 读取请求 url query数据
baseQuery = request.RequestUri.Query.Substring();
baseQuery = baseQuery.Match("(code=)*(?<code>[\\S]+)", );
baseQuery = baseQuery.DecodeUrl();
baseQuery = encrypt.Decode(baseQuery);
}
// 将解密后的 URL 重置URL请求
request.RequestUri = new Uri($"{request.RequestUri.AbsoluteUri.Split('?')[0]}?{baseQuery}");
// 将解密后的BODY数据 重置
request.Content = new StringContent(baseContent);
} return request;
} /// <summary>
/// 处理将要向客户端response时
/// </summary>
/// <param name="response"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
protected override HttpResponseMessage ProcessResponse(HttpResponseMessage response, CancellationToken cancellationToken)
{
//var isMediaType = response.Content.Headers.ContentType.MediaType.Equals(mediaTypeName, StringComparison.OrdinalIgnoreCase);
var ver = System.Web.HttpContext.Current.Request.Headers.GetValues("api_version")?.FirstOrDefault();
var encrypt = MessageEncryptionCreator.GetInstance(ver);
if (encrypt != null)
{
if (response.StatusCode == HttpStatusCode.OK)
{
var result = response.Content.ReadAsStringAsync().Result;
// 返回消息 进行加密
var encodeResult = encrypt.Encode(result);
response.Content = new StringContent(encodeResult);
}
} return response;
} }
JoyMessageHandler
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API 配置和服务
// 将 Web API 配置为仅使用不记名令牌身份验证。
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); // Web API 路由
config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
); // 添加自定义消息处理
config.MessageHandlers.Add(new JoyMessageHandler()); }
}
WebApiConfig
[TestMethod()]
public void GetTest()
{
var id = ;
var resultSuccess = $"\"value{id}\"";
//不加密
Trace.WriteLine($"without encryption.");
var url = $"api/ApiTest?id={id}";
Trace.WriteLine($"get url : {url}");
var response = http.GetAsync(url).Result;
var result = response.Content.ReadAsStringAsync().Result;
Assert.AreEqual(result, resultSuccess);
Trace.WriteLine($"result : {result}"); //使用 方案1加密
Trace.WriteLine($"encryption case one."); url = $"api/ApiTest?code=" + $"id={id}".EncryptBase64().EncodeUrl(); Trace.WriteLine($"get url : {url}"); http.DefaultRequestHeaders.Clear();
http.DefaultRequestHeaders.Add("api_version", "1.0");
response = http.GetAsync(url).Result; result = response.Content.ReadAsStringAsync().Result; Trace.WriteLine($"result : {result}"); result = result.DecryptBase64(); Trace.WriteLine($"DecryptBase64 : {result}"); Assert.AreEqual(result, resultSuccess); //使用 方案2 加密通讯
Trace.WriteLine($"encryption case one."); url = $"api/ApiTest?code=" + $"id={id}".EncryptDES(MessageEncryptionVersion1_1.KEY).EncodeUrl(); Trace.WriteLine($"get url : {url}"); http.DefaultRequestHeaders.Clear();
http.DefaultRequestHeaders.Add("api_version", "1.1");
response = http.GetAsync(url).Result; result = response.Content.ReadAsStringAsync().Result; Trace.WriteLine($"result : {result}"); result = result.DecryptDES(MessageEncryptionVersion1_1.KEY); Trace.WriteLine($"DecryptBase64 : {result}"); Assert.AreEqual(result, resultSuccess);
}
ApiTestControllerTests
四.思想延伸
WebApi实现通讯加密的更多相关文章
- WebApi实现通讯加密 (转)
http://www.cnblogs.com/jonneydong/p/WebApi_Encryption.html 一. 场景介绍: 如题如何有效的,最少量的现有代码侵入从而实现客户端与服务器之间的 ...
- WebApi服务Uri加密及验证的两种方式
最近的一个项目要求服务端与UI层分离,业务层以WebApi方式向外提供所有业务服务,服务在数据保密性方面提出了要求,主要包括: 1:客户端认证: 2:服务请求超时(默认5分钟): 3:服务Get请求的 ...
- ECC(Ellipse Curve Cryptography)+AES(Advanced Encryption Standard)前端通讯加密模拟(使用eccrypto-js)
前置知识 不了解对称加密与非对称加密的小伙伴可以看看下面的文章,想详细学习与区块链有关的加密算法可以戳这里 对称与非对称加密 https://blog.csdn.net/u013320868/arti ...
- Self Host WebApi服务传输层SSL加密(服务器端+客户端调用)
接上篇<WebApi服务URI加密及验证的两种方式>,在实际开发中,仅对URI进行加密是不够的,在传输层采用SSL加密也是必须的. 如果服务寄宿于IIS,那对传输层加密非常简单仅需要配置一 ...
- SQLite学习笔记(十)&&加密
随着移动互联网的发展,手机使用越来越广泛,sqlite作为手机端存储的一种解决方案,使用也非常普遍.但是sqlite本身安全特性却比较弱,比如不支持用户权限,只要能获取到数据库文件就能进行访问:另外也 ...
- .NET中的DES对称加密
DES是一种对称加密(Data Encryption Standard)算法,于1977年得到美国政府的正式许可,是一种用56位密钥来加密64位数据的方法.一般密码长度为8个字节,其中56位加密密钥, ...
- SQLite XXTea加密学习
这几天优化数据库读写,移植了xxtea加密到最新的数据库sqlite 3.12.2里,一些好文章放在这里.移植后,数据库读写性能异常优秀! 这几天又发现,数据库还是发生了无法写入情况,数据库崩溃掉了. ...
- 单片机上使用TEA加密通信(转)
源:单片机上使用TEA加密通信 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN7 开发环境:MDK4.72 单片机:STM32 说 ...
- Nginx集群之SSL证书的WebApi微服务
目录 1 大概思路... 1 2 Nginx集群之SSL证书的WebApi微服务... 1 3 HTTP与HTTPS(SSL协议)... 1 4 Ope ...
随机推荐
- Unity中使用扩展方法解决foreach导致的GC
对于List这种顺序表,我们解决的时候还是可以使用for代替foreach即可.但是对于非顺序表,比如Dictionary或者Set之类,我们可以扩展方法Foreach,ForeachKey和Fore ...
- [Angular Tutorial] 11 -Custom Filters
在这一步中您将学到如何创建您自己的展示过滤器. ·在先前的步骤中,细节页面展示“true”或“false”来显示某部电话是否有某项功能.在这一步中,我们将使用自定义的过滤器来将这些个字符串转化成符号: ...
- 安装oracle后不能连接问题
在安装oracle之前,还特意找了几篇博客和百度经验看过,因为知道oracle的只能安装一次,如果装不上,OK,基本排除安装的可能了(安装的残留文件卸不完) 然而,我的问题没有出在这里,安装很顺利,一 ...
- SQL Server 2005入门到精通(案例详解)
SQL Server 2005基础应用 一.数据库的基本操作 --创建数据库 create database new_db2 on primary ( name='new.mdf', filena ...
- jQuery中animate动画第二次点击事件没反应
jQuery中animate动画第二次点击事件没反应 用animate做点击翻页动画时发现第二次点击事件动画没反应,而第一次点击有动画效果,代码如下: 复制代码 代码如下: $(".page ...
- 第四组UI组件:AdapterView及子类
AdapterView组件是一组重要的组件,AdapterView本省是一个抽象基类,它派生的子类在用法上十分相似,只是显示界面与一定的区别,因此这次针对它们的共性集中讲解,并突出介绍他们的区别. A ...
- iOS oc和swift中协议的使用
创建一个空的工程 在工程中我们新建一个类 继承与NSObject 定义一个协议‘ @protocol UpdateAlertDelegate <NSObject> //这里的红色字体就是我 ...
- U盘为什么还有剩余空间,但却提示说空间不够
你的U盘是FAT32格式,它只支持单一小于4G的文件复制,将U盘改为NTFS格式,可以解决题.方法:开始——运行,输入“cmd”,回车,在命令符后输入:convert h: /fs:ntfs,回车(假 ...
- Java Swing Graphics Graphics2D的一般用法
Java Swing Graphics Graphics2D的一般用法: 贝塞尔曲线参考:http://www.zhangxinxu.com/wordpress/2014/06/deep-unders ...
- Tomcat 使用过程中的一些技巧
url中文地址乱码 原因: tomcat默认的在url传输时是用iso8859-1编码. 解决方案一: 在使用get传输参数时,将参数中的中文转换成url格式,也就是使用urlEncode和urlDe ...