我需要在我的项目中上传文件。我正在使用asp.net webapi。我想在保存到文件系统之前加密上传。我想在将它发送回客户端之前解密它的方式相同。 我试图在MultipartFormDataStreamProvider的GetStream方法中获取流,但它无法正常工作。 public override Stream GetStream(HttpContent parent,HttpContentHeaders headers) { var stream = base.GetStream(parent,headers); Rijndael myRijndael = Rijndael.Create(); 流msEncrypt = new MemoryStream(); //创建加密器以执行流转换。 ICryptoTransform encryptor = myRijndael.CreateEncryptor(myRijndael.Key,myRijndael.IV); CryptoStream csEncrypt = new CryptoStream(msEncrypt,encryptor,CryptoStreamMode.Write);
ContentDispositionHeaderValue contentDisposition = headers.ContentDisposition;
if (contentDisposition != null)
{
if (!string.IsNullOrEmpty(contentDisposition.FileName))
{
try
{
byte[] bytearrayinput = new byte[stream.Length - 1];
csEncrypt.Write(bytearrayinput, 0, bytearrayinput.Length);
encrypted = msEncrypt;
msEncrypt.Seek(0, SeekOrigin.Begin);
return msEncrypt;
}
catch (Exception)
throw;
}
finally
{
myRijndael.Dispose();
msEncrypt.Close();
}
}
}
return new MemoryStream();
}
任何帮助将不胜感激。 谢谢 卡鲁纳