一. 场景介绍:
如题如何有效的,最少量的现有代码侵入从而实现客户端与服务器之间的数据交换加密呢"color: #0000ff">1.需求分析
webapi服务端 有如下接口:
public class ApiTestController : ApiController
{
// GET api/<controller>/5
public object Get(int id)
{
return "value" + id;
}
}
ApiTestController
无加密请求
GET /api/apitest"value10"
我们想要达到的效果为:
Get /api/apitest"value10")
或者更多其它方式加密
2.功能分析
要想对现有代码不做任何修改, 我们都知道所有api controller 初始化在router确定之后, 因此我们应在router之前将GET参数和POST的参数进行加密才行.
看下图 webapi 生命周期:
我们看到在 路由routing 之前 有DelegationgHander 层进行消息处理.
因为我们要对每个请求进行参数解密处理,并且又将返回消息进行加密处理, 因此我们 瞄准 MessageProcessingHandler
//
// 摘要:
// 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
三. 实践:
现在我们将来 先实现2个版本的通讯加密解密功能,定为 版本1.0 base64加密, 版本1.1 Des加密
/// <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"htmlcode">
/// <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(0, 8));
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, 0, 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(0, 8));
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, 0, 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")"(code=)*(", 2);
// 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(1);
baseQuery = baseQuery.Match("(code=)*(", 2);
baseQuery = baseQuery.DecodeUrl();
baseQuery = encrypt.Decode(baseQuery);
}
// 将解密后的 URL 重置URL请求
request.RequestUri = new Uri($"{request.RequestUri.AbsoluteUri.Split('");
// 将解密后的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")"htmlcode">
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 = 10;
var resultSuccess = $"\"value{id}\"";
//不加密
Trace.WriteLine($"without encryption.");
var url = $"api/ApiTest";
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" + $"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" + $"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
至此为止功能实现完毕..
四.思想延伸
要想更加安全的方案,可以将给每位用户生成不同的 private key , 利用AES加密解密
本Demo开源地址:
oschina
https://git.oschina.net/jonneydong/Webapi_Encryption
github
https://github.com/JonneyDong/Webapi_Encryption
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
webapi,加密
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
