Reputation: 51
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;
}
}
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?
Upvotes: 0
Views: 791
Reputation: 993
You can try something like this
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;
}
Upvotes: 1