I am using entity framework(WebAPI/C#/VS2019) and using below code to save file and display file.
Save Code
Document document = new Document();
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(attachment_data); //attachment_data is string
document.document_content=plainTextBytes;//document_content is byte[]
db.Documents.Add(document);
db.SaveChanges();
Below code is used to show saved file
[Route("api/documents/download")]
[HttpGet]
public HttpResponseMessage Download(int documentid)
{
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var document = db.Documents.Where(e => e.id.Equals(documentid)).FirstOrDefault();
if (document != null)
{
var stream = new System.IO.MemoryStream(document.document_content);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType =
new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = document.name
};
}
return result;
}
Both Save file and downloading file is working fine. The issue is how to encrypt the file content before Saving to Database?I saw few SO links but most of the methods are taking string as input and string as output.Not sure which one will fit in this scenario.