如何在web api中返回加密流

本文关键字:返回 加密 密流 api web | 更新日期: 2023-09-27 17:59:11

我有一个JSON字符串,需要在HttpResponseMessage中以加密流的形式在web api中返回。然后,客户端接收加密的流,并像这样对其进行解密。

private string str(HttpWebResponse AStream)
{
    string result;
    using (Stream responseStream = AStream.GetResponseStream())
    {
        result = DecryptAesStream(responseStream, Key);
        return result;
    }
}

我是否需要先加密JSON字符串,将其加载到文件流中,但由于它将字符串作为内容,我如何在HttpRepsonseMessage中返回它?有什么提示我需要做什么吗?

如何在web api中返回加密流

您可以尝试类似的

        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;
        }