如何在Web API中返回加密的流? [英] How to return encrypted stream in web api?
本文介绍了如何在Web API中返回加密的流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个JSON字符串,我需要将其作为HttpResponseMessage中的Web api中的加密流返回.客户端然后接收加密的流并像这样解密它.
I have a JSON string that I need to return it as an encrypted stream in web api in HttpResponseMessage. The client then receives the encrypted stream and decrypts it like this.
private string str(HttpWebResponse AStream)
{
string result;
using (Stream responseStream = AStream.GetResponseStream())
{
result = DecryptAesStream(responseStream, Key);
return result;
}
}
我是否需要首先加密JSON字符串,将其加载到文件流中,但是由于它需要将字符串作为内容,因此我该如何在HttpRepsonseMessage中返回它?有什么提示我需要做什么吗?
Do I need to encrypt the JSON string first, load it to a filestream but then how do I return it in HttpRepsonseMessage since it takes string as a content? Any hints what I need to do?
推荐答案
您可以尝试这样的事情
public byte[] GetEncryptedStream(string jsonData)
{
byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(jsonData);
byte[] key = null;//GetKey() //I am assuming you arealy have your Key
//Call your encrypt function below
byte[] encryptedDataBytes = encrypt(dataBytes, key); // I am assuming your function returns byte array
return encryptedDataBytes;
}
public HttpResponseMessage GetHttpResponseMessage()
{
var result = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
String jsonString = "your json data";
byte[] data = GetEncryptedStream(jsonString);
result.Content = new ByteArrayContent(data);
return result;
}
这篇关于如何在Web API中返回加密的流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文